Terraform Monolith to Modules Refactor Prompt
Break a sprawling single root module into reusable child modules using moved blocks so addresses stay intact and no resource is destroyed.
- Target user
- Teams refactoring a large flat Terraform root module
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior Terraform engineer who has split large flat root modules into clean, reusable child modules without destroying a single live resource. I will provide: - The current flat root module (or a representative excerpt) - The grouping I want (e.g. network, compute, data) - The backend and how state is managed Your job: 1. **Propose the module boundaries** — group resources into child modules with clear responsibilities, defining each module's inputs and outputs. 2. **Preserve addresses logically** — for every resource that moves into a child module, plan the new address (`module.<name>.<type>.<name>`) and pair it with a `moved` block from the old root address. 3. **Wire the data flow** — replace direct references with module `output`/input plumbing, avoiding circular dependencies. 4. **Stage the refactor** — recommend doing one module group at a time, each as its own PR, rather than a big-bang rewrite. 5. **Prove no destroys** — instruct me to run `terraform plan` after wiring the moved blocks and confirm the plan is a pure set of moves with zero create/destroy. 6. **Handle for_each/count** — adjust moved block addresses for indexed and keyed resources correctly. 7. **Clean up** — note when moved blocks can be removed (after everyone has applied) and how to verify state. Output as: (a) the module layout, (b) refactored HCL for one group, (c) the full set of moved blocks, (d) the expected move-only plan. Do not apply until terraform plan shows only moves; any create or destroy means an address is wrong — fix the moved block first.
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 Moved & Import Blocks Prompt
Use declarative `moved` and `import` blocks to refactor and adopt resources without manual `terraform state mv`/`import` — keeping every change reviewable in a plan and reproducible in CI.
-
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 Removed Block Forget-Only Prompt
Use a `removed` block with `lifecycle { destroy = false }` to drop a resource from Terraform management without destroying the real infrastructure — the declarative replacement for `terraform state rm`.
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.