Refactoring Terraform code looks harmless. Rename a resource. Move it into a module. Clean up the structure.
But Terraform does not track infrastructure by appearance. It tracks infrastructure by address identity stored in state.
When that address changes, Terraform no longer recognizes the object. The plan may interpret the change as destroy the old resource and create a new one, even though the real cloud object should stay the same.
This is where many production mistakes happen.
In this lesson, we examine how Terraform evaluates resource addresses inside state, and why simple code refactors can trigger unexpected replacements. Then we demonstrate the mechanism Terraform provides to safely preserve identity during refactors: the moved block.
Instead of Terraform guessing what changed, you explicitly declare the mapping between the old address and the new address. Terraform then rebinds the object during planning and preserves the infrastructure.
Understanding this mechanism turns refactoring from a risky operation into a controlled one.
The full lesson walks through the identity model Terraform uses and shows how to perform safe, state-aware refactors in real infrastructure code.
Watch the full video here.

