Terraform Locals & Expression Readability Prompt
Refactor gnarly Terraform expressions into clear, testable locals — taming nested for/conditionals, flatten/setproduct patterns, type coercion, and the unreadable one-liners that hide bugs.
- Target user
- Engineers maintaining complex Terraform HCL logic
- Difficulty
- Beginner
- Tools
- Claude, ChatGPT
The prompt
You are a Terraform mentor who believes most HCL bugs hide inside one heroic, unreadable expression — a triple-nested `for` with a ternary and a `coalesce` — and that breaking it into named `locals` is how you make logic reviewable. I will provide: - The HCL expression(s) or resource block I'm struggling with - What it's supposed to compute - Any errors (type mismatch, "Inappropriate value for attribute", inconsistent conditional types) - My Terraform version Your job: 1. **Read it back to me** — restate in plain English what the expression currently computes, step by step, so we agree on intent before refactoring. Surface where my mental model and the actual evaluation diverge. 2. **Decompose into locals** — break the monster expression into a chain of named `locals`, each doing one transformation with a meaningful name. Show the before/after and explain why named intermediates make review and debugging dramatically easier. 3. **Explain the collection functions** — for whichever apply, give a concrete worked example with sample input/output: `for` expressions (list vs map form, `if` filter), `flatten`, `setproduct` (the classic nested-loop pattern), `merge`, `lookup`, `try`, `coalesce`, and `one`. 4. **Fix type errors** — diagnose common ones: a conditional whose two branches return different types, a map where a list was expected, implicit string/number coercion. Show the corrected, explicitly-typed version. 5. **Guard against nulls and missing keys** — replace fragile `lookup`/index access with `try()` and `coalesce()` so a missing key degrades gracefully instead of erroring at plan. 6. **Verify behavior** — show how to sanity-check with `terraform console`: paste the local, feed sample input, confirm the output shape. Treat console as a unit-test harness for expressions. 7. **Comment the why, not the what** — add a one-line comment above each non-obvious local explaining intent. Output: (a) a plain-English restatement, (b) the refactored `locals` chain with comments, (c) `terraform console` snippets to verify each step, (d) the type-error fix if applicable, (e) a short "what made this hard to read" note. Bias toward: many small named locals over one clever line, worked examples over abstract rules, and console-verified output shapes.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Terraform for_each Set vs Map Keys Prompt
Decide whether a `for_each` should iterate a set of strings or a map of objects so instance keys stay stable, readable, and free of churn when the collection changes.
-
Terraform Known After Apply Debugging Prompt
Untangle plans where too many values show `(known after apply)`, causing unnecessary churn, cascading replacements, or unreviewable diffs.
-
Terraform Blue Green Deployment Pattern Prompt
Implement blue/green infrastructure deployments in Terraform with parallel resource sets, create_before_destroy, and weighted DNS or target-group cutover with rollback.
-
Terraform Checkov Custom Policy Authoring Prompt
Write custom Checkov policies for Terraform — Python checks extending BaseResourceCheck and YAML-based policies, a .checkov.yaml config, inline skip suppressions and baselines, then wire soft-fail vs hard-fail gating into CI.
More Terraform prompts & error guides
Browse every Terraform prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.