TExcelTAD
Home › Infrastructure as Code: Terraform & OpenTofu › Refactoring with moved & removed

Refactoring with moved & removed

Code needs refactoring: a resource gets a better name, a count becomes a for_each, three resources move into a module. For the tool, a new address is a new resource, so every such change plans as destroy the old, create the new, unless you tell it the two addresses are the same object. moved blocks do that in code; removed blocks stop managing something without destroying it.

moved: same object, new address

hcl
moved {
  from = aws_instance.web
  to   = aws_instance.app
}

When the plan finds the object at from in state and the to address in the code, it moves the state entry instead of replacing anything. The block is reviewed in a pull request and applied to every environment the same way, unlike the manual tofu state mv from Inspecting & Editing State.

The session below does a typical refactor in one go: a count resource moves into a module called with for_each, and an old resource is dropped from management while the object itself is kept:

bash · try it yourself
mkdir r && cd r
cat > main.tf <<'HCL'
resource "local_file" "site" {
  count    = 2
  filename = "site-${count.index}.txt"
  content  = "site ${count.index}\n"
}

resource "terraform_data" "legacy" {
  input = "managed by hand from now on"
}
HCL
tofu init >/dev/null && tofu apply -auto-approve >/dev/null
echo "--- before"
tofu state list

mkdir -p modules/site
cat > modules/site/main.tf <<'HCL'
variable "name" {
  type = string
}

resource "local_file" "this" {
  filename = "site-${var.name}.txt"
  content  = "site ${var.name}\n"
}
HCL
cat > main.tf <<'HCL'
module "site" {
  for_each = toset(["0", "1"])
  source   = "./modules/site"
  name     = each.key
}

moved {
  from = local_file.site[0]
  to   = module.site["0"].local_file.this
}

moved {
  from = local_file.site[1]
  to   = module.site["1"].local_file.this
}

removed {
  from = terraform_data.legacy
  lifecycle {
    destroy = false
  }
}
HCL
tofu init >/dev/null
echo "--- plan"
tofu plan | grep -E '^  #|^Plan:'
tofu apply -auto-approve | tail -1
echo "--- after"
tofu state list
ls site-*
--- before
local_file.site[0]
local_file.site[1]
terraform_data.legacy
--- plan
  # terraform_data.legacy will be removed from the OpenTofu state but will not be destroyed
  # local_file.site[0] has moved to module.site["0"].local_file.this
  # local_file.site[1] has moved to module.site["1"].local_file.this
Plan: 0 to add, 0 to change, 0 to destroy, 1 to forget.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed, 1 forgotten.
--- after
module.site["0"].local_file.this
module.site["1"].local_file.this
site-0.txt
site-1.txt

Zero adds, zero destroys: the two files were never touched, and terraform_data.legacy was forgotten (OpenTofu's word; Terraform says it will "no longer be managed"). Without the blocks, the same code change would have planned two replacements and one destroy.

What moved can express

Refactor from → to
rename aws_s3_bucket.b → aws_s3_bucket.assets
add count to a single resource aws_instance.web → aws_instance.web[0]
count → for_each aws_iam_user.u[0] → aws_iam_user.u["ada"]
into a module aws_vpc.main → module.network.aws_vpc.this
rename a module call module.net → module.network (moves everything inside)
change resource type only where the provider supports it, e.g. null_resource → terraform_data (Terraform 1.9+, OpenTofu 1.10+)

Moving out of a module the caller doesn't own (a registry module) is the module author's job: a well-maintained module ships moved blocks in its own code when its internal addresses change, so callers upgrade without replacements.

removed: stop managing without destroying

hcl
removed {
  from = aws_db_instance.legacy
  lifecycle {
    destroy = false        # keep the database, just forget it
  }
}

With destroy = true (the default), deleting the block from the code and adding removed destroys the object, like simply deleting the resource block, but stated explicitly. destroy = false is the declarative state rm: typical when handing a resource to another configuration, which then imports it.

Keeping or deleting the blocks

moved and removed blocks are only needed until every state that might still have the old address has been applied. In an application repository that is usually one release; in a shared module, keep them for a long time (callers upgrade from old versions months later). Deleting a moved block too early turns the refactor back into destroy-and-create for whoever applies late.

Test yourself

You rename aws_instance.web to aws_instance.app in code and add nothing else. What does the plan show?

  1. No changes
  2. Destroy web, create app
  3. An error
  4. A rename

Which block stops managing a resource without destroying the real object?

  1. moved with to = null
  2. removed with lifecycle { destroy = false }
  3. import
  4. lifecycle { prevent_destroy = true }

Why should a published module keep its moved blocks for a long time?

  1. They speed up plans
  2. Callers may upgrade from old versions much later, and still need the moves
  3. Registries require them
  4. They are needed for import