TerraformPilot

DevOps

Fix Terraform Error - Moved Block Resource Not In State

Fix 'moved block refers to a resource not in the state' when refactoring Terraform. Covers moved blocks, state mv, module renames, and count-to-for_each...

LLuca Berton1 min read

Quick Answer

#

The moved block references a resource address that doesn't exist in state. Either the resource was already moved/destroyed, the address is misspelled, or the state was modified outside of Terraform. Remove the moved block if the migration is complete, or fix the from address to match the actual state.

The Error

#
Error: Moved object is not in the state
 
This statement declares a move from
module.old_name.aws_instance.web but there is no such
object in the state.

What Causes This

#

1. Resource Already Moved or Destroyed

#

The moved block's from address no longer exists because the move already happened in a previous apply.

2. Misspelled Resource Address

#
# ❌ Typo in the from address
moved {
  from = aws_instace.web  # Wrong: "instace" instead of "instance"
  to   = aws_instance.app
}

3. State Modified with terraform state mv

#

If you already ran terraform state mv manually, the moved block can't find the old address.

4. Wrong Module Path

#
# ❌ Module path doesn't match state
moved {
  from = module.old.aws_instance.web     # Not in state
  to   = module.new.aws_instance.web
}

How to Fix It

#

Solution 1: Remove Completed Moved Blocks

#

If the move already happened (previous apply or manual state mv):

# Just delete the moved block — it's done
# moved {
#   from = aws_instance.web
#   to   = aws_instance.app
# }

Solution 2: Fix the Resource Address

#
# Check what's actually in state
terraform state list
 
# Find the correct address
terraform state list | grep instance
# Fix the from address to match state
moved {
  from = aws_instance.web       # Must match state exactly
  to   = aws_instance.app
}

Solution 3: Use terraform state mv Instead

#

If the moved block isn't working, use the CLI directly:

# Manual state move
terraform state mv aws_instance.web aws_instance.app
 
# Move into a module
terraform state mv aws_instance.web module.compute.aws_instance.web
 
# Move between modules
terraform state mv module.old.aws_instance.web module.new.aws_instance.web

Common Moved Block Patterns

#

Rename a Resource

#
moved {
  from = aws_instance.web_server
  to   = aws_instance.app
}
 
resource "aws_instance" "app" {
  # ... same config as the old web_server
}

Move into a Module

#
moved {
  from = aws_instance.web
  to   = module.compute.aws_instance.web
}

count to for_each Migration

#
moved {
  from = aws_subnet.private[0]
  to   = aws_subnet.private["us-east-1a"]
}
 
moved {
  from = aws_subnet.private[1]
  to   = aws_subnet.private["us-east-1b"]
}

Moved Block Lifecycle

#
  1. Add moved block with correct from and to
  2. terraform plan — shows "moved" not "destroy + create"
  3. terraform apply — updates state
  4. Remove the moved block after apply (it's served its purpose)

Troubleshooting Checklist

#
  1. ✅ Does the from address exist in terraform state list?
  2. ✅ Is the address spelled correctly (including module paths)?
  3. ✅ Was terraform state mv already run manually?
  4. ✅ Has a previous terraform apply already completed the move?
#

Conclusion

#

"Moved object not in state" means the from address doesn't match anything in your current state. Check terraform state list, fix the address or remove the moved block if the migration is already done. moved blocks are temporary — delete them after a successful apply.

#Terraform#Troubleshooting#DevOps#Error Fix#Infrastructure as Code

Share this article