terraform-refactor-with-moved
Skill0 starsChanged 7 months agoMITView on GitHub
Safely refactor Terraform code (rename a resource, restructure a module, switch from count to for_each) using moved blocks. Avoids destroy+create cycles that would otherwise drop and recreate live infrastructure.
In this repository3 other files it gives its agents.
The file
We split this skill into the rules it gives an agent
9 rules. About 0 are common, 2 are distinctive, and 7 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
7
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 10
When changing the address of an existing Terraform-managed resource - by renaming, moving between modules, or switching from count to …
- Very specific: line 16
Run terraform state list to see the current state addresses:
- Very specific: line 85
Not a destroy + create. If the plan still shows destroy + create, the moved block does not match - check addresses carefully.
- Very specific: line 99
Remove the moved blocks from HCL.
- Distinctive: line 106
If multiple environments use the same module:
- Very specific: line 111
After every environment is on the new tag, you can delete the moved blocks and tag a follow-up version.
- Very specific: line 128
Deleting
movedblocks too early: if you delete the block before all environments have applied, the un-applied environments will destroy + … - Distinctive: line 132
When you want to stop managing a resource but keep it alive (e.g., handing ownership to another team):
- Very specific: line 161
After apply: remove the import block in a follow-up PR.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Refactor with moved Blocks
When to Use
When changing the address of an existing Terraform-managed resource - by renaming, moving between modules, or switching from count to for_each. Without a moved block, Terraform treats the new address as a new resource and destroys + recreates the old one.
Instructions
Step 1: Identify the affected addresses
Run terraform state list to see the current state addresses:
aws_iam_role.application
aws_iam_role_policy.application_s3
module.vpc.aws_subnet.private[0]
module.vpc.aws_subnet.private[1]Step 2: Write the moved block in HCL
For a simple rename:
moved {
from = aws_iam_role.application
to = aws_iam_role.app
}For moving between modules:
moved {
from = aws_iam_role.app
to = module.iam.aws_iam_role.app
}For switching from count to for_each:
# Before (count-keyed)
resource "aws_subnet" "private" {
count = length(var.azs)
availability_zone = var.azs[count.index]
...
}
# After (for_each-keyed)
resource "aws_subnet" "private" {
for_each = toset(var.azs)
availability_zone = each.value
...
}
# moved block, one per element
moved {
from = aws_subnet.private[0]
to = aws_subnet.private["eu-central-1a"]
}
moved {
from = aws_subnet.private[1]
to = aws_subnet.private["eu-central-1b"]
}Step 3: Plan
terraform planYou should see:
# aws_iam_role.app will be moved from aws_iam_role.applicationNot a destroy + create. If the plan still shows destroy + create, the moved block does not match - check addresses carefully.
Step 4: Apply
terraform applyThe state file is updated. No infrastructure changes.
Step 5: Clean up
moved blocks live for one release cycle. After every consumer environment has applied the migrated configuration:
- Remove the
movedblocks from HCL. - Tag a new module version.
Leaving stale moved blocks indefinitely is harmless but adds noise.
Cross-environment migration
If multiple environments use the same module:
- Tag a new module version that includes both the resource rename and the
movedblock. - Bump each environment's
module "x" { source = "...?ref=vN" }pin to the new version. - Plan + apply each environment. The state file gets migrated.
- After every environment is on the new tag, you can delete the
movedblocks and tag a follow-up version.
When you cannot use moved
moved cannot migrate resources across:
- Different backends (different state files).
- Different cloud providers.
- Different resource types (
aws_instance->aws_ec2_fleet).
For these, use removed { from = ... lifecycle { destroy = false } } to drop the resource from state without destroying it, then re-adopt at the new address with an import block.
Common mistakes
- Forgetting the
movedblock: rename without one and Terraform plans a destroy + create. Always plan before applying. - Wrong address syntax:
moved { from = "aws_iam_role.x" to = "aws_iam_role.y" }(strings) is wrong. The addresses are bare HCL identifiers, not quoted strings. - Mixing
movedwith other changes: a single PR that both moves resources AND changes their arguments is hard to review. Split: PR 1 moves, PR 2 changes args. - Deleting
movedblocks too early: if you delete the block before all environments have applied, the un-applied environments will destroy + create on next apply.
removed blocks for retiring resources without destroy
When you want to stop managing a resource but keep it alive (e.g., handing ownership to another team):
removed {
from = aws_iam_role.legacy
lifecycle {
destroy = false
}
}Terraform drops the resource from state on the next apply but does not destroy it. Useful when transferring ownership; rare otherwise.
import blocks for adopting existing infrastructure
Pair with removed from the source codebase to do a clean handoff:
# in the new codebase
import {
to = aws_iam_role.app
id = "my-iam-role-id"
}
resource "aws_iam_role" "app" {
# ... config that matches the existing role
}After apply: remove the import block in a follow-up PR.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

