terraform-refactor-identity-pitfalls
Step 6 of the pipeline (WORKFLOW.md), for the address of a resource. The three sections share one premise:
Terraform identifies a resource by its address, so any edit that changes an address is a destroy and a create
unless something says otherwise, and the plan that shows it looks like a small diff. State, backends and secrets
are in terraform-state-and-secrets-pitfalls; the pipeline that runs the plan is in terraform-ci-delivery-pitfalls.
Examples are Terraform syntax; OpenTofu floors are given only where the sources state them.
When
- A list or map that drives
count or for_each gains, loses or reorders an element.
- A resource or module is renamed, moved into a module, or switched between
count and for_each.
- An existing resource is adopted into Terraform, or a resource must leave management without being destroyed.
- A provider or runtime version moves, or a feature is used that depends on a recent release.
- A module is tested with
terraform test and mocked providers.
Steps
Read only the section the task meets. They are independent.
| § |
Covers |
Read it when |
File |
| 1 |
Address identity: count versus for_each, keys known at plan time, targeted destroy |
a collection is iterated or an element is added or removed |
01-identity.md |
| 2 |
Moves, imports and removals: moved, import, removed, provider removal |
an address changes, or a resource is adopted or released |
02-moves-imports-removals.md |
| 3 |
Versions and tests: the floor table, pinning, the lock file, mocked tests |
a version is chosen or bumped, or a module test is written |
03-versions-and-tests.md |
Output / checkpoint
Each rule was exercised, not read off the file: a plan was read after the edit and showed moves, not replace
(§2), an adopted resource produced an empty plan (§2.2), an element was removed from the middle of a collection in
a scratch configuration and only that element changed (§1), and the target runtime was stated before any feature in
the floor table was used (§3.1). A configuration that was only formatted and validated is not verified.
Guardrails
- Never iterate a long-lived collection with
count (§1.1).
- Never derive
for_each keys from attributes of another resource that are known only after apply (§1.2).
- Never rename, re-key or move a resource without a
moved block in the same change (§2.1).
- Never apply an adoption whose plan is not empty (§2.2).
- Never remove a provider block while resources of that provider are still in state (§2.3).
- Never use a version-bound feature without stating the target runtime and a fallback below its floor (§3.1).
- Never ship a runtime or provider bump together with a functional change (§3.2).
- Never count a mocked-provider test as integration coverage (§3.3).
- This block states Terraform behaviour as of the repositories read on the date in
references/origin.md. The official Terraform and OpenTofu documentation was not read.
Nothing was run while writing it.
Origin
Rewritten from terrashark (MIT, identity churn and migration references), terraform-skill by Babenko (Apache-2.0,
code patterns, state management and testing references) and the Terramate agent skills (MIT, provider version
constraints rule), read 2026-10-15. 🟡: never run by us; open points are in
references/origin.md.
1---2name: terraform-refactor-identity-pitfalls3description: "Use when Terraform or OpenTofu resources are renamed, re-keyed, moved, adopted or dropped, or when a module is refactored: count versus for_each identity, for_each keys that must be known at plan time, moved blocks before the first apply, declarative import and removed blocks, targeted destroy, provider and runtime pinning with the lock file, a version floor table for newer features, and what terraform test with mocks does and does not prove."4metadata:5 source: https://skillmd.com/skills/techmefr/terraform-refactor-identity-pitfalls6---78# terraform-refactor-identity-pitfalls910Step 6 of the pipeline (`WORKFLOW.md`), for the address of a resource. The three sections share one premise:11**Terraform identifies a resource by its address, so any edit that changes an address is a destroy and a create12unless something says otherwise, and the plan that shows it looks like a small diff**. State, backends and secrets13are in `terraform-state-and-secrets-pitfalls`; the pipeline that runs the plan is in `terraform-ci-delivery-pitfalls`.14Examples are Terraform syntax; OpenTofu floors are given only where the sources state them.1516## When17- A list or map that drives `count` or `for_each` gains, loses or reorders an element.18- A resource or module is renamed, moved into a module, or switched between `count` and `for_each`.19- An existing resource is adopted into Terraform, or a resource must leave management without being destroyed.20- A provider or runtime version moves, or a feature is used that depends on a recent release.21- A module is tested with `terraform test` and mocked providers.2223## Steps2425**Read only the section the task meets.** They are independent.2627| § | Covers | Read it when | File |28|---|---|---|---|29| 1 | Address identity: count versus for_each, keys known at plan time, targeted destroy | a collection is iterated or an element is added or removed | [`01-identity.md`](./references/01-identity.md) |30| 2 | Moves, imports and removals: moved, import, removed, provider removal | an address changes, or a resource is adopted or released | [`02-moves-imports-removals.md`](./references/02-moves-imports-removals.md) |31| 3 | Versions and tests: the floor table, pinning, the lock file, mocked tests | a version is chosen or bumped, or a module test is written | [`03-versions-and-tests.md`](./references/03-versions-and-tests.md) |3233## Output / checkpoint34Each rule was exercised, not read off the file: a plan was read after the edit and showed moves, not replace35(§2), an adopted resource produced an empty plan (§2.2), an element was removed from the middle of a collection in36a scratch configuration and only that element changed (§1), and the target runtime was stated before any feature in37the floor table was used (§3.1). A configuration that was only formatted and validated is not verified.3839## Guardrails40- Never iterate a long-lived collection with `count` (§1.1).41- Never derive `for_each` keys from attributes of another resource that are known only after apply (§1.2).42- Never rename, re-key or move a resource without a `moved` block in the same change (§2.1).43- Never apply an adoption whose plan is not empty (§2.2).44- Never remove a provider block while resources of that provider are still in state (§2.3).45- Never use a version-bound feature without stating the target runtime and a fallback below its floor (§3.1).46- Never ship a runtime or provider bump together with a functional change (§3.2).47- Never count a mocked-provider test as integration coverage (§3.3).48- This block states Terraform behaviour as of the repositories read on the date in49 [`references/origin.md`](./references/origin.md). The official Terraform and OpenTofu documentation was not read.50 Nothing was run while writing it.5152## Origin53Rewritten from terrashark (MIT, identity churn and migration references), terraform-skill by Babenko (Apache-2.0,54code patterns, state management and testing references) and the Terramate agent skills (MIT, provider version55constraints rule), read 2026-10-15. 🟡: never run by us; open points are in56[`references/origin.md`](./references/origin.md).