Refactoring with moved and removed Blocks
Renaming, reorganising and retiring resources changes how configuration maps to state. This lesson covers the moved and removed blocks that do this safely, and the older state commands they replace.
Learning outcomes
After completing this lesson, you will be able to:
- Explain why changing a resource address needs a moved block.
- Describe how removed blocks stop managing or destroy resources.
- Distinguish configuration blocks from terraform state mv and rm.
Why refactoring needs help
Terraform identifies objects by resource address. If you rename aws_instance.web to aws_instance.app, move it into a module, or switch it from count to for_each, the old address disappears from the configuration and a new one appears. Without further information, Terraform plans to destroy the object at the old address and create a new one at the new address, which for a database or a server means downtime and possibly data loss.
moved blocks
A moved block, available since Terraform 1.1, records that an object has a new address. Terraform updates state during the next apply, and the plan shows the move instead of a replacement.
# Renamed resource
moved {
from = aws_instance.web
to = aws_instance.app
}
# Moved into a module
moved {
from = aws_s3_bucket.logs
to = module.logging.aws_s3_bucket.this
}
# Switched from count to for_each
moved {
from = aws_subnet.app[0]
to = aws_subnet.app["eu-west-1a"]
}moved blocks can rename resources and modules, move objects into, out of and between modules, and add or change instance keys. They are especially important inside shared modules: when a module's author renames a resource, the moved block in the module lets every caller upgrade without replacement. Keep moved blocks for as long as any user might still upgrade from the old version; removing them too early brings back the destroy-and-create behaviour.
A refactor that would destroy data
removed blocks
A removed block, available since Terraform 1.7, tells Terraform that a resource has been removed from the configuration on purpose, and what to do with the real object.
removed {
from = aws_instance.legacy
lifecycle {
destroy = false
}
}With destroy = false, Terraform removes the object from state but leaves it running, so it can be managed by hand, by another configuration or imported elsewhere. With destroy = true, the object is destroyed, which is the same as deleting the block but documents the intent. The from address refers to a resource or module without instance keys, and the resource block itself must be deleted from the configuration.
The following diagram shows the blocks in each configuration when a resource moves between them.

removed blocks
State commands and blocks compared
Before these blocks existed, refactoring used terraform state mv and terraform state rm. They still work and change state immediately, which suits one-off repairs, but they bypass the plan and leave no record in code. The following comparison summarises the differences.
State command or block?
Splitting a configuration
A monolithic configuration manages a VPC and an application. The team wants a separate configuration to own the VPC, without recreating it.
The decisive requirement is to change ownership without destroying the object.
In the monolith, the team deletes the VPC block and adds removed { from = aws_vpc.main lifecycle { destroy = false } }; the plan shows the VPC leaving state without being destroyed. In the new network configuration, they write the VPC block and an import block with the VPC's ID; the plan shows the import. After both applies, the VPC is managed by exactly one configuration, and the application reads its ID from the network configuration's outputs.
Which block removes a resource from state without destroying it?
Show answer
A removed block with lifecycle { destroy = false }.
Exam Tips
Remember for the exam
- Changing an address without help means destroy and create.
- moved records new addresses: renames, module moves, count to for_each.
- removed with destroy = false forgets an object without destroying it.
- Blocks are planned and recorded; state mv and rm are immediate and unrecorded.