Skip to content
CloudOps
Newsletter
All prompts
AI for Terraform Difficulty: Beginner ClaudeChatGPT

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.
Newsletter

Free: the DevOps AI Incident-Triage Cheat Sheet

Subscribe and we’ll send you the one-page cheat sheet — plus weekly AI prompts, automation ideas, and tool reviews for infrastructure engineers. One email a week. No spam, unsubscribe anytime.

  • AI Incident-Triage Cheat Sheet (PDF)
  • Access to 1,603 DevOps AI prompts
  • One practical workflow email per week