bitcoin
bitcoin

$91374.47 USD 

0.88%

ethereum
ethereum

$3150.67 USD 

1.23%

tether
tether

$1.00 USD 

0.05%

solana
solana

$240.77 USD 

-1.49%

bnb
bnb

$620.02 USD 

-1.30%

xrp
xrp

$1.13 USD 

0.93%

dogecoin
dogecoin

$0.379516 USD 

2.14%

usd-coin
usd-coin

$0.999963 USD 

0.00%

cardano
cardano

$0.748784 USD 

1.15%

tron
tron

$0.204488 USD 

0.51%

shiba-inu
shiba-inu

$0.000025 USD 

-0.30%

avalanche
avalanche

$35.27 USD 

-1.26%

toncoin
toncoin

$5.61 USD 

1.67%

sui
sui

$3.74 USD 

-3.74%

chainlink
chainlink

$15.06 USD 

4.74%

加密货币新闻

使用 AWS CDK 和 TypeScript 部署 Amazon ElastiCache for Redis 集群 | AWS 数据库博客

2024/04/05 23:25

AWS 云开发工具包 (AWS CDK) 允许您使用一行代码创建 AWS 资源。例如,您可以使用以下行在 TypeScript 中创建 VPC:new EC2.Vpc(this, 'cache_vpc');但是,多个 AWS 资源需要多行代码,因为您经常需要创建支持资源。例如,您需要先创建 CfnSubnetGroup 和 SecurityGroup,然后再创建 Amazon ElastiCache for Redis CfnReplicationGroup。本摘要演示了使用 AWS CDK 和 TypeScript 部署 Amazon ElastiCache 集群的步骤。我们还向您展示如何使用 Amazon ElastiCache for Redis Serverless 部署资源。

使用 AWS CDK 和 TypeScript 部署 Amazon ElastiCache for Redis 集群 | AWS 数据库博客

Creating AWS Resources with AWS Cloud Development Kit (AWS CDK) for ElastiCache

使用适用于 ElastiCache 的 AWS 云开发套件 (AWS CDK) 创建 AWS 资源

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.

AWS 云开发套件 (AWS CDK) 使开发人员能够使用熟悉的编程语言(例如 TypeScript)定义和预置 AWS 资源。本文将指导读者完成使用 AWS CDK 和 TypeScript 部署 Amazon ElastiCache 集群和 ElastiCache Serverless 资源的过程。

Prerequisites

先决条件

  • AWS account
  • AWS Command Line Interface (AWS CLI)
  • AWS CDK
  • Node.js 16.14.0 or later

Creating Prerequisite Resources

AWS 账户AWS 命令​​行界面 (AWS CLI)AWS CDKNode.js 16.14.0 或更高版本创建必备资源

  1. Install AWS CDK:
npm install -g aws-cdk

安装AWS CDK:npm install -g aws-cdk

cdk --version
  1. Create AWS CDK Directory Structure:
mkdir work & cd work

cdk --versionCreate AWS CDK 目录结构: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:

cdk init --language typescript安装NPM软件包:npm install创建VPC:在lib/work-stack.ts文件中,创建VPC:

import * as cdk from 'aws-cdk-lib';

从 'aws-cdk-lib' 导入 * 作为 cdk;

import { Construct } from 'constructs';

从“构造”导入{构造};

import * as EC2 from 'aws-cdk-lib/aws-ec2';

从“aws-cdk-lib/aws-ec2”导入 * 作为 EC2;

