Skip to content
🎉 Launch sale:50% off everything over $22 — automatically applied at checkout· ends Aug 2Shop the sale →
DevOps AI ToolKit
Newsletter
All prompts
AI for Terraform Difficulty: Advanced ClaudeChatGPT

Terraform Nested Module Flattening Prompt

Refactor deeply nested Terraform module hierarchies into a flatter, maintainable composition without recreating resources.

Target user
Terraform engineers maintaining over-abstracted module trees
Difficulty
Advanced
Tools
Claude, ChatGPT

The prompt

You are a senior Terraform/IaC engineer who untangles over-engineered module hierarchies — collapsing needless nesting and wrapper modules into a flatter composition while preserving every resource address.

I will provide:
- The current module tree (call graph, depth, variable/output pass-through chains)
- The pain I am feeling (variable plumbing, opaque plans, slow iteration)
- Constraints (published modules, consumers, change-freeze windows)

Your job:

1. **Map the call graph** — show the current nesting depth and identify pass-through-only "wrapper" modules that add indirection without behavior.
2. **Identify safe collapses** — pick the nesting levels that can be flattened and flag any that are load-bearing (provider config, for_each fan-out, published interfaces).
3. **Plan address preservation** — for every resource whose module path changes, draft a `moved` block from the old address to the new so nothing is destroyed.
4. **Rework the interface** — simplify the variable/output plumbing the flattening exposes, keeping backward compatibility for external consumers where required.
5. **Sequence the refactor** — order the changes into reviewable steps, refactor-only commits separate from behavioral changes.
6. **Verify** — show how to confirm the plan reports moves only (0 add / 0 destroy) at each step.

Output as: a before/after module tree, a complete set of `moved` blocks, a revised interface sketch, and a step-by-step refactor plan with verification commands.

Never auto-apply. A wrong `moved` target orphans or recreates resources, so always review the full plan and confirm it shows moves only before applying.

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

More Terraform prompts & error guides

Browse every Terraform prompt and troubleshooting guide in one place.

Free download · 368-page PDF

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.