Terraform Variable Precedence Practice
Free Terraform practice for the Terraform Associate (004): read the files and the command, then pick the value a variable gets from -var, -var-file, auto files, terraform.tfvars, TF_VAR_ or its default, or inside a child module. Check my answer names the slip, such as taking the first -var or letting TF_VAR_ beat terraform.tfvars.
The files and values are made up for practice. Each problem runs in Bash in the root module folder, and every variable a file sets is declared.
Practise the exam
The Terraform Associate (004) exam content list covers variables in objective 4c and variable scope within modules in 5b. Start with the free real exam questions, then get the full pack.
Terraform FAQ
What is the Terraform variable precedence order?
From highest to lowest: -var and -var-file options on the command line in the order given, *.auto.tfvars and *.auto.tfvars.json files in lexical order, terraform.tfvars.json, terraform.tfvars, TF_VAR_ environment variables, and the default in the variable block.
Does a TF_VAR_ environment variable override terraform.tfvars?
No. HashiCorp describes TF_VAR_ environment variables as a fallback for the other ways of setting variables. terraform.tfvars, terraform.tfvars.json, auto files and command-line options all beat them. Only the default ranks lower.
Which wins, terraform.tfvars or an *.auto.tfvars file?
The auto file. Terraform loads terraform.tfvars, then terraform.tfvars.json, then the *.auto.tfvars and *.auto.tfvars.json files, and later sources take precedence over earlier ones.
If I pass -var twice for the same variable, which value is used?
The last one. Terraform applies -var and -var-file options in the order they are provided, and when a variable gets several values it uses the last value it finds.
In what order are several *.auto.tfvars files loaded?
In lexical order of their file names, and the file loaded later wins. If a.auto.tfvars and b.auto.tfvars both set a variable, the value in b.auto.tfvars is used.
Does Terraform load prod.tfvars on its own?
No. Terraform loads files named terraform.tfvars and terraform.tfvars.json and *.auto.tfvars or *.auto.tfvars.json files. Any other tfvars file is read when you pass it with -var-file.
Can terraform.tfvars set a variable inside a child module?
No. tfvars files, TF_VAR_ and the -var and -var-file options set variables of the root module. A child module gets its input values from the arguments in its module block, and uses its own default when the block does not set the variable.
Where do HCP Terraform workspace variables fit?
HashiCorp groups variables from HCP Terraform with the command-line options at the top of the order. HCP Terraform also has its own precedence for variable sets, so this drill leaves it out.
Which Terraform Associate 004 objectives cover this?
The exam content list for the Terraform Associate (004) includes 4c, use variables and outputs, and 5b, describe variable scope within modules. HashiCorp says the 004 exam tests on Terraform version 1.12.
Sources
- HashiCorp Developer, Use input variables to add module arguments: the order of precedence for root module variables, the files Terraform loads on its own, TF_VAR_, -var-file, and child modules getting their inputs as arguments.
- HashiCorp Developer, Input Variables, Terraform v1.11.x: the load order with later sources taking precedence, auto files processed in lexical order of their file names, options in the order they are provided, and the default used when no value is set.
- HashiCorp Developer, Command: plan (-var and -var-file set variables declared in the root module, for plan and apply) and Use modules in your configuration (module inputs are arguments in the module block).
- HashiCorp tutorial, Customize Terraform configuration with variables: Terraform uses the last value it finds, in order of precedence.
- Exam fit: HashiCorp, Exam Content List, Terraform Associate 004 (objectives 4c and 5b).
- Checked 20 November 2026. The tool's own approach: the files, names and values are invented, and auto file names first differ at two letters or two digits so lexical order is plain. Left out because these pages give no exact rule: HCP Terraform workspace variables and variable sets, -var-file pointing at a file Terraform also loads on its own, maps and objects, and undeclared variables. An independent re-solver that reads only the files, the command and the question agreed with the drill on 12,000 generated problems. This is an independent practice tool, not made or endorsed by HashiCorp.
Checked 20 November 2026 against the HashiCorp Terraform documentation and the Terraform Associate (004) exam content list.
- DrillTerraform version constraint practiceTap the versions that meet ~>, >=, < and != constraints and pick the one Terraform downloads, for the Terraform Associate (004).
- CalculatorUmask calculatorThe default file and directory permissions for any umask, or the umask for the permissions you want.
- DrillLinux file permission practiceDecide if a user can read, write, run, delete or create from two ls -l lines.