Fix Terraform Error: CloudWatch Log Group Already Exists
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
DevOps
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...
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.
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.The moved block's from address no longer exists because the move already happened in a previous apply.
# ❌ Typo in the from address
moved {
from = aws_instace.web # Wrong: "instace" instead of "instance"
to = aws_instance.app
}If you already ran terraform state mv manually, the moved block can't find the old address.
# ❌ Module path doesn't match state
moved {
from = module.old.aws_instance.web # Not in state
to = module.new.aws_instance.web
}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
# }# 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
}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.webmoved {
from = aws_instance.web_server
to = aws_instance.app
}
resource "aws_instance" "app" {
# ... same config as the old web_server
}moved {
from = aws_instance.web
to = module.compute.aws_instance.web
}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 with correct from and toterraform plan — shows "moved" not "destroy + create"terraform apply — updates statemoved block after apply (it's served its purpose)from address exist in terraform state list?terraform state mv already run manually?terraform apply already completed the move?"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.
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
Fix terraform import errors when a resource already exists in state. Covers state rm, state show, reimport workflow, import blocks
Fix terraform too many command line arguments errors. Correct -var syntax, quote values with spaces, and learn proper Terraform CLI argument format for plan
Fix terraform invalid escape sequence errors. Double backslashes for Windows paths, use heredocs for regex, and learn all valid HCL escape sequences.