redis
View SourceElastiCache 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:
# Connect using redis-cli from an EC2 instance in the same VPCredis-cli -u redis://your-redis-endpoint.cache.amazonaws.com:6379/0
# Test basic operationsSET 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");
Related Components
Section titled “Related Components”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 RAMcache.r7g.large
(~$146.83/month) - 13.07GB RAMcache.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
Parameters
Section titled “Parameters”ctx
(Context
) - The context for resource naming and taggingargs
(RedisArgs
) - Configuration arguments for the Redis cluster
Returns
Section titled “Returns”- (
RedisOutput
) - Creates an ElastiCache Redis cluster with security group, parameter group, and subnet group.
Interfaces
Section titled “Interfaces”RedisArgs
Section titled “RedisArgs”Configuration arguments for creating an ElastiCache Redis cluster.
Properties
Section titled “Properties”availabilityZone?
(Input<string>
) - Specific availability zone for the clusterengine?
(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 clusternodeType?
(Input<string>
) - Instance type for cache nodes (defaults to “cache.t4g.micro”)noPrefix?
(boolean
) - Whether to skip adding a prefix to the resource namenumNodes?
(Input<number>
) - Number of cache nodes in the cluster (defaults to 1)parameters?
(Record<string, Input<string>>
) - Custom parameters for the parameter groupport?
(Input<number>
) - Port number for Redis connections (defaults to 6379)sourceSecurityGroupId?
(Input<string>
) - Security group ID that should be allowed to access the cluster
RedisOutput
Section titled “RedisOutput”Output from creating a Redis cluster, containing the instance and connection URL.
Properties
Section titled “Properties”instance
(Cluster
) - The ElastiCache cluster resourceurl
(Output<string>
) - Connection URL for the Redis cluster