Terraform Associate 004 ยท Configuration building blocks
Input variables
variable block arguments: type, default, description, nullable, ways to set values (-var, tfvars, TF_VAR_ env) (4c).
Key points
- A variable "NAME" {} block declares a module input.
- Common arguments: type, default, description, nullable (true by default), sensitive and validation.
- Without a default, Terraform asks for a value.
- Values can come from -var, -var-file, *.tfvars files or TF_VAR_name environment variables.
- Reference a value as var.NAME.
Practice questions
What happens when a root module variable has no default and no value is supplied?
- Terraform assumes null and continues
- Terraform prompts for a value before planning
- Terraform uses an empty string
- terraform init fails
Show answer
Answer: B. Terraform prompts for a value before planning
Terraform prompts interactively for the value. It does not silently use null or an empty string, and it does not fail at init.
Which are valid ways to supply a value for a root module variable?
- terraform apply -var="name=value"
- An environment variable named TF_VAR_name
- A line in a terraform.tfvars file
- An output block in the same module
- A resource block with the same name
Show answer
Answer: A. terraform apply -var="name=value", B. An environment variable named TF_VAR_name, C. A line in a terraform.tfvars file
-var, variable definition files and TF_VAR_ environment variables are all supported. A resource block cannot feed a variable, and output blocks only expose values.
True or false: marking a variable sensitive = true keeps its value out of the state file.
Show answer
Answer: False
False. sensitive only hides the value in CLI output; it is still stored in state. Protect state accordingly.
Practice 8 questions on this topic in Bitzu
Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.
