Terraform moved block: rename resources and refactor modules without destroying anything
A moved block (Terraform 1.1+) tells Terraform that a resource has a new address, so renaming it or moving it into a module updates state instead of destroying and recreating the object. Write moved { from = aws_instance.web to = aws_instance.app } (on separate lines), and the plan shows aws_instance.web has moved to aws_instance.app. It replaces terraform state mv for most refactoring.

Why renaming a resource destroys it
Terraform identifies each object in state by its address: resource type plus name, such as aws_instance.web. Change the name in the configuration to aws_instance.app and Terraform sees one resource that has disappeared and a new one that has appeared:
# aws_instance.app will be created
+ resource "aws_instance" "app" {
+ ami = "ami-0abc1234"
+ instance_type = "t3.micro"
...
}
# aws_instance.web will be destroyed
# (because aws_instance.web is not in configuration)
- resource "aws_instance" "web" {
...
}
Plan: 1 to add, 0 to change, 1 to destroy.For a server that means downtime; for a database or a bucket with data, it means loss. A moved block tells Terraform the two addresses are the same object.
How to rename a resource with a moved block
Rename the resource block, and add a moved block with the old and new address:
resource "aws_instance" "app" {
ami = "ami-0abc1234"
instance_type = "t3.micro"
}
moved {
from = aws_instance.web
to = aws_instance.app
}Now the plan changes nothing real:
$ terraform plan
aws_instance.app: Refreshing state... [id=i-0a1b2c3d4e5f60718]
Terraform will perform the following actions:
# aws_instance.web has moved to aws_instance.app
resource "aws_instance" "app" {
id = "i-0a1b2c3d4e5f60718"
# (31 unchanged attributes hidden)
}
Plan: 0 to add, 0 to change, 0 to destroy.has moved to is the line to look for. Applying updates the address in state; the instance keeps its ID. Before planning, Terraform checks whether state has an object at the from address and, if so, treats it as the to address. If you also changed an argument, the same plan shows that update on the moved resource. moved blocks need Terraform 1.1 or later.
Moving resources into and out of modules
Refactoring flat configuration into a module changes every address: aws_s3_bucket.logs becomes module.logs.aws_s3_bucket.this. Without a moved block, Terraform would destroy the bucket and create a new one inside the module. With one, in the calling (root) module:
module "logs" {
source = "./modules/bucket"
name = "acme-logs"
}
moved {
from = aws_s3_bucket.logs
to = module.logs.aws_s3_bucket.this
}The same works in reverse (from = module.logs.aws_s3_bucket.this, to = aws_s3_bucket.logs), and for whole module calls. Renaming a module call moves every resource in it:
moved {
from = module.storage
to = module.logs
}Terraform resolves addresses in a moved block relative to the module that contains it, and a module may only move its own resources and those of its child modules. So a move from the root into a module goes in the root module.
count to for_each: moving resource instances
moved also works on individual instances, which is how you switch from count to for_each without recreating everything:
resource "aws_instance" "web" {
for_each = toset(["blue", "green"])
ami = "ami-0abc1234"
instance_type = "t3.micro"
}
moved {
from = aws_instance.web[0]
to = aws_instance.web["blue"]
}
moved {
from = aws_instance.web[1]
to = aws_instance.web["green"]
}One case needs no block: when you add count to a resource that had none, Terraform treats the existing object as index [0], and when you remove count, it treats [0] as the single instance. Moving a single resource to a for_each key does need a block (from = aws_instance.web, to = aws_instance.web["blue"]). See for_each and count.
Keep your moved blocks
Once applied, a moved block does nothing in that workspace. It's tempting to delete it. Don't, if anyone else might apply the same code: another environment, a colleague's workspace, or every caller of a shared module. HashiCorp's refactoring guide calls removing a moved block a breaking change, because any state still at the old address would then plan a destroy instead of a move. It recommends keeping all historical moved blocks in modules you publish.
If you rename twice, chain the blocks so states at either old address end up in the right place:
moved {
from = aws_instance.a
to = aws_instance.b
}
moved {
from = aws_instance.b
to = aws_instance.c
}In a single root configuration you apply yourself, removing old moved blocks after every environment has applied them is safe, and keeps the code tidy.
moved block vs terraform state mv
Before Terraform 1.1, renames were done with terraform state mv:
$ terraform state mv aws_instance.web aws_instance.app
Move "aws_instance.web" to "aws_instance.app"
Successfully moved 1 object(s).It still works, and is handy for a one-off fix to a single state. But it edits state immediately, outside the normal workflow:
moved block | terraform state mv | |
|---|---|---|
| Reviewed in a plan | Yes, has moved to | No, changes state at once |
| In version control | Yes, reviewed in the pull request | No record in code |
| Applies to every workspace and module caller | Yes, automatically | Run it by hand in each state |
| Works with CI and HCP Terraform runs | Yes | Needs direct access to state |
| Timing | Same apply as the code change | Must be run in step with merging the rename |
Prefer moved blocks. The exam expects you to know both, and that moved is the recommended way to refactor.
Limitations of moved blocks
- Terraform 1.1+ only.
- Same resource type, normally. Each type has its own schema, so
aws_instance.webcan't simply move to a different type. Since Terraform 1.8, a provider can support moves between specific types (for example when it renames a resource type); the provider's documentation says which. Without that support, the plan fails. - Not into a data source. You can't move a managed
resourceinto adatablock. - Within one state only.
movedcan't move objects between separate configurations or states. To move a resource to another configuration, use aremovedblock withdestroy = falsein one and animportblock in the other (see terraform import). - Scope. A module can only refer to its own resources and its children's.
Common errors
- Moved object still exists: the
fromaddress is still declared in the configuration. Rename the resource block; don't keep both. - Plan still shows destroy and create: the
fromaddress doesn't match what's in state. Check withterraform state list, including any index or key ([0],["blue"]) and module path. - Ambiguous move statements: two blocks move different objects to the same address, or one object to two places. Each move needs one source and one destination.
Try it in a Lab
Planfile's Lab Rename without destroying is exactly this page: rename aws_instance.web to aws_instance.app, see the plan destroy and recreate the server, then add a moved block and apply until the same server has its new address. The Lab's simulator also has terraform state mv if you want to compare.
Questions
How do I rename a resource in Terraform without destroying it?
Rename the resource block and add a moved block with from = the old address and to = the new one. The plan shows 'has moved to' and changes nothing real.
What is the difference between a moved block and terraform state mv?
Both change an object's address in state. moved is declared in code, reviewed in a plan and applied in every workspace; terraform state mv edits one state immediately with no plan.
Can I delete moved blocks after applying?
In shared modules, keep them: removing one is a breaking change for anyone whose state still has the old address. In a configuration only you apply, you can remove them once every environment has applied them.
Can a moved block move a resource into a module?
Yes. Put the block in the calling module, for example from = aws_s3_bucket.logs and to = module.logs.aws_s3_bucket.this.
Can a moved block change the resource type?
Only if the provider supports that particular move, which Terraform allows from version 1.8. Otherwise the types must match.