agentleFS
RoninForgeRoninForge/roninforge-terraform/skills/terraform-refactor-with-moved/SKILL.md

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.

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
  1. Very specific:

    When changing the address of an existing Terraform-managed resource - by renaming, moving between modules, or switching from count to …

    line 10
  2. Very specific:

    Run terraform state list to see the current state addresses:

    line 16
  3. Very specific:

    Not a destroy + create. If the plan still shows destroy + create, the moved block does not match - check addresses carefully.

    line 85
  4. Very specific:

    Remove the moved blocks from HCL.

    line 99
  5. Distinctive:

    If multiple environments use the same module:

    line 106
  6. Very specific:

    After every environment is on the new tag, you can delete the moved blocks and tag a follow-up version.

    line 111
  7. Very specific:

    Deleting moved blocks too early: if you delete the block before all environments have applied, the un-applied environments will destroy + …

    line 128
  8. Distinctive:

    When you want to stop managing a resource but keep it alive (e.g., handing ownership to another team):

    line 132
  9. Very specific:

    After apply: remove the import block in a follow-up PR.

    line 161

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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 plan

You should see:

# aws_iam_role.app will be moved from aws_iam_role.application

Not a destroy + create. If the plan still shows destroy + create, the moved block does not match - check addresses carefully.

Step 4: Apply

terraform apply

The 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 moved blocks 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:

  1. Tag a new module version that includes both the resource rename and the moved block.
  2. Bump each environment's module "x" { source = "...?ref=vN" } pin to the new version.
  3. Plan + apply each environment. The state file gets migrated.
  4. After every environment is on the new tag, you can delete the moved blocks 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 moved block: 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 moved with 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 moved blocks 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.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.