Fix Terraform Error: CloudWatch Log Group Already Exists
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
DevOps
Fix RepositoryAlreadyExistsException when creating ECR repositories in Terraform. Import existing repos, configure lifecycle policies, and manage image cleanup.
An ECR repository with that name already exists in your AWS account and region. Import it with terraform import aws_ecr_repository.app my-app, or use a unique name.
Error: error creating ECR Repository: RepositoryAlreadyExistsException:
The repository with name 'my-app' already exists in the registry
with id '123456789012'terraform import aws_ecr_repository.app my-appresource "aws_ecr_repository" "app" {
name = "${var.project}/${var.environment}/app"
image_tag_mutability = "IMMUTABLE" # Prevent tag overwrites
image_scanning_configuration {
scan_on_push = true
}
encryption_configuration {
encryption_type = "AES256"
}
}
# Clean up old images automatically
resource "aws_ecr_lifecycle_policy" "app" {
repository = aws_ecr_repository.app.name
policy = jsonencode({
rules = [{
rulePriority = 1
description = "Keep last 10 images"
selection = {
tagStatus = "any"
countType = "imageCountMoreThan"
countNumber = 10
}
action = { type = "expire" }
}]
})
}# WARNING: This deletes all images in the repository
aws ecr delete-repository --repository-name my-app --force
terraform applyaws ecr describe-repositories --repository-names my-app)ECR repository names are unique per account per region. Import existing repositories with terraform import, add lifecycle policies to manage image cleanup, and use namespaced names (project/env/app) to organize repositories.
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.