002 · Domain 4: CI/CD and Automation Workflows · 10% of the exam
Task 4.4: Overwrite Terraform variables using auto.tfvars / .tfvars
Which value wins: terraform.tfvars, *.auto.tfvars in lexical order, -var-file and -var in command-line order, TF_VAR_ environment variables, and how HCP Terraform workspace variables fit in.
Study it
Which value wins: .tfvars, .auto.tfvars, -var and TF_VAR_
Lesson coming
Sample questions
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
A root module directory contains these files, and the shell also has
TF_VAR_region=ap-south-1 set:
terraform.tfvars region = "us-east-1"
a-defaults.auto.tfvars region = "us-west-1"
z-team.auto.tfvars region = "eu-west-1"
The engineer runs terraform plan with no -var or -var-file options. Which
region does the plan use?
Aeu-west-1, because z-team.auto.tfvars is processed last of the .auto.tfvars files
Bus-east-1, because terraform.tfvars always outranks other files
Cus-west-1, because a-defaults.auto.tfvars is loaded first and sets the value
Dap-south-1, because environment variables are applied after files
Show the answer and why
Aeu-west-1, because z-team.auto.tfvars is processed last of the .auto.tfvars files
Correct
Automatic files load in lexical order, so the z- file wins, and .auto.tfvars files outrank terraform.tfvars and environment variables.
Bus-east-1, because terraform.tfvars always outranks other files
Incorrect
.auto.tfvars files take precedence over terraform.tfvars.
Cus-west-1, because a-defaults.auto.tfvars is loaded first and sets the value
Incorrect
.auto.tfvars files are processed in lexical order, and a later file overrides an earlier one.
Dap-south-1, because environment variables are applied after files
Incorrect
TF_VAR_ variables rank below every variable definition file.
TF_VAR_ environment variables rank below every variable definition file, terraform.tfvars ranks below the .auto.tfvars files, and .auto.tfvars files load in lexical order, so the last of them wins.
A configuration relies on terraform.tfvars and runs in both HCP Terraform and Terraform Enterprise. What does the HCP Terraform variables documentation note?
ATerraform Enterprise currently ignores terraform.tfvars
BBoth products ignore terraform.tfvars
Cterraform.tfvars overrides workspace variables in both products
DBoth read it only when -var-file names it
Show the answer and why
ATerraform Enterprise currently ignores terraform.tfvars
Correct
HCP Terraform uses terraform.tfvars, below .auto.tfvars files, while Terraform Enterprise currently ignores this file.
BBoth products ignore terraform.tfvars
Incorrect
HCP Terraform uses the file.
Cterraform.tfvars overrides workspace variables in both products
Incorrect
Workspace variables overwrite file values.
DBoth read it only when -var-file names it
Incorrect
HCP Terraform loads it automatically.
For portable setups, move values into workspace variables, variable sets or .auto.tfvars files.
A VCS-driven HCP Terraform workspace sets the Terraform variable instance_type = "m5.large". The repository also contains prod.auto.tfvars with instance_type = "t3.micro". Which value does the run use?
At3.micro, as files in the repository win
BWhichever was changed most recently
Cm5.large, as HCP Terraform settings win
DNeither; the run fails on the conflict
Show the answer and why
At3.micro, as files in the repository win
Incorrect
Workspace variables overwrite .auto.tfvars values with the same key.
BWhichever was changed most recently
Incorrect
Precedence is fixed by source, not by time.
Cm5.large, as HCP Terraform settings win
Correct
Variables in the workspace, and those given on the command line, always overwrite .auto.tfvars values with the same key.
DNeither; the run fails on the conflict
Incorrect
Conflicts are resolved by precedence.
Remove values from .auto.tfvars files once a workspace variable owns them, so reviewers are not misled.