Moved blocks and address refactoring
A new address does not need a new object
Imagine renaming a folder in an infrastructure project without replacing the service it describes. Terraform state associates each managed object with an address. If you change a resource label without recording the move, Terraform may see the old address as no longer declared and the new address as an object to create. A moved block records the relationship between the two addresses. It is not a request to rename the remote object. Our local-only examples use the built-in terraform_data resource; the provisional CLI baseline remains 1.14.8 and no examples have been executed.
Rename a resource safely
Start with this complete resource in a fresh disposable local root.
resource "terraform_data" "catalog" { input = "catalog-v1"}After an approved baseline apply in that root, replace the original resource block with this configuration; do not keep both declarations.
resource "terraform_data" "inventory" { input = "catalog-v1"}
moved { from = terraform_data.catalog to = terraform_data.inventory}The unchanged input is deliberate. The expected plan records the move from catalog to inventory with zero creates, updates and destroys. A plan alone does not persist the new binding; applying the reviewed change does. Compare the ID before and after and update references to the old address. If the plan proposes replacement, stop and investigate rather than assuming the moved block guarantees safety.
Change numeric indexes to stable keys
A count-based resource identifies instances by integer index. This initial configuration gives index 0 the value east and index 1 west.
resource "terraform_data" "record" { count = 2 input = ["east", "west"][count.index]}After recording the actual mapping in the lab state, replace the configuration with this version.
resource "terraform_data" "record" { for_each = toset(["east", "west"]) input = each.key}
moved { from = terraform_data.record[0] to = terraform_data.record["east"]}
moved { from = terraform_data.record[1] to = terraform_data.record["west"]}Each old address maps to exactly one new address. The input values remain the same. Never infer the mapping from a newly sorted list: if the previous state associated west with index 0, these statements would map the wrong object. Check existing state and reject duplicate targets, unintended updates or deletions. The example is independent of the rename example.
Move a root resource into a child module
Starting from the original catalog resource, the following root configuration replaces the old declaration. The module source is a local directory.
module "catalog" { source = "./modules/catalog"}
moved { from = terraform_data.catalog to = module.catalog.terraform_data.record}In modules/catalog/main.tf, define the resource:
resource "terraform_data" "record" { input = "catalog-v1"}The expected destination is module.catalog.terraform_data.record. A move is resolved relative to the module declaring it; module boundaries do not imply separate state. Keep module sources and interfaces compatible, update references and review the plan. Across resource types, a move needs explicit provider support and compatible state transformation; same-type moves are the ordinary case. Do not treat moved as a general conversion utility.
Keep a migration history
If a published module changes an address twice, preserve the chain so consumers can upgrade from either earlier version. In this example the current declaration must be terraform_data.index with the original input.
moved { from = terraform_data.catalog to = terraform_data.inventory}
moved { from = terraform_data.inventory to = terraform_data.index}Removing earlier mappings can break upgrades by users who have not yet applied an intermediate release. A historical source absent from state is normally ignored. Avoid conflicting destinations or chains that form a cycle.
Use state mv only when justified
The CLI can also change the binding directly. This alternative uses the same local rename scenario, with the destination resource declared and no moved block needed for that particular operation.
terraform state mv -dry-run terraform_data.catalog terraform_data.inventory# Only after approval and matching configuration change:terraform state mv terraform_data.catalog terraform_data.inventoryThe dry run identifies matching objects; it is not a full infrastructure plan. Actual state mv changes state immediately. Coordinate exclusive change windows, protect backups, retain locking, and review a fresh plan afterwards. Do not run these commands against an existing project. Prefer declarative moves for reusable module upgrade paths. Practice by mapping the synthetic cases before any local execution.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Moved blocks and address refactoring — knowledge check
Review refactoring, state identity and ownership safety.
What does a moved block primarily change?
Which mapping preserves the example east instance?
When is the new binding persisted in the declarative workflow?
Why retain historical moved blocks in a published module?
A move plan also proposes an unexpected replacement. What should you do?