Chapter 61 · Amazon Elasticache
Subchapter 61.44
references/setup/iac-best-practices.mdMarkdown7 KBView on GitHub
Operational guidance for deploying ElastiCache via CloudFormation, Terraform, and CDK. For resource names, property mappings, and endpoint attributes, see iac-reference.md.
ElastiCache control-plane APIs enforce throttle limits. Deploying multiple caches in a single stack or account can trigger ThrottlingException.
Mitigation strategies:
DependsOn between them so they provision sequentially rather than in parallel.MaxConcurrentCount: 1 (or a low value) in StackSet operation preferences to stagger deployments.terraform apply -parallelism=1 or add depends_on between cache resources to serialize creation.A stack can get stuck when a rollback itself fails (for example, a resource that was modified outside of CFN).
Resolution:
aws cloudformation describe-stack-events to identify the failed resource.aws cloudformation continue-update-rollback --stack-name <name> to retry.aws cloudformation continue-update-rollback --stack-name <name> --resources-to-skip <logical-id>.Serverless caches create VPC endpoints in the associated subnets. If these endpoints still exist when the stack tries to delete the cache or its networking resources, deletion fails.
Resolution:
aws ec2 describe-vpc-endpoints --filters Name=vpc-id,Values=<vpc-id> and identify ElastiCache-related endpoints.aws ec2 delete-vpc-endpoints --vpc-endpoint-ids <id1> <id2>.Prevention: In CFN templates, place the cache resource with an explicit DependsOn on the VPC/subnet resources so CFN deletes the cache (and its endpoints) before attempting to remove networking resources.
Common causes:
Define AWS::ElastiCache::ParameterGroup (CFN) or aws_elasticache_parameter_group (Terraform) as standalone resources rather than relying on defaults. This allows parameter changes without replacing the cache.
Always export cache endpoints so dependent stacks or applications can reference them:
Outputs:
CacheEndpoint:
# For AWS::ElastiCache::ServerlessCache: !GetAtt Cache.Endpoint.Address
# For AWS::ElastiCache::ReplicationGroup (CME): !GetAtt Cache.ConfigurationEndPoint.Address
# For AWS::ElastiCache::ReplicationGroup (CMD): !GetAtt Cache.PrimaryEndPoint.Address
Value: !GetAtt Cache.PrimaryEndPoint.Address
Export:
Name: !Sub "${AWS::StackName}-cache-endpoint"Use Conditions and Mappings to manage dev/staging/prod from a single template:
Parameters:
Environment:
Type: String
AllowedValues: [dev, staging, prod]
Conditions:
IsProd: !Equals [!Ref Environment, prod]Then use !If [IsProd, ...] to vary node types, replica counts, snapshot retention, and cost limits by environment. For serverless, vary CacheUsageLimits (lower maximums in dev to control cost).
For node-based replication groups, define auto-scaling policies directly in your templates using AWS::ApplicationAutoScaling::ScalableTarget and AWS::ApplicationAutoScaling::ScalingPolicy. You can scale replicas (elasticache:replication-group:Replicas) or shards (elasticache:replication-group:NodeGroups). For example:
ScalingTarget:
Type: 'AWS::ApplicationAutoScaling::ScalableTarget'
Properties:
MaxCapacity: 5
MinCapacity: 1
ResourceId: !Sub replication-group/${MyReplicationGroup}
ScalableDimension: 'elasticache:replication-group:Replicas'
ServiceNamespace: elasticache
ScalingPolicy:
Type: 'AWS::ApplicationAutoScaling::ScalingPolicy'
Properties:
ScalingTargetId: !Ref ScalingTarget
ServiceNamespace: elasticache
PolicyName: target-tracking-cpu
PolicyType: TargetTrackingScaling
ScalableDimension: 'elasticache:replication-group:Replicas'
TargetTrackingScalingPolicyConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: ElastiCacheReplicaEngineCPUUtilization
TargetValue: 60Note: The
ScalableTargetsupports an optionalRoleARNproperty. If omitted, Application Auto Scaling uses the service-linked roleAWSServiceRoleForApplicationAutoScaling_ElastiCacheRG, which is created automatically. If the service-linked role does not yet exist in your account, either addRoleARNexplicitly or ensure the deploying principal hasiam:CreateServiceLinkedRolepermission.
Use Terraform workspaces or tfvars files per environment. Define cache configuration as variables with environment-specific defaults:
variable "node_type" {
default = "cache.t4g.micro" # overridden in prod.tfvars
}elasticache:Create*, elasticache:Modify*, elasticache:Delete*, elasticache:Describe*, elasticache:List*, elasticache:AddTagsToResource, elasticache:ListTagsForResource, elasticache:CopySnapshot, and elasticache:TestFailover, plus ec2:CreateVpcEndpoint, ec2:DeleteVpcEndpoints, and iam:CreateServiceLinkedRole (with condition iam:AWSServiceName: elasticache.amazonaws.com). This action set covers every create/modify/delete/describe call this skill issues. See the Provisioning profile in iam-policies.md for the canonical list. elasticache:* is acceptable only for initial prototyping; do not use it in production. The iam:CreateServiceLinkedRole permission is required for first-time deployments or when the ElastiCache service-linked role does not yet exist in the account (see IAM.IdentityBasedPolicies Example 4). Note: for serverless caches, VPC endpoint lifecycle is managed by the ElastiCache service-linked role, but the deploying role still needs ec2:CreateVpcEndpoint for the initial creation call.CacheUsageLimits set to prevent unexpected cost in non-production environments