0
0
Terraformcloud~10 mins

Input variable precedence order in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Input variable precedence order
Default variable value
Check terraform.tfvars file
Check environment variables
Check CLI -var options
Final variable value used
Terraform picks variable values by checking defaults first, then tfvars files, then environment variables, and finally CLI options, with later sources overriding earlier ones.
Execution Sample
Terraform
variable "region" {
  default = "us-east-1"
}

# terraform.tfvars contains region = "eu-west-1"
# Assume environment variable TF_VAR_region="us-west-2"
# CLI: terraform apply -var='region=ap-south-1'
Shows how Terraform chooses the region variable value from multiple sources with different precedence.
Process Table
StepSource CheckedValue FoundOverrides Previous?Current Variable Value
1Default value in variable blockus-east-1N/A (initial)us-east-1
2terraform.tfvars fileeu-west-1Yeseu-west-1
3Environment variable TF_VAR_regionus-west-2Yesus-west-2
4CLI -var optionap-south-1Yesap-south-1
💡 All sources checked; CLI -var option has highest precedence and final value is 'ap-south-1'
Status Tracker
VariableStartAfter tfvarsAfter Env VarAfter CLI
regionus-east-1eu-west-1us-west-2ap-south-1
Key Moments - 3 Insights
Why does the CLI -var option override the terraform.tfvars file value?
Because the execution_table shows CLI -var is checked last and overrides all previous values, including tfvars (see step 4).
What happens if no environment variable or CLI option is set?
Terraform uses the terraform.tfvars file value if present; otherwise, it falls back to the default value (see steps 1 and 2).
Can environment variables override CLI options?
No, CLI options have higher precedence than environment variables, so CLI options override environment variables (see steps 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the variable 'region' value after checking the environment variable?
Aus-east-1
Beu-west-1
Cus-west-2
Dap-south-1
💡 Hint
Check the 'After Env Var' column in variable_tracker and step 3 in execution_table.
At which step does the variable value become 'eu-west-1'?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Source Checked' and 'Value Found' columns in execution_table.
If the CLI -var option was not provided, what would be the final variable value?
Aus-west-2
Beu-west-1
Cus-east-1
Dap-south-1
💡 Hint
Refer to the variable_tracker and note the value before 'After CLI' step.
Concept Snapshot
Terraform input variable precedence order:
1. Default value in variable block
2. terraform.tfvars file
3. Environment variables (TF_VAR_*)
4. CLI -var options
Later sources override earlier ones, CLI options have highest priority.
Full Transcript
Terraform variables can have values from multiple places. First, Terraform uses the default value defined in the variable block. Then it checks for values in terraform.tfvars files, which override the default. Next, it looks if an environment variable with the prefix TF_VAR_ is set, which overrides tfvars files. Finally, any CLI -var options provided during terraform apply or plan override all previous values. This order ensures the most specific input wins. For example, if the default region is 'us-east-1', tfvars file sets 'eu-west-1', environment variable sets 'us-west-2', and CLI option sets 'ap-south-1', the final region used is 'ap-south-1'.