Home›Guides›Refactor with moved blocks

Rename and refactor resources without destroying them: moved blocks

Renaming a resource or moving it into a module changes its address. Without help, the plan proposes to destroy the old object and create a new one. A moved block prevents that.

The block

moved {
  from = aws_instance.a
  to   = aws_instance.b
}

If state holds an object at the old address, OpenTofu renames it to the new address before planning. The plan then treats it as if it had always lived there — nothing is destroyed.

Moving into a module

moved {
  from = aws_instance.web
  to   = module.web.aws_instance.this
}

Should you delete old moved blocks?

The OpenTofu docs call removing a moved block “generally a breaking change”: anyone still on the old address would get a plan that deletes the object. Keep them, especially in shared modules. In a private module, remove them only when every user has applied the new version.

Always check the plan after adding a moved block: it should show the move and no destroy.

Examples use OpenTofu (tofu); in Terraform the commands start with terraform. Check the documentation for your version before relying on a feature.

Big refactoring ahead?We do this as a fixed-price job.IaC Health Check →

Book a 30-minute call

Tell us what you are trying to fix. No preparation needed.

Book a 30-minute call