TerraformPilot

DevOps

Fix Terraform Error - Error Creating CloudWatch Log Group - ResourceAlreadyExistsException

Fix ResourceAlreadyExistsException when creating CloudWatch Log Groups in Terraform. Import existing groups, handle retention policies, and prevent name...

LLuca Berton1 min read

Quick Answer

#

A CloudWatch Log Group with that name already exists. Import it with terraform import aws_cloudwatch_log_group.main /my/log/group, or use a unique name. Many AWS services auto-create log groups — check before creating them in Terraform.

The Error

#
Error: creating CloudWatch Log Group (/ecs/my-app): 
ResourceAlreadyExistsException: The specified log group already exists

What Causes This

#
  • AWS service auto-created it — Lambda, ECS, API Gateway automatically create log groups
  • Created manually in CloudWatch console
  • Previous Terraform apply created it but state was lost
  • Duplicate across configs — two Terraform configs managing the same log group

How to Fix It

#

Solution 1: Import the Existing Log Group

#
terraform import aws_cloudwatch_log_group.app /ecs/my-app

Solution 2: Let AWS Create It (Skip Terraform)

#

For Lambda and ECS, you can let AWS auto-create the log group and just manage retention:

# Instead of creating the log group, import it after first deployment
# Or use data source to reference it
data "aws_cloudwatch_log_group" "lambda" {
  name = "/aws/lambda/${aws_lambda_function.main.function_name}"
}

Solution 3: Create Before the Service

#
# Create log group BEFORE the Lambda/ECS resource
resource "aws_cloudwatch_log_group" "lambda" {
  name              = "/aws/lambda/${var.function_name}"
  retention_in_days = 30
 
  # Create before Lambda so Terraform owns it
  lifecycle {
    create_before_destroy = true
  }
}
 
resource "aws_lambda_function" "main" {
  depends_on    = [aws_cloudwatch_log_group.lambda]
  function_name = var.function_name
  # ...
}

Solution 4: Use Unique Names

#
resource "aws_cloudwatch_log_group" "app" {
  name              = "/${var.project}/${var.environment}/app"
  retention_in_days = 14
  tags              = { Environment = var.environment }
}

Services That Auto-Create Log Groups

#
AWS ServiceLog Group Pattern
Lambda/aws/lambda/<function-name>
ECS/ecs/<service-name> (if configured)
API Gateway/aws/apigateway/<api-id>
RDS/aws/rds/instance/<id>/<log-type>
VPC Flow LogsCustom name
CodeBuild/aws/codebuild/<project-name>

Troubleshooting Checklist

#
  1. ✅ Does the log group exist? (aws logs describe-log-groups --log-group-name-prefix /ecs/my-app)
  2. ✅ Was it auto-created by a service?
  3. ✅ Can you import it instead of creating?
  4. ✅ Is Terraform creating the log group before the service that needs it?
#

Conclusion

#

CloudWatch Log Groups often already exist because AWS services auto-create them. Import existing groups into Terraform state, or create them before the service that uses them. Set retention_in_days to avoid unlimited log storage costs.

#Terraform#Troubleshooting#DevOps#Error Fix#Infrastructure as Code

Share this article