Fix Terraform Error: CloudWatch Log Group Already Exists
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
DevOps
Fix 'provider produced inconsistent result' errors in Terraform. Handle API normalization, default values, computed fields, and provider bugs.
The cloud API returned a value different from what Terraform expected after applying. This is usually a provider bug (API normalizes values) or a computed attribute that changes server-side. Workaround with ignore_changes lifecycle or upgrade the provider.
Error: Provider produced inconsistent result after apply
When applying changes to aws_security_group.web, the provider
produced an unexpected new value for was present, but now absent.The cloud API reformats your input. For example:
10.0.0.0/16 becomes 10.0.0.0/16 (trailing zeros stripped)The API adds fields you didn't specify, and the provider doesn't handle them correctly.
The provider doesn't properly reconcile what it sent vs. what the API returned.
AWS services like IAM may take time to propagate; Terraform reads back stale data.
terraform init -upgrade
terraform plan
# Many inconsistency bugs are fixed in newer provider versionsresource "aws_security_group" "web" {
name = "web-sg"
vpc_id = aws_vpc.main.id
lifecycle {
ignore_changes = [
ingress, # Ignore if API reorders ingress rules
tags, # Ignore if API normalizes tags
]
}
}# ❌ Raw JSON string — API may reorder keys
resource "aws_iam_role" "main" {
assume_role_policy = <<-EOF
{"Version":"2012-10-17","Statement":[...]}
EOF
}
# ✅ Use jsonencode — canonical key ordering
resource "aws_iam_role" "main" {
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = { Service = "ec2.amazonaws.com" }
}]
})
}Sometimes the second apply succeeds because state now matches reality:
terraform apply # May succeed on retryterraform apply -refresh-only # Sync state with actual cloud values
terraform plan # Check if issue is resolved| Resource | Cause | Fix |
|---|---|---|
aws_security_group | Rule reordering | ignore_changes = [ingress, egress] |
aws_iam_role | Policy JSON normalization | Use jsonencode() |
aws_s3_bucket | ACL/CORS defaults | Upgrade provider |
azurerm_* | API returns extra fields | Upgrade provider |
jsonencode() for policy documents?ignore_changes work as a workaround?terraform apply succeed?"Inconsistent result after apply" is usually a provider bug where the API normalizes values differently than expected. Upgrade the provider first, use jsonencode() for policies, and ignore_changes as a last resort. Report persistent issues on the provider's GitHub.
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.