Skip to content

/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

bash
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-cli

Output

  + 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

#SourceLoaded
1default in the variable blockalways
2TF_VAR_name environment variablesalways
3terraform.tfvarsautomatically, if present
4terraform.tfvars.jsonautomatically, if present
5*.auto.tfvars, *.auto.tfvars.jsonautomatically, in alphabetical order
6-var-file=FILE and -var name=valuein command-line order

Sources