Refactor Terraform State Without Destroying Resources Using moved Blocks
Use Terraform moved blocks to rename or relocate resources in state without triggering destroy-and-recreate.
21 Mar 2026, 17:58 UTC

Renaming a Terraform resource should not mean rebuilding it
You decide to tidy up configuration: maybe you rename aws_instance.web to aws_instance.app_server, or move a resource into a child module. On the next terraform plan, Terraform sees the old address as gone and the new address as missing. The default plan is to destroy the existing virtual machine and create a replacement. For a database or a production endpoint, that is often unacceptable.
The useful takeaway: Terraform 1.1 and later support moved blocks. These blocks tell Terraform to adopt an existing state object under a new configuration address. Remote infrastructure stays running; only the state-to-address mapping changes.
What a moved block does
A moved block is a state refactoring directive, not a configuration migration tool. It does not rewrite provider arguments, update resource properties, or migrate data. It maps an old state address to a new configuration address so Terraform treats them as the same object.
Syntax uses from for the current state address and to for the new configuration address. One block per resource or module instance is typical. Terraform ignores moved blocks that no longer apply, but stale blocks should be removed after a successful apply to keep configuration clean.
Worked example: move a resource into a module
Suppose your configuration currently defines a resource in the root module:
resource "aws_instance" "web" {
ami = "ami-0c1234567890abcdef"
instance_type = "t3.micro"
}
You refactor it into a module named network:
module "network" {
source = "./modules/network"
}
Inside modules/network/main.tf:
resource "aws_instance" "web" {
ami = "ami-0c1234567890abcdef"
instance_type = "t3.micro"
}
Before applying, add a moved block in the root module so Terraform knows the state object at aws_instance.web now lives at module.network.aws_instance.web:
moved {
from = aws_instance.web
to = module.network.aws_instance.web
}
Run these commands from the directory containing your root module and backend configuration. You need permissions to read and write the state file in your backend, such as S3 bucket write access or Terraform Cloud state update rights.
terraform version
terraform plan
Check that terraform plan reports no create, destroy, or update operations for aws_instance.web. The EC2 instance itself should not appear in the plan as a replacement.
If the plan looks correct, apply the change:
terraform apply
After apply, verify the state mapping:
terraform state list
The old address aws_instance.web should be absent, and module.network.aws_instance.web should appear. The running EC2 instance retains its ID and IP address because Terraform only updated its state record.
Checks, risks, and rollback
- Confirm the CLI version is 1.1 or later. Older versions cannot parse
movedblocks and will return a parse error. - Back up state before applying. With a local backend, copy
terraform.tfstate. With remote backends, use the backend's snapshot or export feature if available. - If the remote object was already deleted or imported into another state, applying the
movedblock can fail or leave an orphaned resource. Inspectterraform plancarefully before applying. - State locking still applies. If another run holds the lock, your plan or apply will wait or fail.
Because terraform apply updates state, a rollback means restoring the previous state file from backup and removing or reversing the moved blocks, then running terraform plan again. Test in a non-production workspace first.
Limitations
moved blocks do not change the remote resource or its provider schema. If the new module requires different arguments, you must update those separately, and Terraform may still plan updates after the address move. The feature supports resources, modules, and instances using for_each or count, but each address must be valid in the current configuration.
Closing
Use moved blocks when renaming resources, reorganizing modules, or correcting state addresses without rebuilding infrastructure. Always run terraform plan first, confirm no replacements are planned, and keep a state backup before applying. Once the apply succeeds, remove the moved blocks so your configuration stays readable.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.