Interview Prep/Terraform/Resources Variables and Expressions

Which value wins when a variable is set in several places?

BeginnerScenarioResources Variables and Expressions
Answer

Situation read → A teammate's -var is being silently overridden.

What I'd do → Recall the order, lowest to highest precedence:

  1. Defaults in the variable block.
  2. TF_VAR_name environment variables.
  3. terraform.tfvars, then terraform.tfvars.json.
  4. *.auto.tfvars files in alphabetical order.
  5. -var and -var-file on the command line, in the order given.

Why → Later sources override earlier ones, and the CLI always wins. Knowing this explains "why isn't my tfvars value used?" in seconds.

Interview tip → Check terraform console and var.name to see the resolved value.

All Terraform questions Roadmap: Terraform

More on Resources Variables and Expressions