DEV Community

Abdelrhman Fikri
Abdelrhman Fikri

Posted on Edited on Originally published at runbook-lab.pages.dev

Terraform moved blocks: refactor without destroying anything

I wrapped one resource in a module and switched another from count to for_each. Then I ran terraform plan. It wanted to destroy all three resources and create three new ones.

Same files, different addresses. Terraform can't tell they're the same objects.

On a local file that's nothing. On a database it's a bad afternoon. So I kept the plan with and without moved blocks. This was Terraform 1.17.2 with the local provider, local state, no cloud account.

Starting point

One config file at the root, plus two workers made with count:

resource "local_file" "app_config" {
  filename = "${path.module}/out/app.conf"
  content  = "port=8080\n"
}

resource "local_file" "worker" {
  count    = 2
  filename = "${path.module}/out/worker-${count.index}.conf"
  content  = "worker=${count.index}\n"
}
Enter fullscreen mode Exit fullscreen mode

terraform apply ended with:

Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
Enter fullscreen mode Exit fullscreen mode

The refactor

app_config goes into modules/app/ and gets called as module "app". worker switches to for_each, keyed by name instead of index.

module "app" {
  source = "./modules/app"
}

locals {
  workers = {
    primary = 0
    replica = 1
  }
}

resource "local_file" "worker" {
  for_each = local.workers
  filename = "${path.module}/out/worker-${each.value}.conf"
  content  = "worker=${each.value}\n"
}
Enter fullscreen mode Exit fullscreen mode

One detail inside the module: I wrote the filename as ${path.root}/out/app.conf, not path.module. That keeps it at ./out/app.conf. With path.module the path would move into the module folder, and a changed filename means a new file. I didn't try that variant. If a plan still shows a replace after you've added your move blocks, check paths like this first.

Plan without moved blocks

I ran terraform plan against the existing state:

  # local_file.app_config will be destroyed
  # (because local_file.app_config is not in configuration)
  # local_file.worker[0] will be destroyed
  # (because resource does not use count)
  # local_file.worker[1] will be destroyed
  # (because resource does not use count)
  # local_file.worker["primary"] will be created
  # local_file.worker["replica"] will be created
  # module.app.local_file.app_config will be created

Plan: 3 to add, 0 to change, 3 to destroy.
Enter fullscreen mode Exit fullscreen mode

(I cut the attribute detail. The headline lines are all there.)

Plan with moved blocks

I added a moved.tf, one block per instance:

moved {
  from = local_file.app_config
  to   = module.app.local_file.app_config
}

moved {
  from = local_file.worker[0]
  to   = local_file.worker["primary"]
}

moved {
  from = local_file.worker[1]
  to   = local_file.worker["replica"]
}
Enter fullscreen mode Exit fullscreen mode

Now the plan reads:

  # local_file.worker[0] has moved to local_file.worker["primary"]
  # local_file.worker[1] has moved to local_file.worker["replica"]
  # local_file.app_config has moved to module.app.local_file.app_config

Plan: 0 to add, 0 to change, 0 to destroy.
Enter fullscreen mode Exit fullscreen mode

I applied it. 0 added, 0 changed, 0 destroyed, which looks like nothing happened. But that apply is the step that rewrites the addresses in state. A second plan said No changes and exited 0 with -detailed-exitcode. terraform state list showed the new addresses. The IDs hadn't changed (worker 0 was 1e99b877... before and after), and all three files were still in out/.

Things I'd check again

Read the plan for "has moved". 0 to destroy is the line that matters, not the absence of red.

Match the old key exactly. count gives you numbers like worker[0], for_each wants strings like worker["primary"].

Keep the refactor commit to addresses only. Change content in the same commit and you get a normal in-place change mixed in with the move, which is hard to review.

Leave the moved blocks in while anyone might still apply an older state, like other teams using your module. Check the Terraform docs for your version before you delete them.

For shared code I'd rather use moved than terraform state mv. It goes through a pull request and replays in every environment, so nobody has to remember a manual step.

That was all local files. I haven't tried azurerm or aws, remote state, or locking, so read your own plan before you apply.

The full write-up with the script that produced every output above is here. I also put a few Terraform modules I've tested offline (an S3 bucket and an Azure storage account) in a free pack. I made it. No real AWS or Azure account has seen those modules yet.

Disclosure: I wrote this with an AI assistant. The commands were run for real in a scratch folder and the outputs above are copied from that run.

Top comments (0)