Full lesson
Explore the full explanation, examples, and visuals at your own pace.
main.tf after the rename
You renamed one resource, and Terraform now plans to destroy your production database. Look at the code. Only the label changed from db to main_db. The identifier is still prod-db, same engine, same size. Yet the plan says one to add, one to destroy. Why would a label matter?
resource "aws_db_instance" "main_db" { # was "db"
identifier = "prod-db"
engine = "postgres"
instance_class = "db.t3.micro"
}# aws_db_instance.db will be destroyed # aws_db_instance.main_db will be created Plan: 1 to add, 0 to change, 1 to destroy.
Before: address db maps to prod-db
Before the rename, the chain works. The code in main.tf declares aws_db_instance.db, and Terraform matches that address to a state entry. That entry holds the real ID of prod-db. So your code never points at the database directly. It finds prod-db only through the address recorded in state.
After rename: the address no longer matches
Now main.tf says main_db instead of db. When Terraform looks for that address in state, nothing matches, so the lookup fails at the state entry. The entry still tracks prod-db's ID, but no code claims it anymore. Because of that, prod-db is marked to destroy, even though you only changed a name.
How plan reads each address
Plan works like a diff, one address per row. The address db is in state, pointing at prod-db, but not in code, so plan destroys it. The address main_db is in code but not in state, so plan creates it. Terraform never compares attributes to decide these are the same database.
What applying that plan does to RDS
Applying that plan would delete prod-db, and the data goes with it, unless a final snapshot or deletion_protection steps in. Then Terraform creates an empty instance with a new endpoint, so every client breaks. Never apply a plan with unexpected destroys.
- prod-db deleted, data with it
- A new, empty instance created
- Endpoint changes for every client
Fix 1: a moved block (Terraform 1.1+)
Keep the resource block with its new name, main_db, and add a moved block beside it. It says the object once at aws_db_instance.db now lives at aws_db_instance.main_db. Run a plan, and it reports a move with zero to destroy, assuming nothing else is pending. Apply it to record the rename in state.
resource "aws_db_instance" "main_db" {
identifier = "prod-db"
# ...unchanged
}
moved {
from = aws_db_instance.db
to = aws_db_instance.main_db
}# aws_db_instance.db has moved to aws_db_instance.main_db Plan: 0 to add, 0 to change, 0 to destroy.
The moved block rewrites the state address
Here's what the moved block does. The state entry starts out labeled db, and the moved block rewrites that address to main_db. Now main.tf matches the state again. The entry still tracks the same ID, so prod-db is kept, not destroyed. AWS never even sees the rename.
Fix 2: move state directly from the CLI
The CLI route is terraform state mv, with the old address first and the new one second. It does the same state rewrite immediately, and it works on versions before 1.1. The output reports one object moved. Back up or lock remote state first, then confirm plan shows zero destroys.
terraform state mv aws_db_instance.db aws_db_instance.main_dbMove "aws_db_instance.db" to "aws_db_instance.main_db" Successfully moved 1 object(s).
Same result, different workflow
Both fixes give the same state rename. The moved block lives in code, so it's reviewed in a pull request and applies on the next apply, including other environments automatically. State mv is a quick CLI edit, but it skips review and you run it per state. Remove a moved block only after every state has applied it.
State ties each code address to a real resource ID.
State ties each code address to a real resource ID. Your code only reaches prod-db through the state entry, so a renamed address needs the state entry moved with it. Whenever an address changes, add a moved block, then check that the plan shows zero destroys before you apply.