Fix Terraform Error: CloudWatch Log Group Already Exists
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
DevOps
Fix terraform moved block errors. Remove old resource blocks, clean up applied moved blocks, and use terraform state mv as an alternative for complex renames.
# Remove the OLD resource block — keep only the NEW name + moved block
moved {
from = aws_instance.web
to = aws_instance.app
}
resource "aws_instance" "app" { # ← Only this block exists
# ...
}
# Do NOT also have: resource "aws_instance" "web" { ... }Error: Moved object still exists
The configuration still has a resource "aws_instance" "web" declared at
main.tf:10,1-32, so Terraform cannot move it to "aws_instance" "app".
To complete the move, remove the original resource block.Or:
Error: Cross-resource move statement
The move statement at main.tf:1,1-3,2 would conflict with the existing
resource "aws_instance" "web" at main.tf:5,1-7,2.moved block but didn't delete the old resource blockaws_instance.web AND aws_instance.app in configmoved block is still theremoved blocks try to move the same resourceThe moved block tells Terraform "the resource formerly at A is now at B." You must delete A's resource block:
Before (broken):
# main.tf
moved {
from = aws_instance.web
to = aws_instance.app
}
resource "aws_instance" "web" { # ❌ Still here — causes error
ami = "ami-abc123"
}
resource "aws_instance" "app" {
ami = "ami-abc123"
}After (correct):
# main.tf
moved {
from = aws_instance.web
to = aws_instance.app
}
resource "aws_instance" "app" { # ✅ Only the new name
ami = "ami-abc123"
}terraform plan # Shows: aws_instance.web has moved to aws_instance.app
terraform apply # Updates state, no infrastructure changesAfter terraform apply successfully processes the move, you can remove the moved block:
# After apply — this is safe to remove
# moved {
# from = aws_instance.web
# to = aws_instance.app
# }
resource "aws_instance" "app" {
ami = "ami-abc123"
}Tip: Keep moved blocks for one release cycle so all team members and CI/CD environments pick up the move, then remove them.
For quick renames without moved blocks:
# Rename in state directly
terraform state mv aws_instance.web aws_instance.appThen rename the resource block in your .tf files. No moved block needed.
moved block | terraform state mv | |
|---|---|---|
| Works in CI/CD | ✅ Automatic on apply | ❌ Manual step |
| Team-friendly | ✅ Code-reviewed in PR | ❌ Must coordinate |
| Reversible | ✅ Remove block to undo | ❌ Run reverse mv |
| Module moves | ✅ Supports module renames | ✅ Supports modules |
| Best for | Team workflows | Quick solo renames |
moved {
from = aws_instance.web
to = aws_instance.application_server
}moved {
from = aws_instance.web
to = module.compute.aws_instance.this
}moved {
from = module.old_vpc.aws_vpc.main
to = module.networking.aws_vpc.main
}moved {
from = module.web
to = module.application
}moved {
from = aws_instance.web[0]
to = aws_instance.web["primary"]
}
moved {
from = aws_instance.web[1]
to = aws_instance.web["secondary"]
}The "moved object still exists" error means you have both the old and new resource blocks. Delete the old one — the moved block handles the state migration. Clean up moved blocks after all environments have applied the change. For quick solo renames, terraform state mv is simpler; for team workflows, moved blocks are safer because they're code-reviewed.
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.