Skip to content

Moving or renaming a unit

The backend key is ${path_relative_to_include()}/terraform.tfstate, so a unit's path under live/ is its state key. Move or rename the folder and the unit points at a new, empty state file. Terraform then plans to create everything the unit already manages, and to destroy nothing, because the old state is simply orphaned. See Live tree.

Move the state before the merge, not after

A plan that shows a full create for a unit you only renamed is the signal that state has not moved yet. Do not apply it.

Procedure:

  1. Open the PR with the move. Let it plan. Confirm the plan shows the new path creating everything and, if the old path is still discovered, destroying it. That is the orphan, made visible.
  2. Copy the state blob to the new key, before you apply or merge:
az storage blob copy start \
  --auth-mode login \
  --account-name <state-account> \
  --destination-container tfstate \
  --destination-blob landingzones/corp/corp-demo-dev/westeurope/network/terraform.tfstate \
  --source-container tfstate \
  --source-blob landingzones/corp/corp-demo-dev/westeurope/net/terraform.tfstate

Copy, do not move. The old blob stays as the rollback until you are done. 3. Comment tfpr plan again. The unit must come back with no changes. That is the verification: same resources, new key. 4. Apply if there are real changes, then merge. 5. Delete the old blob once the new key has applied cleanly at least once. Blob versioning, 30-day soft delete and point-in-time restore are on for the state account, so a mistaken delete is recoverable within that window.

terragrunt run -- state mv moves resources inside one state file. It does not change the backend key, so it is not the tool for a folder move. Reach for it only when you are also splitting or merging units, and do it as a separate, deliberate step.