I renamed a resource, wrapped it in a module, and changed count to for_each. The next terraform plan wanted to destroy and recreate everything.

For a local file that does not matter. For a database or a VNet it does. Here is the exact before and after, including the bad plan.

I ran all of this with Terraform 1.17.2 and the local provider, with local state and no cloud account.

The starting point

One config file at the root, and 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" } 

terraform apply ended with:

Apply complete! Resources: 3 added, 0 changed, 0 destroyed. 

The refactor

Two changes:

  1. app_config moves into modules/app/ and is called as module "app".
  2. worker switches from count to for_each, keyed by names instead of indexes.
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" } 

The 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. 

Same files. New addresses. Terraform cannot tell they are the same objects.

The fix: one moved block per instance

I added a moved.tf:

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"] } 

The plan now 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. 

terraform apply reported 0 added, 0 changed, 0 destroyed. A second plan said No changes and exited 0 with -detailed-exitcode. The resource IDs in state were the same before and after. terraform state list showed the new addresses.

What I would check every time

  • Read the plan for the words "has moved". 0 to destroy is the line that matters.
  • Match the old key exactly. count uses numbers, for_each uses strings.
  • Change only the address in that commit. If filename or content changes too, you get a normal change on top of the move, and the plan gets hard to read.
  • Keep the moved blocks while someone might still apply an older state, for example if other teams use your module. Check the Terraform docs for your version before removing them.
  • I prefer moved to terraform state mv for shared code. The move gets reviewed in a pull request and replays in every environment.

What I did not test

I did not try a real provider such as azurerm or aws. I did not try remote state or state locking. The moved mechanics should not depend on the provider, but read your own plan before you apply.

The full write-up, with the script that produced every output above, is here: https://runbook-lab.pages.dev/posts/terraform-moved-block-module-count-to-for-each/.

I also keep a few tested Terraform modules (an S3 bucket and an Azure storage account, both with offline tests) in a free pack. I made it. It is here if you want a starting point: https://fikriwave7.gumroad.com/l/nacamf. I have not run those modules against a real cloud account either.

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.