Moved blocks and safe refactoring
Rename a resource, move it into a module or switch from count to for_each without a destroy: the moved block, with a demo of the plan before and after.
Moved blocks and safe refactoring
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Terraform tracks objects by address. Rename a resource block and the old address vanishes and a new one appears, which a plan reads as destroy and create.
- A `moved { from = ..., to = ... }` block tells the planner the two addresses are the same object, so the plan shows a move and no destruction. It works for renames, moves into or out of modules, and `count` to `for_each`.
- Unlike `terraform state mv`, a `moved` block is code. It is reviewed in a pull request and fixes every copy of state, in every environment, the next time each one is planned.
- Terraform 1.8 and OpenTofu 1.10 allow a `moved` block between two resource types, but only when the provider says it can convert one into the other.
- Read the plan. A correct refactor shows `has moved to` lines and no creates or destroys. If you see a destroy, the `moved` block is wrong or missing.
The rename that destroys a database
Someone tidies a module and renames aws_db_instance.main to aws_db_instance.primary. The diff is one line, the review is quick, and the plan says 1 to add, 1 to destroy. Terraform did exactly what it was told. State holds an object at aws_db_instance.main. The code no longer declares that address and does declare a new one. The names are labels and Terraform can’t know the two refer to one database.
Before the moved block existed, the fix was terraform state mv run by hand in every environment. That works, and the previous lesson covers it, but it is a manual step nobody reviews and it is easy to run in prod and forget in staging.
The moved block
The fix is one block in the same pull request as the rename. Terraform 1.1 introduced it. This is written from the docs, and I have not run it.
resource "aws_db_instance" "primary" {
# unchanged arguments
}
moved {
from = aws_db_instance.main
to = aws_db_instance.primary
}
On the next plan, Terraform finds aws_db_instance.main in state, sees the block, and treats the object as the one now at aws_db_instance.primary. The plan reports a move. After apply, the state records the new address and the block has done its job.
Seeing the difference
I can’t run Terraform on this machine. To show the idea I wrote a toy planner in Python, which compares sets of resource addresses and nothing else. It is a stand-in. The labels + create and - destroy are mine, not Terraform’s output. The first case is a rename:
Renamed assets -> notes in code, nothing else:
+ create aws_s3_bucket.notes
- destroy aws_s3_bucket.assets
Same, with moved { from = assets, to = notes }:
# aws_s3_bucket.assets has moved to aws_s3_bucket.notes
The second is the case that bites more often. count addresses instances by position, for_each by key, and switching changes every address:
count -> for_each, no moved blocks:
+ create aws_subnet.private["a"]
+ create aws_subnet.private["b"]
- destroy aws_subnet.private[0]
- destroy aws_subnet.private[1]
with moved blocks:
# aws_subnet.private[0] has moved to aws_subnet.private["a"]
# aws_subnet.private[1] has moved to aws_subnet.private["b"]
Both subnets would have been replaced, taking down anything attached. With two moved blocks, nothing is. The count and for_each lesson explains why the key matters, and the toy planner is the same idea.
What you can move
The docs and changelogs give the supported cases.
- Rename a resource or a module call.
fromandtoare addresses written relative to the module that holds the block. - Move into or out of a module. The address includes the module path, as in
to = module.network.aws_vpc.main. This is the usual reason: you extracted resources into a module, which the modules group does. - Add or remove
countorfor_each. Move a single address to[0]or to a key, or the other way around. - Change resource type, from Terraform 1.8. The
movedblock between two different types works only if the target provider declares it can convert state from the source type. OpenTofu added the same in 1.10. Check the provider’s docs for the pairs it supports. It does not mean any two resources can be swapped.
You cannot move between separate state files with a moved block. Splitting one state into two is a different job, covered in splitting state.
Habits
Read the plan, and look for the words has moved to. A correct refactor shows only moves and, at most, in-place changes you meant to make. A destroy next to a create of the same kind means a missing or mistyped address.
Keep the block for a while. In your own root module, once every environment has applied, it can go. In a module other people use, keep it. Someone on the old version has state at the old address, and your moved block is what moves it when they upgrade. Deleting it turns their upgrade into a destroy and create.
A moved block is for objects that are staying, not for ones you want to forget. To stop managing something, use a removed block with lifecycle { destroy = false } (Terraform 1.7, OpenTofu 1.7), which the docs say removes it from state without destroying it.
About the video
The embed is Ned Bellavance’s “Using Moved Blocks in Terraform”, published December 2021 for Terraform 1.1, 17 minutes. His description says it covers refactoring an existing deployment into modules, renaming resources and adding looping without disrupting the deployment. That is this lesson’s core. It is older than the type-changing moves of 1.8 and the removed block of 1.7, and it has no chapters. I added those in the text above. The Merge Ready video in deeper is a 4 minute 2025 demo that, by its description, mentions cross-module moves, import and removed. English was judged from the title, description and the channel’s English auto-caption track.
Where this fits
moved handles objects Terraform already tracks. The opposite problem, a real object Terraform has never heard of, is importing existing infrastructure.
Check yourself
You rename `aws_s3_bucket.assets` to `aws_s3_bucket.notes` in the code and change nothing else. What does the plan show, and why?
A destroy of `assets` and a create of `notes`, because state holds the old address and the code now only declares the new one. Terraform has no way to know they are the same bucket.
You switch `aws_subnet.private` from `count` to `for_each` over `["a", "b"]`. What `moved` blocks do you need?
One per existing instance: from `aws_subnet.private[0]` to `aws_subnet.private["a"]`, and from `[1]` to `["b"]`. Without them the indexes change and both subnets are replaced.
Why would you leave `moved` blocks in a module you publish?
People using older versions of your module have state at the old addresses. The `moved` block migrates them when they upgrade. If you delete it, their next plan sees destroy and create.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.