|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 部署资源。
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 或更高版本创建必备资源
- Install AWS CDK:
npm install -g aws-cdk安装AWS CDK:npm install -g aws-cdk
cdk --version
- Create AWS CDK Directory Structure:
mkdir work & cd workcdk --versionCreate AWS CDK 目录结构: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:
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');
}}
}
- Bootstrap AWS Environment:
cdk bootstrap
- Synthesize CloudFormation Template:
cdk synth
- Deploy VPC:
cdk deploy --require-approval never
Create Subnet Group
}Bootstrap AWS 环境:cdk bootstrapSynthesize CloudFormation 模板:cdk SynthDeploy VPC:cdk 部署 --require-approval neverCreate 子网组
- 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 子网组”,
}}
}
);
}}
}
- 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
- 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 端口");
}}
}
- 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);
}}
}
- 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
- 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),我们将及时删除。
-
- Tron Bull:具有不可阻挡的看涨能量的 Meme 市场佼佼者
- 2024-11-19 12:10:26
- 迷因币的爆炸式增长是任何加密货币业内人士都无法否认的现象,激发了大众的想象力。
-
- 科技股飙升,比特币、以太坊小幅走高:修正前比特币可能触及 13.8 万美元
- 2024-11-19 12:10:26
- 周一,主要加密货币小幅走高,与科技股飙升同时发生。
-
- CoinEx 交易所:近距离观察
- 2024-11-19 10:20:01
- CoinEx 是一个以其多功能性和易用性而闻名的加密货币交易平台。该平台成立于 2017 年,在数字资产交易领域占据了重要地位。
-
- 错过了卡尔达诺? DTX交易所是未来!
- 2024-11-19 10:20:01
- 随着市场开始反弹,SOL 强大的生态系统和 ADA 的业绩记录继续吸引着投资者。