|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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),我們將及時刪除。
-
- 芝幣對經濟和日常生活的不可言喻的影響
- 2024-11-19 12:30:50
- 隨著 Shiba Coin 作為數位貨幣市場的潛在領導者而登上新聞頭條,它的旅程所帶來的重大影響超出了單純的技術創新。
-
- 柴犬價格上限已近,是時候尋找其他地方了嗎?
- 2024-11-19 12:20:02
- 在比特幣近期創下歷史新高帶動的加密貨幣大漲的推動下,柴犬價格在過去幾個小時內飆升了 37%,達到 0.00002608 美元。
-
- 每件物品都值得聲望令牌,排名
- 2024-11-19 12:20:02
- 選擇聲望令牌物品時的一個很好的經驗法則是考慮它們對您的遊戲玩法有多重要以及需要多長時間才能取回它們。
-
- 比特幣交易技巧:開始之前您應該了解的 6 個基本規則
- 2024-11-19 12:20:02
- 比特幣再次成為新聞焦點——這是有充分理由的,因為這種數位資產的價格已經接近 10 萬美元。
-
- 比特幣熱潮:10萬美元以上是什麼及其對社會的影響
- 2024-11-19 12:20:02
- 隨著加密貨幣市場繼續成為頭條新聞,有一個方面尚未被廣泛報導:這些市場趨勢如何重塑日常生活
-
- Qubetics:2024 年最值得購買的加密貨幣
- 2024-11-19 12:20:02
- 透過將區塊鏈的力量與革命性的代幣化資產市場相結合,Qubetics 正在為下一波數位資產創新浪潮奠定基礎。
-
- 比特幣持倉量近期再創新高
- 2024-11-19 12:20:02
- 數據顯示,比特幣持倉量已再創歷史新高(ATH),顯示比特幣可能會出現更大的波動。