A CI job runs terraform plan in a root module directory that contains dev.tfvars, staging.tfvars and prod.tfvars, but the plan uses each variable's default instead of the staging values. The three environment files must stay side by side in that directory. What is the correct fix for the staging job?
Select an answer to reveal the explanation.
Short Explanation
Terraform only picks up files with the magic names on its own: terraform.tfvars, terraform.tfvars.json, and anything ending in .auto.tfvars or .auto.tfvars.json. Anything else you have to hand over explicitly with -var-file. Renaming all three to .auto would load all of them at once, and you'd get whichever sorts last.
Full Explanation
Terraform automatically loads only terraform.tfvars, terraform.tfvars.json, and files ending in .auto.tfvars or .auto.tfvars.json. A file named staging.tfvars is a valid variable definition file, but it is read only when passed with -var-file on the command line, which also gives it the highest precedence among file sources. Adding -var-file=staging.tfvars to the staging job loads the intended values while keeping one file per environment. Renaming every file with the .auto suffix would load all three on every run; with conflicting keys, the lexically last file, prod.auto.tfvars, would silently win in every environment. TF_VAR_FILE is not a mechanism for naming a definition file; Terraform would treat it as an attempt to set a variable called FILE and ignore it unless such a variable exists. The -backend-config option supplies partial backend settings during init and has nothing to do with input variables. Exam caveat: a missing -var-file is silent when variables have defaults, so the plan can look plausible while using the wrong values. Operational check: run the plan with the option and confirm the staging-specific values in the output, or declare staging-critical variables without defaults so a forgotten file fails fast.