Bitzu

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

Single choice

Which source wins over all the others listed?

  1. The default in the variable block
  2. A TF_VAR_ environment variable
  3. terraform.tfvars
  4. 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.

Select all that apply

Which of these sources are loaded automatically, without a flag naming them?

  1. terraform.tfvars
  2. prod.auto.tfvars
  3. prod.tfvars
  4. A file passed by -var-file
  5. 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

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.

Download on the App StoreGet it on Google Play

More Terraform Associate 004 topics in this module