$ cd ../writing

2026-11-20 · 5 min

A rename is a destroy. Three places a new name breaks Terraform.

A list of repositories drives a for_each, and each repository gets an access group keyed by its name. Rename one repository and the next plan deletes its group and creates a new one. A moved block fixes the address, ignore_changes fixes the attribute that forces a replacement, and anything that finds the object by name has to switch to its ID first.

A list of repositories, read from GitHub on every run, drives a for_each. Every repository gets an Entra ID access group, keyed by the repository name, and the group’s memberships are keyed the same way. Then someone renames a repository on GitHub. The next scheduled plan wants to delete its group and create a new one.

This is part 2 of a series on Terraform state surgery, which means changing what Terraform manages without destroying anything live, with the plan as the proof. Part 1 took one item out of a for_each. The plans below were reproduced with terraform_data on Terraform 1.8 and 1.17.

The setup

locals {
  repos = jsondecode(
    file("repos.json")
  ).repositories
}

resource "azuread_group" "repo" {
  for_each         = toset(local.repos)
  display_name     = "repo-${each.key}"
  mail_nickname    = "repo-${each.key}"
  security_enabled = true
}

resource "azuread_group_member" "devops" {
  for_each         = toset(local.repos)
  group_object_id  = azuread_group.repo[each.key].object_id
  member_object_id = var.devops_group_id
}

Here a JSON file stands in for that list. The repository docs becomes handbook, and the list follows on the next run.

What the plan sees

  # azuread_group.repo["docs"] will be destroyed
  # (because key ["docs"] is not in for_each map)
  # azuread_group.repo["handbook"] will be created
  # azuread_group_member.devops["docs"] will be destroyed
  # (because key ["docs"] is not in for_each map)
  # azuread_group_member.devops["handbook"] will be created
Plan: 2 to add, 0 to change, 2 to destroy.

The address of an item in a for_each is the resource name plus its key. A new key is a new address, so Terraform sees one group that should no longer exist and one that should.

The new group gets a new object ID. Members someone added by hand are gone, and so is anything outside this configuration that points at the old ID, such as a role assignment.

Fix 1: move the key

A moved block tells Terraform that the object at the old address now lives at the new one:

moved {
  from = azuread_group.repo["docs"]
  to   = azuread_group.repo["handbook"]
}

moved {
  from = azuread_group_member.devops["docs"]
  to   = azuread_group_member.devops["handbook"]
}

Every resource keyed by the repository needs its own block. terraform state list | grep '"docs' lists them. The plan still destroys both objects:

  # azuread_group.repo["handbook"] must be replaced
  # (moved from azuread_group.repo["docs"])
  # azuread_group_member.devops["handbook"] must be replaced
  # (moved from azuread_group_member.devops["docs"])
Plan: 2 to add, 0 to change, 2 to destroy.

Fix 2: the attribute that forces a replacement

The move fixed the address. The attributes still follow the new name, and the provider replaces the group when mail_nickname changes. Its documentation for the argument ends with this sentence.

Changing this forces a new resource to be created.

Terraform Registry: azuread_group

The membership is replaced too, because it points at the group’s object ID, and a new group has a new one.

The full plan marks each such attribute with # forces replacement. Search for that string before you read anything else in a plan.

When the attribute can keep its old value, tell Terraform to stop following it:

resource "azuread_group" "repo" {
  # arguments as above

  lifecycle {
    ignore_changes = [mail_nickname]
  }
}

With both moved blocks and the ignore rule, the plan is the one a reviewer wants to see:

  # azuread_group.repo["handbook"] will be updated in-place
  # (moved from azuread_group.repo["docs"])
  # azuread_group_member.devops["docs"] has moved to azuread_group_member.devops["handbook"]
Plan: 0 to add, 1 to change, 0 to destroy.

The display name changes. The object ID and the members stay. The price is a mail alias that keeps the old name, which matters little on a security group that receives no mail.

New groups still get the right alias, because ignore_changes only applies to updates. It applies to every later update, though, so a deliberate change to the alias is ignored too.

Fix 3: anything that finds the object by name

An object can be renamed in place and still break the configurations that find it by name. Custom roles are the common case.

Renaming a role definition changes its name in place and keeps its ID.

A configuration that assigns the role with role_definition_name reads the new name back on its next plan, finds that it differs from the name in its code, and replaces the assignment. That argument also forces a new resource (provider docs, which call it the name of a built-in role, although it finds custom roles too).

The apply deletes the assignment and then fails to create it, because the old name finds no role. The access stays gone until someone fixes that configuration.

The fix comes before the rename. Switch every assignment to role_definition_id, apply, then rename the role.

The switch must plan no changes. If it plans a replacement, the ID differs from the one in state, so use the role_definition_id the plan shows. The configurations that assign it often live in other repositories, so search for the old name across all of them first.

Catch it before the apply

A scheduled run applies whatever the plan says. A check that stops the run when the plan destroys anything, replacements included, catches the rename before the apply. The group survives, and every other change waits until the pull request with the moved blocks merges. That wait is the cost of the check.

Try it without a provider

terraform_data shows the same plans with no provider and no cloud account. Its input changes in place, like display_name, and a change to triggers_replace forces a replacement, like mail_nickname.

Apply it first with ["docs", "api"] and without the lifecycle block. Then rename docs to handbook and add the blocks one fix at a time. This is the end state:

locals {
  repos = ["handbook", "api"]
}

resource "terraform_data" "group" {
  for_each         = toset(local.repos)
  input            = "repo-${each.key}"
  triggers_replace = "repo-${each.key}"

  lifecycle {
    ignore_changes = [triggers_replace]
  }
}

resource "terraform_data" "member" {
  for_each         = toset(local.repos)
  input            = "devops"
  triggers_replace = terraform_data.group[each.key].id
}

moved {
  from = terraform_data.group["docs"]
  to   = terraform_data.group["handbook"]
}

moved {
  from = terraform_data.member["docs"]
  to   = terraform_data.member["handbook"]
}

The plan prints # forces replacement only on the second resource. The first shows its triggers_replace change without the marker, but its plan line still says “must be replaced”.

What this bought, and what stays manual

The group keeps its object ID and its members under the new name, in one pull request whose plan shows 0 to destroy. The price is a moved block for every resource keyed by the name and an alias that keeps the old name.

Three things stay manual. Someone has to notice the rename, or a check on the plan has to. A second pull request deletes the moved blocks once every state has applied them. Configurations that find the object by name are found by search, before the rename.


Part 2 of a series on Terraform state surgery without downtime. Next: import from the list, not from a string.

terraformgovernancetutorial

read next