TerraformPilot

Troubleshooting

Fix Terraform Elasticache Cluster - CacheClusterAlreadyExists

Fix ElastiCache cluster name conflicts in Terraform. Import existing clusters, use unique naming conventions, and handle replication group configurations.

LLuca Berton1 min read

Quick Answer

#

An ElastiCache cluster with the same ID already exists. Import it into Terraform, choose a unique cluster ID, or delete the orphaned cluster. ElastiCache IDs are unique per region per account.

The Error

#
Error: creating ElastiCache Cluster (redis-prod):
  CacheClusterAlreadyExists: CacheCluster redis-prod already exists.

What Causes This Error

#
  1. Cluster created manually via Console or CLI, now Terraform tries to create one with the same name
  2. Another Terraform workspace already manages a cluster with this name
  3. Failed previous apply created the cluster but didn't record it in state
  4. Recently deleted cluster — ElastiCache may take minutes to fully release the name

How to Fix It

#

Solution 1: Import the Existing Cluster

#
aws elasticache describe-cache-clusters --cache-cluster-id redis-prod
 
terraform import aws_elasticache_cluster.redis redis-prod

Solution 2: Use Unique Naming

#
resource "aws_elasticache_cluster" "redis" {
  cluster_id           = "${var.project}-${var.environment}-redis"
  engine               = "redis"
  node_type            = "cache.t3.micro"
  num_cache_nodes      = 1
  parameter_group_name = "default.redis7"
  port                 = 6379
  subnet_group_name    = aws_elasticache_subnet_group.redis.name
  security_group_ids   = [aws_security_group.redis.id]
}

Solution 3: Use Replication Group Instead

#

For production Redis, use a replication group (supports failover):

resource "aws_elasticache_replication_group" "redis" {
  replication_group_id = "${var.project}-${var.environment}"
  description          = "Redis for ${var.project}"
  node_type            = "cache.r6g.large"
  num_cache_clusters   = 2
  engine_version       = "7.0"
  port                 = 6379
  subnet_group_name    = aws_elasticache_subnet_group.redis.name
  security_group_ids   = [aws_security_group.redis.id]
 
  automatic_failover_enabled = true
  at_rest_encryption_enabled = true
  transit_encryption_enabled = true
}

Troubleshooting Checklist

#
  1. ✅ Does the cluster exist? (aws elasticache describe-cache-clusters)
  2. ✅ Should you import it or delete it?
  3. ✅ Are you using environment-specific names?
  4. ✅ Was a previous apply interrupted?

Prevention Tips

#
  • Include environment in cluster IDsmyapp-prod-redis not redis
  • Use replication groups for production — better availability and naming
  • Tag with ManagedBy = "terraform" for easy identification
  • Import existing clusters before creating new ones
#

Conclusion

#

CacheClusterAlreadyExists means the name is taken. Import the existing cluster or use unique environment-prefixed names. For production Redis, prefer replication groups over standalone clusters.

#Terraform#AWS#Troubleshooting#Error Fix

Share this article