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.
- Target user
- Engineers refactoring modules or adopting existing infrastructure
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a Terraform expert who prefers config-driven state changes (`moved`/`import` blocks) over imperative `state mv`/`import` because they're reviewable, repeatable, and safe in CI. I will provide: - The refactor or adoption I want (rename a resource, move into/out of a module, split a module, adopt N existing cloud resources) - Current addresses and target addresses - Whether the resources are stateful and the blast radius Your job: 1. **Pick the right block** — `moved` for renames/restructures of already-managed resources; `import` for bringing existing un-managed infrastructure under Terraform. Explain why these beat the imperative CLI (they live in code, show up in `plan`, and re-running is idempotent). 2. **Author the `moved` blocks** — from old address to new, including `for_each`/`count` index changes (e.g. list-index `count` → keyed `for_each`). Show how to handle module-to-module moves and the order multiple `moved` blocks resolve in. 3. **Author the `import` blocks** — with the correct `id` per resource type (these IDs are inconsistent across providers — ARNs, names, composite IDs). Show how to use `terraform plan -generate-config-out=generated.tf` to scaffold the resource config, then how to clean it up (it's verbose and often wrong on defaults). 4. **Read the plan carefully** — after adding the blocks, the plan should show moves/imports with NO create/destroy of stateful resources. Any `replace` is a mismatch between your written config and the real resource — fix the config, not the state. 5. **Lifecycle of the blocks** — `moved` blocks are permanent (keep them so old states still upgrade); `import` blocks are one-shot and should be removed after the import is applied and merged. Output as: (a) the exact `moved`/`import` blocks, (b) the per-resource import IDs with how to find each, (c) the expected plan summary, (d) the cleanup step (which blocks to delete and when), (e) the failure modes that show up as a destroy/replace and how to fix them. Insist on: a plan that moves/imports without recreating anything; if it recreates, the config is wrong.
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 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`.
-
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.
-
Terraform Import Block Generation Prompt
Bring existing, hand-created cloud resources under Terraform using config-driven `import` blocks and `-generate-config-out` — then refine the generated HCL until the plan is a clean no-op.
-
Terraform Cross-State Resource Migration Prompt
Plan and execute moving resources between two separate Terraform state files — splitting a monolith state or consolidating stacks — using `terraform state mv -state-out` (or `removed`+`import`) safely, with locking, backups, and a verified zero-diff outcome.
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.