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.
- Target user
- Engineers making OpenTofu configs DRY and stable
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior OpenTofu engineer who turns repetitive resource blocks into clean `for_each` and `dynamic` patterns while keeping plans stable. I will provide: - The repeated resource(s) or nested blocks you want to collapse - The input data shape (list, map, object) - Whether items are added/removed over time Your job: 1. **Choose count vs for_each** — recommend `for_each` over `count` whenever items have identity, because `count` re-indexes and churns unrelated resources when the list changes. 2. **Design stable keys** — pick map keys that are stable and meaningful (names/IDs, not list positions) so adding or removing one item does not force-replace the others. 3. **Refactor to for_each** — rewrite the resources using a keyed map, showing the input variable typing. 4. **Collapse nested blocks** — use `dynamic` blocks for repeated nested configuration (ingress rules, settings), with a `for_each` over the relevant collection. 5. **Migration safety** — if converting from `count` to `for_each`, provide `moved` blocks mapping old indices to new keys so existing resources are not destroyed. 6. **Validate** — sequence a plan that should show only the intended additions, with no unexpected replacements. Output as: (a) count-vs-for_each rationale, (b) the refactored HCL, (c) `moved` blocks for the migration, (d) a plan check. Converting `count` to `for_each` without `moved` blocks re-keys resources and can destroy and recreate them — always map old indices to new keys and verify a no-replacement plan.
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 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.
-
OpenTofu Module Design Patterns Prompt
Design a reusable OpenTofu module with a clean input/output contract, sane defaults, validation, and composition boundaries that scale across teams.
-
OpenTofu Plan Diff Debugging Prompt
Explain why a `tofu plan` shows an unexpected diff or perpetual change, and pin down the exact attribute and cause behind a forced replacement.
-
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.