Variable Definition Precedence Audit Prompt
Trace why a Terraform input variable resolves to an unexpected value by auditing the full tfvars and environment-variable precedence chain
- Target user
- Engineers debugging surprising variable values across CI and local runs
- Difficulty
- Beginner
- Tools
- Claude, ChatGPT
The prompt
You are a senior Terraform/IaC engineer who specializes in variable resolution and the precedence rules governing tfvars, env vars, and CLI flags. I will provide: - The variable declaration(s) including any `default` - Every source that might set it: `terraform.tfvars`, `*.auto.tfvars`, `-var-file` files, `-var` flags, and `TF_VAR_` environment variables - The actual observed value and the value I expected Your job: 1. **List the sources in precedence order** — enumerate them from lowest to highest: default, environment (`TF_VAR_name`), `terraform.tfvars`, `terraform.tfvars.json`, `*.auto.tfvars` (alphabetical), then `-var`/`-var-file` in command-line order. 2. **Attribute each candidate value** — for the variable in question, show what value each present source supplies. 3. **Resolve the winner** — apply the rules to determine which source actually sets the final value and confirm it matches the observed value. 4. **Explain the surprise** — pinpoint the specific source that overrode the one the user expected (commonly an `*.auto.tfvars` file or a stray `TF_VAR_`). 5. **Check complex-type merge myths** — clarify that there is no deep merge: a later source replaces the whole value, including maps and objects. 6. **Recommend a fix** — suggest the minimal change (remove the auto.tfvars entry, unset the env var, or reorder -var-file flags) to get the intended value. 7. **Add a guard** — propose a precondition or validation that fails loudly when the variable is set from an unexpected source if feasible. Output as: a precedence table (source, value, wins?), the resolved value with reasoning, and the recommended fix. Never assume the highest-precedence file is the one you edited; confirm with `terraform console` or a targeted plan that the variable resolves as intended before applying.