1. Answers
  2. Using Amazon ElastiCache Serverless with Redis and Memcached

How do I use Amazon ElastiCache Serverless with Redis and Memcached?

In this guide, we will demonstrate how to set up and use Amazon ElastiCache Serverless with both Redis and Memcached using Pulumi. Amazon ElastiCache is a fully managed in-memory caching service that supports both Redis and Memcached. We will create two serverless cache clusters, one for Redis and one for Memcached.

Key Points

  • We will create a serverless Redis cache cluster.
  • We will create a serverless Memcached cache cluster.
  • We will use Pulumi to define and deploy these resources.

Below is the Pulumi program in TypeScript that accomplishes this:

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create a security group for ElastiCache
const securityGroup = new aws.ec2.SecurityGroup("elasticache-security-group", {
    description: "Security group for ElastiCache",
    ingress: [
        { protocol: "tcp", fromPort: 6379, toPort: 6379, cidrBlocks: ["0.0.0.0/0"] }, // Redis port
        { protocol: "tcp", fromPort: 11211, toPort: 11211, cidrBlocks: ["0.0.0.0/0"] }, // Memcached port
    ],
    egress: [{ protocol: "-1", fromPort: 0, toPort: 0, cidrBlocks: ["0.0.0.0/0"] }],
});

// Create a subnet group for ElastiCache
const subnetGroup = new aws.elasticache.SubnetGroup("elasticache-subnet-group", {
    description: "Subnet group for ElastiCache",
    subnetIds: [/* Add your subnet IDs here */],
});

// Create a serverless Redis cache cluster
const redisCluster = new aws.elasticache.Cluster("redis-cluster", {
    engine: "redis",
    nodeType: "cache.t3.micro",
    numCacheNodes: 1,
    parameterGroupName: "default.redis6.x",
    subnetGroupName: subnetGroup.id,
    securityGroupIds: [securityGroup.id],
});

// Create a serverless Memcached cache cluster
const memcachedCluster = new aws.elasticache.Cluster("memcached-cluster", {
    engine: "memcached",
    nodeType: "cache.t3.micro",
    numCacheNodes: 1,
    parameterGroupName: "default.memcached1.5",
    subnetGroupName: subnetGroup.id,
    securityGroupIds: [securityGroup.id],
});

// Export the Redis and Memcached endpoints
export const redisEndpoint = redisCluster.cacheNodes.apply(nodes => nodes[0].address);
export const memcachedEndpoint = memcachedCluster.cacheNodes.apply(nodes => nodes[0].address);

Conclusion

In this guide, we have demonstrated how to create serverless Redis and Memcached cache clusters using Amazon ElastiCache with Pulumi. We created a security group, a subnet group, and then deployed the cache clusters. Finally, we exported the endpoints for both Redis and Memcached clusters. This setup allows you to leverage the benefits of in-memory caching with minimal management overhead.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up