Terraform Associate 004 ยท Configuration building blocks
Variable value precedence
Order of precedence for environment, terraform.tfvars, *.auto.tfvars, -var-file, -var, and interactive prompts (4c).
Key points
- Lowest to highest: the variable's default, TF_VAR_ environment variables, terraform.tfvars, terraform.tfvars.json, *.auto.tfvars (lexical order), then -var and -var-file on the command line (later flags beat earlier ones).
- HCP Terraform workspace variables rank with the command line.
- If nothing sets the value and there is no default, Terraform prompts.
Source: https://developer.hashicorp.com/terraform/language/values/variables
Practice questions
Which source wins over all the others listed?
- The default in the variable block
- A TF_VAR_ environment variable
- terraform.tfvars
- The -var command-line flag
Show answer
Answer: D. The -var command-line flag
Command-line -var overrides tfvars files, auto.tfvars files, environment variables and defaults.
Which of these sources are loaded automatically, without a flag naming them?
- terraform.tfvars
- prod.auto.tfvars
- prod.tfvars
- A file passed by -var-file
- TF_VAR_ environment variables
Show answer
Answer: A. terraform.tfvars, B. prod.auto.tfvars, E. TF_VAR_ environment variables
terraform.tfvars and *.auto.tfvars files in the working directory are picked up automatically. A custom-named file needs -var-file, and -var is itself a flag.
True or false: a TF_VAR_ environment variable overrides a value set in terraform.tfvars.
Show answer
Answer: False
False. Environment variables rank below terraform.tfvars, so the file value wins.
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.
