Variables & defaults
Give configuration inputs clear types
Lesson 2 of 20 · 12 min · Module 1: Declare a configuration contract
A typed input boundary makes environment differences visible.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Name the input instead of scattering literals
- Preserve the declared type
The idea
Terraform accepts .tf.json as an alternative configuration representation. Here, built-in terraform_data values make state and plan concepts inspectable without provisioning a cloud resource.
{
"variable": {
"environment": {
"type": "string",
"default": "staging"
}
}
}
01Name the input instead of scattering literals
A variable makes a configurable choice explicit. Keep its meaning narrow enough that callers know what they are changing.
02Preserve the declared type
The JSON expression string ${var.replicas} represents a Terraform reference, not necessarily a string result. The lab resolves an exact variable reference to its typed value.
03Separate default from supplied value
A default is used when no input is supplied. A later lesson supplies a scenario override to demonstrate the same configuration in another environment.
Your turn
What the checks look for
- Declares a numeric variable
- Resolves a number rather than text
Take this with you
Declare input types and preserve them through configuration references.
Go a little further
Supply a string for the numeric scenario variable and inspect the type error.