Refactoring State with moved and removed
Safely rename and restructure resources without destroying infrastructure by using state manipulation commands and the declarative moved and removed blocks.
Refactoring State with moved and removed is a free DevOps Bootcamp lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Renaming Problem
When you rename a resource in your config, Terraform sees the old address gone and a new one appearing. By default it will destroy and recreate the resource, which can mean downtime or data loss.
State refactoring tools let you tell Terraform that a resource simply moved.
Inspecting State
Start by listing what is tracked in state. The terraform state list command shows every managed resource address.
terraform state listImperative state mv
The classic approach is terraform state mv, which rewrites a resource's address in state. Here we rename an instance from aws_instance.web to aws_instance.app.
terraform state mv aws_instance.web aws_instance.appThe Downside of state mv
state mv is a manual, one-time CLI action. It is not recorded in code, so teammates running the same change on their machines must repeat it. This is error-prone in collaborative workflows.
Declarative moved Blocks
The moved block records a rename in your configuration. Terraform applies it automatically for everyone during the next plan, then you can remove the block later.
moved {
from = aws_instance.web
to = aws_instance.app
}Moving Into a Module
moved blocks also handle wrapping resources in a module. The target address includes the module path.
moved {
from = aws_instance.app
to = module.compute.aws_instance.app
}Verifying with a Plan
After adding a moved block, run terraform plan. A correct move shows no destroy or create, only an address change. If you still see a replacement, the block is wrong.
terraform planRemoving from State
Sometimes you want Terraform to stop managing a resource without destroying the real infrastructure. The imperative command is terraform state rm.
terraform state rm aws_instance.legacyDeclarative removed Blocks
Newer Terraform supports a removed block. With lifecycle { destroy = false } it forgets the resource from state but leaves the real infrastructure untouched.
removed {
from = aws_instance.legacy
lifecycle {
destroy = false
}
}Splitting State Files
For large projects, terraform state mv -state-out can move resources between separate state files, useful when splitting a monolith into independently applied stacks.
terraform state mv -state-out=network.tfstate \
aws_vpc.main aws_vpc.mainBackups and Safety
Terraform writes a .backup file before mutating state. Still, with remote backends always confirm state is not locked by a teammate before manual operations to avoid corruption.
Quick Check
Test your refactoring knowledge.
Recap: Safe State Refactoring
You can now restructure infrastructure without destroying it:
state mv/state rmfor imperative changes.movedblocks for declarative renames.removedblocks to forget without destroying.- Always verify with a plan that shows no replacement.
Frequently asked questions
Is the “Refactoring State with moved and removed” lesson free?
Yes — the full text of “Refactoring State with moved and removed” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Refactoring State with moved and removed”?
Safely rename and restructure resources without destroying infrastructure by using state manipulation commands and the declarative moved and removed blocks. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Refactoring State with moved and removed” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Remote State Backends
- State Locking and Consistency
- Importing Existing Resources
- Refactoring State with moved and removed