Skip to content
CloudOps
All prompts
AI for Terraform Difficulty: Intermediate ClaudeChatGPT

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.
Newsletter

Get weekly AI workflows for DevOps engineers

Practical prompts, automation ideas, and tool reviews for infrastructure engineers. One email per week. No spam.