Fix Terraform Error: CloudWatch Log Group Already Exists
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
DevOps
Fix BucketNotEmpty errors when destroying S3 buckets in Terraform. Handle versioned objects, force_destroy, lifecycle rules, and manual cleanup.
S3 buckets must be empty before deletion. Set force_destroy = true in Terraform to auto-delete all objects, or empty the bucket manually with AWS CLI before destroying.
Error: error deleting S3 Bucket (my-bucket): BucketNotEmpty:
The bucket you tried to delete is not emptyforce_destroy is not set (defaults to false)resource "aws_s3_bucket" "data" {
bucket = "my-data-bucket"
force_destroy = true # Deletes ALL objects when bucket is destroyed
}Then:
terraform destroy # Will empty the bucket automatically# Delete all objects
aws s3 rm s3://my-bucket --recursive
# If versioning is enabled, delete all versions too
aws s3api list-object-versions --bucket my-bucket \
--query 'Versions[].{Key:Key,VersionId:VersionId}' \
--output json | \
jq -c '.[]' | while read obj; do
key=$(echo $obj | jq -r '.Key')
version=$(echo $obj | jq -r '.VersionId')
aws s3api delete-object --bucket my-bucket --key "$key" --version-id "$version"
done
# Delete all delete markers
aws s3api list-object-versions --bucket my-bucket \
--query 'DeleteMarkers[].{Key:Key,VersionId:VersionId}' \
--output json | \
jq -c '.[]' | while read obj; do
key=$(echo $obj | jq -r '.Key')
version=$(echo $obj | jq -r '.VersionId')
aws s3api delete-object --bucket my-bucket --key "$key" --version-id "$version"
done
# Now destroy
terraform destroy# List incomplete multipart uploads
aws s3api list-multipart-uploads --bucket my-bucket
# Abort them
aws s3api abort-multipart-upload --bucket my-bucket \
--key "large-file.zip" --upload-id "abc123"| Scenario | Use force_destroy? |
|---|---|
| Dev/staging buckets | ✅ Yes — easy cleanup |
| Production data buckets | ❌ No — protect against accidental deletion |
| CI/CD test buckets | ✅ Yes |
| Terraform state buckets | ❌ No — use prevent_destroy instead |
| Log buckets | Depends — maybe lifecycle rules instead |
# Production: protect against accidental deletion
resource "aws_s3_bucket" "production" {
bucket = "production-data"
lifecycle {
prevent_destroy = true # Terraform will refuse to destroy
}
}aws s3 ls s3://bucket)aws s3api list-object-versions)force_destroy = true set?BucketNotEmpty means there are objects (or versioned objects) in the bucket. Set force_destroy = true for non-production buckets, or empty manually with AWS CLI. For production, use prevent_destroy lifecycle rules to avoid accidental deletion entirely.
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.