0
0
Terraformcloud~10 mins

Environment variables (TF_VAR_) in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Environment variables (TF_VAR_)
Set environment variable TF_VAR_name=value
Run terraform command
Terraform reads environment variables
Terraform maps TF_VAR_name to variable 'name'
Variable 'name' gets value from environment
Terraform applies configuration using variable value
Terraform reads environment variables starting with TF_VAR_ and uses their values to set input variables during execution.
Execution Sample
Terraform
export TF_VAR_region="us-west-2"
terraform apply
Sets the variable 'region' to 'us-west-2' via environment variable before running terraform apply.
Process Table
StepActionEnvironment VariableTerraform VariableValue Assigned
1Set environment variableTF_VAR_region=us-west-2--
2Run terraform apply---
3Terraform reads environment variablesTF_VAR_region=us-west-2--
4Map TF_VAR_region to variable 'region'TF_VAR_regionregionus-west-2
5Use variable 'region' in configuration-regionus-west-2
💡 Terraform completes apply using 'region' variable set from environment variable TF_VAR_region
Status Tracker
VariableStartAfter Step 4Final
regionundefinedus-west-2us-west-2
Key Moments - 3 Insights
Why does Terraform use the environment variable TF_VAR_region for the variable 'region'?
Terraform automatically maps environment variables starting with TF_VAR_ to input variables by removing the prefix. See execution_table row 4 where TF_VAR_region maps to 'region'.
What happens if the environment variable TF_VAR_region is not set?
The variable 'region' remains undefined unless set elsewhere (like in a tfvars file or default). Terraform will prompt or error if required. This is implied by the variable_tracker showing 'undefined' at start.
Can environment variables override variables set in files?
Yes, environment variables TF_VAR_ have higher precedence and override values from files. This is why Terraform reads environment variables first as shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what value is assigned to the Terraform variable 'region' at step 4?
Aus-west-2
Bundefined
CTF_VAR_region
Dnull
💡 Hint
Check the 'Value Assigned' column at step 4 in the execution_table.
At which step does Terraform read the environment variables?
AStep 1
BStep 3
CStep 5
DStep 2
💡 Hint
Look at the 'Action' column in execution_table where Terraform reads environment variables.
If TF_VAR_region was not set, what would the variable_tracker show for 'region' after step 4?
Aus-west-2
Bnull
Cundefined
Dempty string
💡 Hint
Refer to variable_tracker showing 'region' as 'undefined' before assignment.
Concept Snapshot
Terraform environment variables starting with TF_VAR_ set input variables.
Syntax: export TF_VAR_variableName=value
Terraform reads these before applying config.
They override variable defaults and tfvars files.
Use to pass sensitive or dynamic values easily.
Full Transcript
Terraform allows setting input variables using environment variables prefixed with TF_VAR_. For example, setting TF_VAR_region to 'us-west-2' assigns the variable 'region' this value during terraform apply. The flow starts by exporting the environment variable, then running terraform commands. Terraform reads these variables, maps them by removing the TF_VAR_ prefix, and uses the values in the configuration. If the environment variable is not set, the variable remains undefined unless set elsewhere. Environment variables override other variable sources, making them useful for dynamic or secret values.