AWS Database Blog
Deploy Amazon ElastiCache for Redis clusters using AWS CDK and TypeScript
The AWS Cloud Development Kit (AWS CDK) allows you to create AWS resources with a single line of code. For example, you can create a VPC in TypeScript with the following line:
However, several AWS resources require several lines of code because you often need to create supporting resources. For example, you need to create a CfnSubnetGroup
and a SecurityGroup
before creating an Amazon ElastiCache for Redis CfnReplicationGroup.
In this post, we show you all the prerequisites and steps to deploy an Amazon ElastiCache cluster using AWS CDK and TypeScript. We also show you how to deploy resources using Amazon ElastiCache for Redis Serverless.
Solution overview
ElastiCache is a fully managed, Redis- and Memcached-compatible service that delivers real-time, cost- optimized performance, and up to 99.99% Service Level Agreement (SLA) for modern applications. ElastiCache speeds up database and application performance, scaling to hundreds of millions of operations per second with microsecond response times. ElastiCache offers enhanced security, reliability, scalability, and performance when compared to open-source alternatives.
ElastiCache Serverless is a new serverless option that allows you to create a cache in under a minute and instantly scale capacity based on application traffic patterns.
In the following sections, we demonstrate the steps to deploy the prerequisite resources, then to deploy ElastiCache and ElastiCache Serverless resources.
Prerequisites
You need to have the following on your laptop:
- An AWS account.
- The AWS Command Line Interface (AWS CLI) installed.
- The AWS CDK installed. For instructions, refer to Configure the AWS CLI.
- Node.js 16.14.0 or later.
This post assumes you are familiar with AWS networking and security mechanisms. For more information, refer to Networking Essentials and AWS Cloud Security.
Create the prerequisite resources
Before creating the ElastiCache cluster, you need to install the AWS CDK on your local machine, then create the AWS CDK directory structure, install the NPM packages, and create the VPC and the subnet group.
Complete the following steps to create the prerequisite resources.
Install the AWS CDK on your local machine
First, install the AWS CDK on your local machine:
- Enter the following command in a terminal:
- Validate the installation with the following command:
You should get an output like the following:
Create the AWS CDK directory structure
Enter the following commands to create the AWS CDK directory structure:
You should get an output like the following:
Install the NPM packages
Enter the following command to install the NPM packages:
You should get an output like the following:
Create the VPC
Now you’re ready to create your first AWS resource using AWS CDK, which is the VPC. Complete the following steps:
- Using your preferred editor, open the
lib/work-stack.ts
file, erase its contents, and enter the following code:
It’s this straightforward to create a VPC using the AWS CDK.
- Prepare the AWS environment by entering the following command:
You should get an output like the following:
- Create the AWS CloudFormation template by running the following command:
You should see a long output describing the generated CloudFormation template.
- Create the VPC by entering the following command. The
—-require-approval
option bypasses the prompt for approval.
You should get an output like the following:
The preceding command deployed a CloudFormation stack. If you navigate to the AWS CloudFormation console (replace the URL to point to the AWS Region you deployed your stack to), you should see the Events tab and other information related to the deployment.
If you navigate to the Amazon Virtual Private Cloud (Amazon VPC) console, you should see the cache VPC deployed.
Create the subnet group
Now that you have created the VPC, you can create the subnet group for ElastiCache, which determines the Availability Zones that the nodes can be deployed to. Complete the following steps:
- Update the
lib/work-stack.ts
file to add thesubnetGroup
creation code, containing the private subnets:
- Enter the following command to synthesize and deploy the stack:
Create an ElastiCache for Redis replication group
The following steps create an ElastiCache for Redis replication group with cluster mode disabled and a single primary node using AWS CDK.
Create the security group
First, create the security group to be used with ElastiCache. This security group allows any IP address to connect to Redis port (6379).
- Update the
lib/work-stack.ts
file to add the security group:
- Enter the following command to synthesize and deploy the stack:
Create the replication group
Finally, deploy a replication group to create the ElastiCache instance:
- Update the
lib/work-stack.ts
file to add aReplicationGroup
configuration. The following code creates a single-node cluster, which is useful for testing purposes.
- Enter the following command to synthetize and deploy the stack:
Wait 5–7 minutes for the ReplicationGroup
deployment. You should get an output like the following:
Verify the resources
Log in to the ElastiCache console in the Region where you deployed your resources. You should see a Redis cluster.
Deploy ElastiCache for Redis Serverless resources
So far, you have deployed the node-based version of ElastiCache for Redis, where you created the subnet group and security group, and defined the node type and number of nodes.
To deploy the ElastiCache for Redis Serverless version, complete the steps in this section after you create the VPC and the subnet group in the previous sections.
Create the security group
Complete the following steps to create the security group to use with ElastiCache. This security group allows any IP address to connect to the Redis port (6379) and replica port (6380).
- Update the lib/work-stack.ts file to create the security group:
- Enter the following command to synthesize and deploy the stack:
Create a serverless cache
To add the serverless cache, complete the following steps:
- Update the AWS CDK code in the lib/work-stack.ts file:
- Enter the following command to synthesize and deploy the stack:
Verify the resources
On the ElastiCache console, you should see a Redis cluster like the one in the following screenshot.
Clean up
It’s straightforward to clean up the deployed resources, because you deployed them as a single AWS CDK application. Enter the following command:
You should get an output like the following:
Conclusion
In this post, we showed you how to create all the prerequisites and the two versions of ElastiCache for Redis: the provisioned version required multiple steps, such as creating the subnet group and security group; the serverless version was much more straightforward and could be created with a single AWS CDK command.
Let us know in the comments section if you have any suggestions or concerns.
About the Author
Eduardo Patrocinio is a distinguished Principal Solutions Architect on the AWS Strategic Accounts team, bringing unparalleled expertise to the forefront of cloud technology. With an impressive career spanning over 25 years, Eduardo has been a driving force in designing and delivering innovative customer solutions within the dynamic realms of cloud and service management.