Fix Terraform Error: CloudWatch Log Group Already Exists
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
DevOps
Fix ConflictException when creating AWS API Gateway resources in Terraform. Handle duplicate REST APIs, stages, and resource path conflicts.
An API Gateway resource with the same name or path already exists. Import the existing resource, use a unique name, or delete the orphaned API before re-creating.
Error: error creating API Gateway Rest API: ConflictException:
REST API with this name already existsError: error creating API Gateway Stage: ConflictException:
Stage already exists# Find the API ID
aws apigateway get-rest-apis --query 'items[?name==`my-api`].id' --output text
# Import the REST API
terraform import aws_api_gateway_rest_api.main abc123def4
# Import a stage
terraform import aws_api_gateway_stage.prod abc123def4/prodresource "aws_api_gateway_rest_api" "main" {
name = "${var.project}-${var.environment}-api"
description = "API for ${var.project}"
endpoint_configuration {
types = ["REGIONAL"]
}
}
resource "aws_api_gateway_stage" "prod" {
deployment_id = aws_api_gateway_deployment.main.id
rest_api_id = aws_api_gateway_rest_api.main.id
stage_name = var.environment
}# List all APIs
aws apigateway get-rest-apis
# Delete the conflicting one
aws apigateway delete-rest-api --rest-api-id abc123def4terraform state list | grep api_gateway)API Gateway ConflictException means the resource exists. Import it, use unique names with environment prefixes, or delete the orphaned resource. Check both AWS Console and Terraform state to find the duplicate.
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.