/config/terraform/variable-precedence-tfvars
Terraform variable precedence: tfvars, auto.tfvars, -var and TF_VAR_
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · Terraform v1.16.6updated 2026-10-20
- tool
- Terraform
- file
- terraform.tfvars / *.auto.tfvars / *.tfvars
- key
- variable values
- type
- HCL assignments: name = value
- default
- the variable's default
A root module variable can get its value from several places. Terraform loads them in a fixed order and the last one wins: the default in the variable block, then TF_VAR_name environment variables, terraform.tfvars, terraform.tfvars.json, *.auto.tfvars files in alphabetical order, and finally -var-file and -var options in the order they appear on the command line.
See the order
cat > main.tf <<'EOF'
variable "env" {
type = string
default = "from-default"
}
output "env" {
value = var.env
}
EOF
terraform init -input=false -no-color >/dev/null
show() { terraform plan -no-color -input=false "$@" | grep '+ env'; }
show
TF_VAR_env=from-env-var show
echo 'env = "from-terraform.tfvars"' > terraform.tfvars
TF_VAR_env=from-env-var show
echo 'env = "from-auto.tfvars"' > prod.auto.tfvars
show
echo 'env = "from-var-file"' > ci.tfvars
show -var-file=ci.tfvars
show -var-file=ci.tfvars -var env=from-cliOutput
+ env = "from-default" + env = "from-env-var" + env = "from-terraform.tfvars" + env = "from-auto.tfvars" + env = "from-var-file" + env = "from-cli"
TF_VAR_env loses to terraform.tfvars, which loses to *.auto.tfvars, which loses to explicit -var-file and -var.
Order, first to last
| # | Source | Loaded |
|---|---|---|
| 1 | default in the variable block | always |
| 2 | TF_VAR_name environment variables | always |
| 3 | terraform.tfvars | automatically, if present |
| 4 | terraform.tfvars.json | automatically, if present |
| 5 | *.auto.tfvars, *.auto.tfvars.json | automatically, in alphabetical order |
| 6 | -var-file=FILE and -var name=value | in command-line order |
Sources
Related pages
errors · terraformTerraform: Reference to undeclared input variableterraform validate and plan fail with Reference to undeclared input variable when code uses var.name without a variable block in the same module. How to fix it.flags · terraformterraform plan -out: save a plan and apply exactly that planterraform plan -out=tfplan saves the plan to a file; terraform apply tfplan then applies exactly those changes without a prompt. Inspect it with terraform show.cheat sheets · terraformTerraform cheat sheet: init, plan, apply, state and workspacesTerraform cheat sheet: init and upgrade, fmt and validate, plan and apply, outputs, state list, mv and rm, import, workspaces, and common language syntax.