bitcoin
bitcoin

$91285.28 USD 

0.79%

ethereum
ethereum

$3160.88 USD 

1.43%

tether
tether

$1.00 USD 

0.05%

solana
solana

$240.01 USD 

0.53%

bnb
bnb

$619.15 USD 

-1.05%

xrp
xrp

$1.13 USD 

1.17%

dogecoin
dogecoin

$0.374443 USD 

-0.13%

usd-coin
usd-coin

$0.999914 USD 

-0.01%

cardano
cardano

$0.738244 USD 

0.64%

tron
tron

$0.203581 USD 

0.44%

shiba-inu
shiba-inu

$0.000025 USD 

-0.82%

toncoin
toncoin

$5.65 USD 

3.22%

avalanche
avalanche

$35.07 USD 

-0.76%

sui
sui

$3.71 USD 

-3.36%

chainlink
chainlink

$15.03 USD 

5.27%

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 Nov 19, 2024