This component provisions AWS ElastiCache Serverless caches with support for Redis, Memcached, and Valkey engines. ElastiCache Serverless provides a serverless option for caching that automatically scales based on demand, eliminating the need to manage cache nodes or plan for capacity.
- VPC with subnets
- Optional: KMS key for encryption at rest
- Optional: Route53 hosted zone for DNS records
Stack parameters that can be provided at runtime:
| Name | Use | Default | Global | Type | Allowed Values |
|---|---|---|---|---|---|
| EnvironmentName | Tagging and naming | dev | true | String | |
| EnvironmentType | Tagging | development | true | String | ['development','production'] |
| VPCId | ID of the VPC to launch cache in | false | AWS::EC2::VPC::Id | ||
| Subnets | Subnet IDs for cache subnet group | false | CommaDelimitedList | ||
| DnsDomain | Domain for Route53 record | false | String | ||
| KmsKeyId | KMS key ID for encryption (when kms_encryption enabled in config) | false | String |
Note: Cache engine and version are configured at compile-time via config.yaml, not as runtime parameters.
The simplest configuration uses the default Redis engine:
# This uses all defaults - Redis 7 with encryption enabledImportant: Engine and version are compile-time configuration. These values are set when the CloudFormation template is generated and cannot be changed at stack runtime. To change the engine or version, you must recompile the template.
Redis (Default)
engine: redis
major_engine_version: '7'Memcached
engine: memcached
major_engine_version: '1.6'
hostname: memcachedValkey
Valkey is an open-source Redis fork:
engine: valkey
major_engine_version: '7'
hostname: valkeyElastiCache Serverless automatically scales, but you can set minimum and maximum limits:
cache_usage_limits:
ecpu_per_second:
minimum: 1000 # Minimum ECPUs per second
maximum: 5000 # Maximum ECPUs per second
data_storage:
minimum: 1 # Minimum storage in GB
maximum: 5 # Maximum storage in GB
unit: GBIf not specified, AWS uses service-defined defaults which automatically scale based on your workload.
ElastiCache Serverless automatically enables encryption at rest and in transit. These cannot be disabled.
To use a custom KMS key for encryption at rest (instead of the AWS managed key):
kms_encryption: true # This enables the KmsKeyId parameterThen provide the KMS key ID as a stack parameter when creating the stack.
For Redis and Valkey engines, you can configure automated snapshots:
daily_snapshot_time: "03:00" # Time for daily snapshots (UTC)
snapshot_retention_limit: 7 # Number of days to retain (0-35)Note: Snapshots are not available for Memcached.
By default, a Route53 CNAME record is created:
hostname: cache
dns_format: ${EnvironmentName}.${DnsDomain}
create_route53_record: trueThis creates a DNS record like: cache.dev.example.com
To disable DNS record creation:
create_route53_record: falseConfigure ingress rules for cache access:
security_group_rules:
- protocol: tcp
from: 6379
to: 6379
ip_blocks:
- stack # Reference to another stack's security groupOr allow from specific CIDR:
security_group_rules:
- protocol: tcp
from: 6379
to: 6379
cidr: 10.0.0.0/8Add custom tags to all resources:
tags:
CostCenter: Engineering
Project: ${EnvironmentName}-cache| Output | Description | Export Name |
|---|---|---|
| SecurityGroupId | ID of the cache security group | ${EnvironmentName}-${ComponentName}-SecurityGroup |
| CacheEndpoint | DNS endpoint address of the cache | ${EnvironmentName}-${ComponentName}-Endpoint |
| CachePort | Port number of the cache | ${EnvironmentName}-${ComponentName}-Port |
| CacheFullEndpoint | Full endpoint with port (host:port) | ${EnvironmentName}-${ComponentName}-FullEndpoint |
| CacheArn | ARN of the serverless cache | ${EnvironmentName}-${ComponentName}-ARN |
| CacheDnsName | Route53 DNS name (if created) | ${EnvironmentName}-${ComponentName}-DnsName |
engine: redis
major_engine_version: '7'
hostname: redis-cache
daily_snapshot_time: "02:00"
snapshot_retention_limit: 14
cache_usage_limits:
ecpu_per_second:
minimum: 2000
maximum: 10000engine: memcached
major_engine_version: '1.6'
hostname: memcached
cache_usage_limits:
data_storage:
minimum: 2
maximum: 8
unit: GBengine: valkey
major_engine_version: '7'
hostname: valkey-cache
dns_format: ${EnvironmentName}.internal.${DnsDomain}
security_group_rules:
- protocol: tcp
from: 6379
to: 6379
ip_blocks:
- app-stack
- worker-stackengine: redis
major_engine_version: '7'
create_route53_record: false- ElastiCache Serverless automatically handles scaling, backups, and patching
- No instance type selection is needed - capacity is automatically managed
- Pricing is based on data storage and compute (ECPUs) used
- Snapshot features are only available for Redis and Valkey engines
- For production workloads, consider enabling KMS encryption and configuring appropriate capacity limits
lib-ec2@0.1.0- For security group rule generation
Apache 2.0 - See LICENSE file for details