Terraform Refactors Without Breaking Infrastructure

Terraform identifies infrastructure by state address, not by the cloud resource name. This detail becomes critical when refactoring code.

If you rename a resource block, move a resource into a module, or change a for_each key, Terraform does not see a refactor. It sees a different object. The default plan becomes destroy and recreate, even when the underlying AWS resource is identical.

The moved block explicitly maps an old resource address to a new one. During the planning phase, Terraform rewrites the state mapping so the existing cloud resource remains intact. The infrastructure stays in place while the configuration evolves.

This lesson demonstrates how moved blocks protect infrastructure during common refactors such as:

  • Renaming a resource block
  • Moving a resource into a module
  • Changing for_each keys

Understanding this mechanism turns refactoring from a risky operation into a controlled state migration.

Watch the full lesson here: