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:new EC2.Vpc(this, 'cache_vpc');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. This abstract demonstrates the 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.
Creating AWS Resources with AWS Cloud Development Kit (AWS CDK) for ElastiCache
Introduction
AWS Cloud Development Kit (AWS CDK) enables developers to define and provision AWS resources using familiar programming languages such as TypeScript. This article guides readers through the process of deploying an Amazon ElastiCache cluster and ElastiCache Serverless resources using AWS CDK and TypeScript.
Prerequisites
- AWS account
- AWS Command Line Interface (AWS CLI)
- AWS CDK
- Node.js 16.14.0 or later
Creating Prerequisite Resources
- Install AWS CDK:
npm install -g aws-cdk
cdk --version
- Create AWS CDK Directory Structure:
mkdir work & cd work
cdk init --language typescript
- Install NPM Packages:
npm install
- Create VPC:
In the lib/work-stack.ts
file, create a VPC:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as EC2 from 'aws-cdk-lib/aws-ec2';
export class WorkStack extends cdk.Stack {
private vpc: EC2.Vpc;
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
this.vpc = new EC2.Vpc(this, 'cache_vpc');
}
}
- Bootstrap AWS Environment:
cdk bootstrap
- Synthesize CloudFormation Template:
cdk synth
- Deploy VPC:
cdk deploy --require-approval never
Create Subnet Group
- Update
lib/work-stack.ts
to create a subnet group for ElastiCache:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as EC2 from 'aws-cdk-lib/aws-ec2';
import { aws_elasticache as ElastiCache } from 'aws-cdk-lib';
export class WorkStack extends cdk.Stack {
private vpc: EC2.Vpc;
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
const groupName = "ElastiCacheSubnetGroup";
super(scope, id, props);
this.vpc = new EC2.Vpc(this, 'cache_vpc');
const subnetIds = [];
for (const subnet of this.vpc.privateSubnets) {
console.log("createElastiCache subnet ID: ", subnet.subnetId);
subnetIds.push(subnet.subnetId);
}
const subnetGroup = new ElastiCache.CfnSubnetGroup(this, "ElastiCacheSubnetGroup", {
cacheSubnetGroupName: groupName,
subnetIds: subnetIds,
description: "ElastiCache Subnet Group",
});
}
}
- Synthesize and deploy the stack:
cdk synth; cdk deploy --require-approval never
Creating an ElastiCache for Redis Replication Group
- Create Security Group:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as EC2 from 'aws-cdk-lib/aws-ec2';
import { aws_elasticache as ElastiCache } from 'aws-cdk-lib';
import { SecurityGroup, Peer, Port } from 'aws-cdk-lib/aws-ec2';
export class WorkStack extends cdk.Stack {
private vpc: EC2.Vpc;
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
const groupName = "ElastiCacheSubnetGroup";
super(scope, id, props);
this.vpc = new EC2.Vpc(this, 'cache_vpc');
const subnetIds = [];
for (const subnet of this.vpc.privateSubnets) {
console.log("createElastiCache subnet ID: ", subnet.subnetId);
subnetIds.push(subnet.subnetId);
}
const subnetGroup = new ElastiCache.CfnSubnetGroup(this, "ElastiCacheSubnetGroup", {
cacheSubnetGroupName: groupName,
subnetIds: subnetIds,
description: "ElastiCache Subnet Group",
});
const securityGroup = new SecurityGroup(this, "ElastiCacheSecurityGroup", {
vpc: this.vpc,
allowAllOutbound: true,
description: "ElastiCache Security Group",
securityGroupName: "ElastiCacheSecurityGroup",
});
securityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(6379), "Redis port");
}
}
- Create Replication Group:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as EC2 from 'aws-cdk-lib/aws-ec2';
import { aws_elasticache as ElastiCache } from 'aws-cdk-lib';
import { SecurityGroup, Peer, Port } from 'aws-cdk-lib/aws-ec2';
export class WorkStack extends cdk.Stack {
private vpc: EC2.Vpc;
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
const groupName = "ElastiCacheSubnetGroup";
super(scope, id, props);
this.vpc = new EC2.Vpc(this, 'cache_vpc');
const subnetIds = [];
for (const subnet of this.vpc.privateSubnets) {
console.log("createElastiCache subnet ID: ", subnet.subnetId);
subnetIds.push(subnet.subnetId);
}
const subnetGroup = new ElastiCache.CfnSubnetGroup(this, "ElastiCacheSubnetGroup", {
cacheSubnetGroupName: groupName,
subnetIds: subnetIds,
description: "ElastiCache Subnet Group",
});
const securityGroup = new SecurityGroup(this, "ElastiCacheSecurityGroup", {
vpc: this.vpc,
allowAllOutbound: true,
description: "ElastiCache Security Group",
securityGroupName: "ElastiCacheSecurityGroup",
});
securityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(6379), "Redis port");
const cache = new ElastiCache.CfnReplicationGroup(this, "ReplicationGroup", {
replicationGroupDescription: "Elastic Cache Replication Group",
numCacheClusters: 1,
automaticFailoverEnabled: false,
engine: 'redis',
cacheNodeType: 'cache.m7g.large',
cacheSubnetGroupName: subnetGroup.ref,
securityGroupIds: [securityGroup.securityGroupId],
});
// Establish dependency between cache and subnetGroup
cache.addDependency(subnetGroup);
}
}
- Synthesize and deploy the stack:
cdk synth; cdk deploy --require-approval never
Deploying ElastiCache for Redis Serverless Resources
- Create Security Group:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as EC2 from 'aws-cdk-lib/aws-ec2';
import { aws_elasticache as ElastiCache } from 'aws-cdk-lib';
import { SecurityGroup, Peer, Port } from 'aws-cdk-lib/aws-ec2';
export class WorkStack extends cdk.Stack {
private vpc: EC2.Vpc;
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
const groupName = "ElastiCacheSubnetGroup";
super(scope, id, props);
this.vpc = new EC2.Vpc(this, 'cache_vpc');
const subnetIds = [];
for (const subnet of this.vpc.privateSubnets) {
console.log("createElastiCache subnet ID: ", subnet.subnetId);
subnetIds.push(subnet.subnetId);
}
const subnetGroup = new ElastiCache.CfnSubnetGroup(this, "ElastiCacheSubnetGroup", {
cacheSubnetGroupName: groupName,
subnetIds: subnetIds,
description: "ElastiCache Subnet Group",
});
const securityGroup = new SecurityGroup(this, "ElastiCacheSecurityGroup", {
vpc: this.vpc,
allowAllOutbound: true,
description: "ElastiCache Security Group",
securityGroupName: "ElastiCacheSecurityGroup",
});