Market Cap: $4.0771T 0.75%
Volume(24h): $192.0842B 35.52%
  • Market Cap: $4.0771T 0.75%
  • Volume(24h): $192.0842B 35.52%
  • Fear & Greed Index:
  • Market Cap: $4.0771T 0.75%
Cryptos
Topics
Cryptospedia
News
CryptosTopics
Videos
Top News
Cryptos
Topics
Cryptospedia
News
CryptosTopics
Videos
bitcoin
bitcoin

$116671.700731 USD

-0.07%

ethereum
ethereum

$4614.067903 USD

2.14%

xrp
xrp

$3.088291 USD

1.49%

tether
tether

$1.000362 USD

-0.01%

bnb
bnb

$987.229886 USD

2.93%

solana
solana

$245.931058 USD

3.98%

usd-coin
usd-coin

$0.999926 USD

-0.02%

dogecoin
dogecoin

$0.282081 USD

4.73%

cardano
cardano

$0.916372 USD

4.08%

tron
tron

$0.343952 USD

0.28%

hyperliquid
hyperliquid

$58.838953 USD

8.45%

chainlink
chainlink

$23.998618 USD

2.02%

ethena-usde
ethena-usde

$1.001077 USD

-0.02%

avalanche
avalanche

$32.209027 USD

7.08%

sui
sui

$3.800649 USD

5.65%

Cryptocurrency News Articles

Deploy Amazon ElastiCache for Redis clusters using AWS CDK and TypeScript | AWS Database Blog

Apr 05, 2024 at 11:25 pm

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.

Deploy Amazon ElastiCache for Redis clusters using AWS CDK and TypeScript | AWS Database Blog

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

  1. Install AWS CDK:
npm install -g aws-cdk
cdk --version
  1. Create AWS CDK Directory Structure:
mkdir work & cd work
cdk init --language typescript
  1. Install NPM Packages:
npm install
  1. 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');
  }
}
  1. Bootstrap AWS Environment:
cdk bootstrap
  1. Synthesize CloudFormation Template:
cdk synth
  1. Deploy VPC:
cdk deploy --require-approval never

Create Subnet Group

  1. 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",
    });
  }
}
  1. Synthesize and deploy the stack:
cdk synth; cdk deploy --require-approval never

Creating an ElastiCache for Redis Replication Group

  1. 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");
  }
}
  1. 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);
  }
}
  1. Synthesize and deploy the stack:
cdk synth; cdk deploy --require-approval never

Deploying ElastiCache for Redis Serverless Resources

  1. 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",
    });

Disclaimer:info@kdj.com

The information provided is not trading advice. kdj.com does not assume any responsibility for any investments made based on the information provided in this article. Cryptocurrencies are highly volatile and it is highly recommended that you invest with caution after thorough research!

If you believe that the content used on this website infringes your copyright, please contact us immediately (info@kdj.com) and we will delete it promptly.

Other articles published on Sep 18, 2025