Skip to content

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?

  1. Aeu-west-1, because z-team.auto.tfvars is processed last of the .auto.tfvars files
  2. Bus-east-1, because terraform.tfvars always outranks other files
  3. Cus-west-1, because a-defaults.auto.tfvars is loaded first and sets the value
  4. 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.

Question 2 · choose 1

A value is assigned to a variable that the root module does not declare. Which handling matches the documentation?

  1. AEvery method fails with an error
  2. BEvery method is ignored without notice
  3. CDefinition files error, while -var and environment values are ignored
  4. DEnvironment variables are ignored, files warn and -var errors
Show the answer and why
  • AEvery method fails with an error

    Incorrect

    Behavior differs by assignment method.

  • BEvery method is ignored without notice

    Incorrect

    Files warn and -var errors.

  • CDefinition files error, while -var and environment values are ignored

    Incorrect

    That reverses the documented behavior.

  • DEnvironment variables are ignored, files warn and -var errors

    Correct

    Terraform ignores undeclared TF_VAR_ values, warns for definition files and errors for -var.

The file warning catches typos in variable definition files without breaking shared environment setups.

Question 3 · choose 1

A directory has both terraform.tfvars and terraform.tfvars.json, each setting region differently, and no other variable inputs. Which value wins?

  1. AThe value in the plain .tfvars file
  2. BThe value in the JSON file
  3. CNeither; Terraform errors on the duplicate
  4. DWhichever file was modified most recently
Show the answer and why
  • AThe value in the plain .tfvars file

    Incorrect

    terraform.tfvars ranks below terraform.tfvars.json.

  • BThe value in the JSON file

    Correct

    terraform.tfvars.json sits above terraform.tfvars in the precedence order.

  • CNeither; Terraform errors on the duplicate

    Incorrect

    Later sources override earlier ones instead of erroring.

  • DWhichever file was modified most recently

    Incorrect

    Precedence does not depend on file times.

Avoid keeping both files; one source per value is easier to review.

Question 4 · choose 1

A configuration relies on terraform.tfvars and runs in both HCP Terraform and Terraform Enterprise. What does the HCP Terraform variables documentation note?

  1. ATerraform Enterprise currently ignores terraform.tfvars
  2. BBoth products ignore terraform.tfvars
  3. Cterraform.tfvars overrides workspace variables in both products
  4. 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.

Question 5 · choose 1

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?

  1. At3.micro, as files in the repository win
  2. BWhichever was changed most recently
  3. Cm5.large, as HCP Terraform settings win
  4. 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.

Practice domain 4 →Practice all domains →