export class WorkStack extends cdk.Stack {

导出类 WorkStack 扩展 cdk.Stack {

private vpc: EC2.Vpc;

私有vpc:EC2.Vpc;

constructor(scope: Construct, id: string, props?: cdk.StackProps) {

构造函数(范围:构造,id:字符串,道具?:cdk.StackProps){

super(scope, id, props);

超级(范围,ID,道具);

this.vpc = new EC2.Vpc(this, 'cache_vpc');

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

}Bootstrap AWS 环境:cdk bootstrapSynthesize CloudFormation 模板:cdk SynthDeploy VPC:cdk 部署 --require-approval neverCreate 子网组

  1. Update lib/work-stack.ts to create a subnet group for ElastiCache:
import * as cdk from 'aws-cdk-lib';

更新 lib/work-stack.ts 为 ElastiCache 创建子网组:import * as cdk from 'aws-cdk-lib';

从 'aws-cdk-lib' 导入 * 作为 cdk;

import { Construct } from 'constructs';

从“构造”导入{构造};

import * as EC2 from 'aws-cdk-lib/aws-ec2';

从“aws-cdk-lib/aws-ec2”导入 * 作为 EC2;

import { aws_elasticache as ElastiCache } from 'aws-cdk-lib';

从 'aws-cdk-lib' 导入 { aws_elasticache as ElastiCache };

export class WorkStack extends cdk.Stack {

导出类 WorkStack 扩展 cdk.Stack {

private vpc: EC2.Vpc;

私有vpc:EC2.Vpc;

constructor(scope: Construct, id: string, props?: cdk.StackProps) {

构造函数(范围:构造,id:字符串,道具?:cdk.StackProps){

const groupName = "ElastiCacheSubnetGroup";

const 组名称 = "ElastiCacheSubnetGroup";

super(scope, id, props);

超级(范围,ID,道具);

this.vpc = new EC2.Vpc(this, 'cache_vpc');

this.vpc = new EC2.Vpc(this, 'cache_vpc');

const subnetIds = [];

const 子网ID = [];

for (const subnet of this.vpc.privateSubnets) {

for (this.vpc.privateSubnets 的 const 子网) {

console.log("createElastiCache subnet ID: ", subnet.subnetId);

console.log("创建ElastiCache子网ID:",subnet.subnetId);

subnetIds.push(subnet.subnetId);

subnetIds.push(subnet.subnetId);

}

}

}

const subnetGroup = new ElastiCache.CfnSubnetGroup(this, "ElastiCacheSubnetGroup", {

const subnetGroup = new ElastiCache.CfnSubnetGroup(this, "ElastiCacheSubnetGroup", {

cacheSubnetGroupName: groupName,

缓存子网组名:组名,

subnetIds: subnetIds,

子网 ID:子网 ID,

description: "ElastiCache Subnet Group",

描述:“ElastiCache 子网组”,

}

}

}

); }

}

}
  1. Synthesize and deploy the stack:
cdk synth; cdk deploy --require-approval never

Creating an ElastiCache for Redis Replication Group

}综合并部署堆栈:cdk synt; cdk deploy --require-approval never为 Redis 复制组创建 ElastiCache

  1. Create Security Group:
import * as cdk from 'aws-cdk-lib';

创建安全组:import * as cdk from 'aws-cdk-lib';

从 'aws-cdk-lib' 导入 * 作为 cdk;

import { Construct } from 'constructs';

从“构造”导入{构造};

import * as EC2 from 'aws-cdk-lib/aws-ec2';

从“aws-cdk-lib/aws-ec2”导入 * 作为 EC2;

import { aws_elasticache as ElastiCache } from 'aws-cdk-lib';

从 'aws-cdk-lib' 导入 { aws_elasticache as ElastiCache };

import { SecurityGroup, Peer, Port } from 'aws-cdk-lib/aws-ec2';

从 'aws-cdk-lib/aws-ec2' 导入 { SecurityGroup, Peer, Port };

export class WorkStack extends cdk.Stack {

导出类 WorkStack 扩展 cdk.Stack {

private vpc: EC2.Vpc;

私有vpc:EC2.Vpc;

constructor(scope: Construct, id: string, props?: cdk.StackProps) {

构造函数(范围:构造,id:字符串,道具?:cdk.StackProps){

const groupName = "ElastiCacheSubnetGroup";

const 组名称 = "ElastiCacheSubnetGroup";

super(scope, id, props);

超级(范围,ID,道具);

this.vpc = new EC2.Vpc(this, 'cache_vpc');

this.vpc = new EC2.Vpc(this, 'cache_vpc');

const subnetIds = [];

const 子网ID = [];

for (const subnet of this.vpc.privateSubnets) {

for (this.vpc.privateSubnets 的 const 子网) {

console.log("createElastiCache subnet ID: ", subnet.subnetId);

console.log("创建ElastiCache子网ID:",subnet.subnetId);

subnetIds.push(subnet.subnetId);

subnetIds.push(subnet.subnetId);

}

}

}

const subnetGroup = new ElastiCache.CfnSubnetGroup(this, "ElastiCacheSubnetGroup", {

const subnetGroup = new ElastiCache.CfnSubnetGroup(this, "ElastiCacheSubnetGroup", {

cacheSubnetGroupName: groupName,

缓存子网组名:组名,

subnetIds: subnetIds,

子网 ID:子网 ID,

description: "ElastiCache Subnet Group",

描述:“ElastiCache 子网组”,

}

}

}

); const securityGroup = new SecurityGroup(this, "ElastiCacheSecurityGroup", {

const securityGroup = new SecurityGroup(this, "ElastiCacheSecurityGroup", {

vpc: this.vpc,

vpc:这个.vpc,

allowAllOutbound: true,

允许所有出站:true,

description: "ElastiCache Security Group",

描述:“ElastiCache 安全组”,

securityGroupName: "ElastiCacheSecurityGroup",

安全组名称:“ElastiCacheSecurityGroup”,

}

}

}

); securityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(6379), "Redis port");

securityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(6379), "Redis 端口");

}

}

}
  1. Create Replication Group:
