Variable files and precedence
Assign a value without rewriting the declaration
A variable declaration defines a module input; assignment supplies a value. A variable definition file holds assignments, not variable blocks. In the following illustration the comments mark two separate files:
# variables.tf (declaration)variable "replicas" { type = number default = 1}
# terraform.tfvars (assignment, separate file)replicas = 3The terraform.tfvars assignment selects 3 instead of the default 1. The accompanying local fixture exposes var.replicas through an output; it does not allocate infrastructure.
Trace local CLI precedence
For a normal local root module, read the input sources from weakest to strongest: (1) declaration default, (2) TF_VAR_<name> environment variable, (3) terraform.tfvars, (4) terraform.tfvars.json, (5) all *.auto.tfvars and *.auto.tfvars.json files sorted lexically by filename, then (6) explicit -var and -var-file flags processed in the order written. A later assignment of the same input wins. These rules select the value for each variable independently. They do not execute configuration files sequentially. A required variable with no supplied value prompts when permitted; -input=false makes an unassigned required input an error instead. Child modules take values from their caller's module arguments rather than automatically loading their own tfvars files.
Follow a complete example
The precedence/ fixture declares replicas with default 1. Its terraform.tfvars supplies 3, its terraform.tfvars.json supplies 4, 10-team.auto.tfvars supplies 5 and 20-lab.auto.tfvars.json supplies 6. The explicit review.tfvars supplies 7 and is not automatically loaded. In a clean POSIX shell and initialized fixture, try the following separate commands only after verifying the CLI and isolation requirements. TF_VAR_replicas=2 applies to the command it prefixes.
TF_VAR_replicas=2 terraform plan -input=falseTF_VAR_replicas=2 terraform plan -input=false -var-file=review.tfvars -var='replicas=8'TF_VAR_replicas=2 terraform plan -input=false -var='replicas=8' -var-file=review.tfvarsPredicted output values are 6, 8 and 7 respectively. The last flag wins, not necessarily the -var flag. No apply or saved plan is required. If no source assigns a required variable, disabling input makes the run fail. See the separate required-input/ lab for this case. Shell quoting differs on Windows; use a variable file rather than copying POSIX shell syntax there.
Use structured files for complex inputs
For lists and maps, files avoid many shell quoting problems. These examples show two alternative file formats, not two files to load together:
# review.tfvarsregions = ["north", "south"]labels = { team = "catalog" }{ "regions": [ "north", "south" ], "labels": { "team": "catalog" }}The receiving declarations would use list(string) and map(string) respectively. Assigning a map at higher precedence generally replaces the input value as a whole; do not assume Terraform recursively combines it with an earlier map. Give files meaningful names and choose explicit -var-file flags for reviewable environment choices. Avoid automatically loaded files when their implicit overrides would surprise collaborators.
Keep hosted workflows and secrets separate
HCP Terraform remote execution has additional precedence rules involving workspace variables, variable sets, ownership and priority. A priority set can take precedence over a more specific value; local CLI ordering cannot be used as a universal hosted-workflow rule. Consult the current HCP documentation for the exact run mode. A Terraform variable is also different from a general environment variable used by a provider or the CLI. Keep actual credentials out of tracked files, shell history and command arguments; TF_VAR_ alone is not a secret store. The files in this lab contain fictional nonsecret data only.
Practice resolving conflicting assignments
Use the worksheet to trace a default of 1, environment 2, terraform.tfvars 3, 05-team.auto.tfvars 4, 90-local.auto.tfvars.json 5, explicit -var-file=review.tfvars setting 6 and final -var='replicas=7'. Determine the result with all sources, after reversing the flags, and without the flags. Then explain what would happen if the input had no default and no assignments with -input=false. Next: locals and derived values. Related reference: Variable files and precedence.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Variable files and precedence — knowledge check
Check input contracts and assignment decisions.
Which local input source has the lowest precedence?
Which auto file wins for a shared key?
With -var=replicas=8 followed by -var-file=review.tfvars setting 7, what wins?
What occurs when a required root input is missing and -input=false is used?
Why is local precedence not universal in HCP Terraform?