Fix Terraform Error: CloudWatch Log Group Already Exists
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
DevOps
How to fix 'Error loading state' from S3 backends including AccessDenied, NoSuchBucket, and corrupted state file issues.
Error refreshing state: AccessDenied / NoSuchBucket / state data in S3 does not have the expected contentTerraform cannot load or parse the state file from S3. Common causes: IAM permissions too restrictive, bucket doesn't exist, bucket is in wrong region, state file was manually edited and corrupted, or KMS encryption key access issues.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::my-terraform-state",
"arn:aws:s3:::my-terraform-state/*"
]
},
{
"Effect": "Allow",
"Action": [
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:DeleteItem"
],
"Resource": "arn:aws:dynamodb:*:*:table/terraform-locks"
}
]
}aws s3 ls s3://my-terraform-state/
aws s3api get-bucket-location --bucket my-terraform-stateterraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "us-east-1" # Must match bucket region!
}
}# Download the state file
aws s3 cp s3://my-terraform-state/prod/terraform.tfstate .
# Check if it's valid JSON
python3 -m json.tool terraform.tfstate
# If corrupted, restore from S3 versioning
aws s3api list-object-versions \
--bucket my-terraform-state \
--prefix prod/terraform.tfstate
# Restore a previous version
aws s3api get-object \
--bucket my-terraform-state \
--key prod/terraform.tfstate \
--version-id "VERSION_ID_HERE" \
terraform.tfstate.restoredterraform plan — always review before applyingterraform validate — catches syntax errors earlyLearn to avoid these errors with interactive, project-based courses:
This error is common and fixable. Follow the solutions above, and check our Terraform course for hands-on training that covers real-world troubleshooting scenarios.
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.