import * as cdk from 'aws-cdk-lib';

}创建复制组:import * as cdk from 'aws-cdk-lib';

从 'aws-cdk-lib' 导入 * 作为 cdk;

import { Construct } from 'constructs';

从“构造”导入{构造};

import * as EC2 from 'aws-cdk-lib/aws-ec2';

从“aws-cdk-lib/aws-ec2”导入 * 作为 EC2;

import { aws_elasticache as ElastiCache } from 'aws-cdk-lib';

从 'aws-cdk-lib' 导入 { aws_elasticache as ElastiCache };

import { SecurityGroup, Peer, Port } from 'aws-cdk-lib/aws-ec2';

从 'aws-cdk-lib/aws-ec2' 导入 { SecurityGroup, Peer, Port };

export class WorkStack extends cdk.Stack {

导出类 WorkStack 扩展 cdk.Stack {

private vpc: EC2.Vpc;

私有vpc:EC2.Vpc;

constructor(scope: Construct, id: string, props?: cdk.StackProps) {

构造函数(范围:构造,id:字符串,道具?:cdk.StackProps){

const groupName = "ElastiCacheSubnetGroup";

const 组名称 = "ElastiCacheSubnetGroup";

super(scope, id, props);

超级(范围,ID,道具);

this.vpc = new EC2.Vpc(this, 'cache_vpc');

this.vpc = new EC2.Vpc(this, 'cache_vpc');

const subnetIds = [];

const 子网ID = [];

for (const subnet of this.vpc.privateSubnets) {

for (this.vpc.privateSubnets 的 const 子网) {

console.log("createElastiCache subnet ID: ", subnet.subnetId);

console.log("创建ElastiCache子网ID:",subnet.subnetId);

subnetIds.push(subnet.subnetId);

subnetIds.push(subnet.subnetId);

}

}

}

const subnetGroup = new ElastiCache.CfnSubnetGroup(this, "ElastiCacheSubnetGroup", {

const subnetGroup = new ElastiCache.CfnSubnetGroup(this, "ElastiCacheSubnetGroup", {

cacheSubnetGroupName: groupName,

缓存子网组名:组名,

subnetIds: subnetIds,

子网 ID:子网 ID,

description: "ElastiCache Subnet Group",

描述:“ElastiCache 子网组”,

}

}

}

); const securityGroup = new SecurityGroup(this, "ElastiCacheSecurityGroup", {

const securityGroup = new SecurityGroup(this, "ElastiCacheSecurityGroup", {

vpc: this.vpc,

vpc:这个.vpc,

allowAllOutbound: true,

允许所有出站:true,

description: "ElastiCache Security Group",

描述:“ElastiCache 安全组”,

securityGroupName: "ElastiCacheSecurityGroup",

安全组名称:“ElastiCacheSecurityGroup”,

}

}

}

); securityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(6379), "Redis port");

securityGroup.addIngressRule(Peer.anyIpv4(), Port.tcp(6379), "Redis 端口");

const cache = new ElastiCache.CfnReplicationGroup(this, "ReplicationGroup", {

const 缓存 = new ElastiCache.CfnReplicationGroup(this, "ReplicationGroup", {

replicationGroupDescription: "Elastic Cache Replication Group",

replicationGroupDescription: "弹性缓存复制组",

numCacheClusters: 1,

缓存集群数量:1,

automaticFailoverEnabled: false,

自动故障转移启用:假,

engine: 'redis',

引擎:'redis',

cacheNodeType: 'cache.m7g.large',

缓存节点类型: 'cache.m7g.large',

cacheSubnetGroupName: subnetGroup.ref,

缓存子网组名称:subnetGroup.ref,

securityGroupIds: [securityGroup.securityGroupId],

securityGroupIds: [securityGroup.securityGroupId],

}

}

}

); // Establish dependency between cache and subnetGroup

// 建立cache和subnetGroup之间的依赖关系

cache.addDependency(subnetGroup);

缓存.addDependency(subnetGroup);

}

}

}
  1. Synthesize and deploy the stack:
