Runbook Lab

Terraform moved blocks: into a module, count to for_each

By Abdelrhman Fikri · Published 2026-12-02

You renamed a resource, wrapped it in a module, or changed count to for_each. Terraform now wants to destroy and recreate it, because as far as it can tell the old address vanished and a new one appeared. For a file that is harmless. For a database or a VNet it is not.

The fix is a moved block. Below is the exact before and after, including the destructive plan you get without it.

The starting point

Three resources: one config file at the root, and two workers created 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"
}

After terraform apply the output ended with:

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

The refactor

Two changes:

  1. app_config moves into modules/app/, called as module "app".
  2. worker changes from count to for_each, keyed by names (primary, replica) 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"
}

Without moved blocks: Terraform wants to destroy everything

I ran terraform plan against the existing state with only the refactor above:

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

(Trimmed to the headline lines. The full output is in the evidence folder.)

Three resources destroyed, three created. Same files, new addresses.

With moved blocks: no destroy

Add a file, for example moved.tf, that tells Terraform the old addresses and the new ones are the same object:

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

Now terraform plan:

  # 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 finished with Apply complete! Resources: 0 added, 0 changed, 0 destroyed. A second plan reported No changes. Your infrastructure matches the configuration. and exited with code 0 under -detailed-exitcode.

The resource IDs in state did not change (for example 1e99b877... before and after), and terraform state list shows the new addresses:

local_file.worker["primary"]
local_file.worker["replica"]
module.app.local_file.app_config

Rules of thumb

  • Always read the plan for the words “has moved”. 0 to destroy is the check that matters.
  • Match the old key exactly. count uses numbers (worker[0]), for_each uses strings (worker["primary"]). Write one moved block per instance.
  • Change only the address in the same commit. If the filename or content changes too, you will see a normal in-place change or replacement on top of the move. Keep refactors and behavior changes separate so the plan stays readable.
  • Keep the moved blocks while anyone might still apply an older state, for example a module that other teams consume. Terraform’s documentation covers when it is safe to remove them; check it for your version.
  • Prefer moved over terraform state mv when the code is shared: the move is reviewed in a pull request and replays in every environment on its next plan, instead of being a manual step someone must remember.

Reproduce it

The script that produced every output on this page is in the evidence folder. It needs only the Terraform binary and downloads the local provider on init.

./run.sh
cat recorded/06-plan-with-moved.txt