Skip to content

ElastiCache Redis in AWS provides managed Redis instances for caching and session storage. Stackattack creates Redis clusters with secure networking, parameter groups, and proper security group configuration.

import * as saws from "@stackattack/aws";
const ctx = saws.context();
const network = saws.vpc(ctx);
const cache = saws.redis(ctx, {
network: network.network("private")
});
export const redisUrl = cache.url;

After deploying a Redis cluster, you can connect to it using:

Direct Connection:

Terminal window
# Connect using redis-cli from an EC2 instance in the same VPC
redis-cli -u redis://your-redis-endpoint.cache.amazonaws.com:6379/0
# Test basic operations
SET mykey "Hello Redis"
GET mykey

Application Code:

import Redis from "ioredis";
const redis = new Redis({
host: "your-redis-endpoint.cache.amazonaws.com",
port: 6379,
retryDelayOnFailover: 100,
maxRetriesPerRequest: 3
});
await redis.set("session:123", JSON.stringify({ userId: 456 }));
const session = await redis.get("session:123");

Redis clusters work together with other Stackattack components:

  • vpc - Provides secure private networking for Redis access

ElastiCache Redis costs are fixed hourly charges based on node type:

  • Instance costs - The default cache.t4g.micro costs ~$11.68/month if running 24/7. Larger instances provide more memory and performance:

    • cache.t4g.small (~$23.36/month) - 1.37GB RAM
    • cache.r7g.large (~$146.83/month) - 13.07GB RAM
    • cache.r7g.xlarge (~$293.66/month) - 26.32GB RAM
  • Backup storage - Automatic snapshots are free within the allocated memory size. Additional backup storage is ~$0.085/GB/month.

  • Data transfer - Connections within the same VPC are free. Cross-AZ data transfer costs ~$0.01/GB in each direction.

  • Multi-AZ - If you enable replication groups for high availability, you pay for each additional node.

Memory requirements planning:

  • Session storage: ~1KB per user session
  • Application caching: Depends on cache hit ratio and object sizes
  • Database query caching: Can range from MBs to GBs depending on query complexity

Cost optimization strategies:

  • Start small; use cache.t4g.micro for development and small applications. Unless you’re using redis as a primary data store or a cache for a large amount of data, there’s a good chance you won’t need anything bigger than that for some time.
  • Monitor memory utilization and scale instance type as needed
  • Set appropriate TTL values to prevent memory leaks
  • Use Redis eviction policies like allkeys-lru for automatic cleanup

See ElastiCache Pricing for current rates.

Creates an ElastiCache Redis cluster with security group, parameter group, and subnet group.

function redis(ctx: Context, args: RedisArgs): RedisOutput
  • ctx (Context) - The context for resource naming and tagging
  • args (RedisArgs) - Configuration arguments for the Redis cluster
  • (RedisOutput) - Creates an ElastiCache Redis cluster with security group, parameter group, and subnet group.

Configuration arguments for creating an ElastiCache Redis cluster.

  • availabilityZone? (Input<string>) - Specific availability zone for the cluster
  • engine? (Input<string>) - ElastiCache engine type (defaults to “redis”)
  • engineVersion? (Input<string>) - Engine version (defaults to “6.x”)
  • network (Network) - The network configuration (VPC and subnets) for the Redis cluster
  • nodeType? (Input<string>) - Instance type for cache nodes (defaults to “cache.t4g.micro”)
  • noPrefix? (boolean) - Whether to skip adding a prefix to the resource name
  • numNodes? (Input<number>) - Number of cache nodes in the cluster (defaults to 1)
  • parameters? (Record<string, Input<string>>) - Custom parameters for the parameter group
  • port? (Input<number>) - Port number for Redis connections (defaults to 6379)
  • sourceSecurityGroupId? (Input<string>) - Security group ID that should be allowed to access the cluster

Output from creating a Redis cluster, containing the instance and connection URL.

  • instance (Cluster) - The ElastiCache cluster resource
  • url (Output<string>) - Connection URL for the Redis cluster