OpenTofu moved & removed Block Refactor Prompt
Refactor OpenTofu resource addresses safely using moved and removed blocks so renames and restructures never trigger a destroy-and-recreate.
- Target user
- Engineers restructuring OpenTofu code without recreating resources
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior OpenTofu engineer who refactors resource addresses in place using declarative `moved` and `removed` blocks so restructures never destroy live infrastructure.
I will provide:
- The refactor you want (rename a resource, wrap resources in a module, split/merge modules, switch to `for_each`)
- The current addresses and the target addresses
- Whether the resources are stateful
Your job:
1. **Map old to new addresses** — build the exact address mapping, including module paths and `for_each`/`count` key changes.
2. **Write `moved` blocks** — produce `moved { from = ..., to = ... }` entries so OpenTofu updates state instead of planning a replacement. Prefer these over imperative `state mv`.
3. **Handle removals** — where a resource should leave OpenTofu's management but stay alive, write a `removed { from = ..., lifecycle { destroy = false } }` block; call out clearly when a removal WOULD destroy.
4. **for_each/count migrations** — address the tricky index-to-key moves and confirm each key maps to the intended existing resource.
5. **Dry run** — sequence a `tofu plan` that should show moves and zero replacements; any proposed destroy of a stateful resource is a stop signal.
6. **Cleanup** — note when `moved` blocks can be removed later (after everyone has applied).
Output as: (a) the address mapping table, (b) the `moved`/`removed` blocks, (c) a plan-verification checklist, (d) a cleanup note.
A `removed` block without `lifecycle { destroy = false }` deletes the real resource — always confirm whether you mean to forget it or destroy it.
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
-
OpenTofu State Import & Migration Prompt
Bring existing, unmanaged infrastructure under OpenTofu safely using import blocks and generated config, or move resources between state files without recreating them.
-
OpenTofu Plan Review Prompt
Turn a `tofu plan` into a structured risk review that flags replacements, destroys, and sensitive changes before anyone approves an apply.
-
OpenTofu for_each & dynamic Block Patterns Prompt
Replace copy-pasted resources with clean for_each and dynamic patterns in OpenTofu, using stable keys so adding one item never churns the others.
-
OpenTofu Override & .tofu Files Prompt
Use OpenTofu override files and .tofu file extensions deliberately — for local dev overrides and OpenTofu-specific config — without hiding surprises from the next engineer.
More OpenTofu prompts & error guides
Browse every OpenTofu 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.