Review ElastiCache subnet group selection

Explicitly select the VPC and subnets for the cache.

Description

ElastiCache places cache nodes in the subnets of its subnet group. If you omit the group, the cluster can use the account’s default VPC.

Potential impact

An unintended VPC can leave the application without a route to the cache or give unnecessary clients a network path.

Remediation

Check the group’s VPC and subnets, then set subnet_group_name to that group. Allow cache connections only from the applications that need them. Moving an existing cluster to another subnet group requires replacement. Review the Terraform plan and prepare to repopulate the cache and switch clients.

Examples

The Memcached example adds var.subnet_group_name. Set it to an existing group name and supply the referenced parameter group.

Before

hcl
resource "aws_elasticache_cluster" "example" {
  cluster_id           = "cluster-example"
  engine               = "memcached"
  node_type            = "cache.m4.large"
  num_cache_nodes      = 2
  parameter_group_name = aws_elasticache_parameter_group.default.id
  port                 = 11211
}

After

hcl
resource "aws_elasticache_cluster" "example" {
  cluster_id           = "cluster-example"
  engine               = "memcached"
  node_type            = "cache.m4.large"
  num_cache_nodes      = 2
  parameter_group_name = aws_elasticache_parameter_group.default.id
  port                 = 11211
  subnet_group_name    = var.subnet_group_name
}

References