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
-
Terraform IAM Policy Document Refactor Prompt
Refactor inline JSON IAM policies into composable aws_iam_policy_document data sources with least privilege, while keeping the rendered policy semantically identical.
-
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 Aliased Provider Passing to Nested Modules Prompt
Wire aliased providers (multi-region, multi-account) down through nested module trees using explicit `configuration_aliases` and `providers = {}` maps instead of implicit inheritance.
-
terraform_data Replacement for null_resource Prompt
Migrate legacy null_resource + triggers patterns to the built-in terraform_data resource without forcing spurious replacements
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.