cdk synth; cdk deploy --require-approval never

Deploying ElastiCache for Redis Serverless Resources

}综合并部署堆栈:cdk synt; cdk deploy --require-approval never为 Redis 无服务器资源部署 ElastiCache

  1. Create Security Group:
import * as cdk from 'aws-cdk-lib';

创建安全组:import * as cdk from 'aws-cdk-lib';

从 'aws-cdk-lib' 导入 * 作为 cdk;

import { Construct } from 'constructs';

从“构造”导入{构造};

import * as EC2 from 'aws-cdk-lib/aws-ec2';

从“aws-cdk-lib/aws-ec2”导入 * 作为 EC2;

import { aws_elasticache as ElastiCache } from 'aws-cdk-lib';

从 'aws-cdk-lib' 导入 { aws_elasticache as ElastiCache };

import { SecurityGroup, Peer, Port } from 'aws-cdk-lib/aws-ec2';

从 'aws-cdk-lib/aws-ec2' 导入 { SecurityGroup, Peer, Port };

export class WorkStack extends cdk.Stack {

导出类 WorkStack 扩展 cdk.Stack {

private vpc: EC2.Vpc;

私有vpc:EC2.Vpc;

constructor(scope: Construct, id: string, props?: cdk.StackProps) {

构造函数(范围:构造,id:字符串,道具?:cdk.StackProps){

const groupName = "ElastiCacheSubnetGroup";

const 组名称 = "ElastiCacheSubnetGroup";

super(scope, id, props);

超级(范围,ID,道具);

this.vpc = new EC2.Vpc(this, 'cache_vpc');

this.vpc = new EC2.Vpc(this, 'cache_vpc');

const subnetIds = [];

const 子网ID = [];

for (const subnet of this.vpc.privateSubnets) {

for (this.vpc.privateSubnets 的 const 子网) {

console.log("createElastiCache subnet ID: ", subnet.subnetId);

console.log("创建ElastiCache子网ID:",subnet.subnetId);

subnetIds.push(subnet.subnetId);

subnetIds.push(subnet.subnetId);

}

}

}

const subnetGroup = new ElastiCache.CfnSubnetGroup(this, "ElastiCacheSubnetGroup", {

const subnetGroup = new ElastiCache.CfnSubnetGroup(this, "ElastiCacheSubnetGroup", {

cacheSubnetGroupName: groupName,

缓存子网组名:组名,

subnetIds: subnetIds,

子网 ID:子网 ID,

description: "ElastiCache Subnet Group",

描述:“ElastiCache 子网组”,

}

}

}

); const securityGroup = new SecurityGroup(this, "ElastiCacheSecurityGroup", {

const securityGroup = new SecurityGroup(this, "ElastiCacheSecurityGroup", {

vpc: this.vpc,

vpc:这个.vpc,

allowAllOutbound: true,

允许所有出站:true,

description: "ElastiCache Security Group",

描述:“ElastiCache 安全组”,

securityGroupName: "ElastiCacheSecurityGroup",

安全组名称:“ElastiCacheSecurityGroup”,

}

}

}

);

免责声明:info@kdj.com

所提供的信息并非交易建议。根据本文提供的信息进行的任何投资,kdj.com不承担任何责任。加密货币具有高波动性,强烈建议您深入研究后,谨慎投资!

如您认为本网站上使用的内容侵犯了您的版权,请立即联系我们(info@kdj.com),我们将及时删除。

2024年11月19日 发表的其他文章