bitcoin
bitcoin

$91824.57 USD 

1.38%

ethereum
ethereum

$3156.84 USD 

1.64%

tether
tether

$1.00 USD 

0.03%

solana
solana

$240.30 USD 

-0.71%

bnb
bnb

$620.07 USD 

-1.12%

xrp
xrp

$1.12 USD 

-1.14%

dogecoin
dogecoin

$0.387474 USD 

4.58%

usd-coin
usd-coin

$0.999787 USD 

-0.02%

cardano
cardano

$0.741654 USD 

-0.48%

tron
tron

$0.204080 USD 

0.37%

shiba-inu
shiba-inu

$0.000025 USD 

1.56%

avalanche
avalanche

$35.13 USD 

-1.06%

toncoin
toncoin

$5.59 USD 

1.24%

sui
sui

$3.73 USD 

-3.19%

chainlink
chainlink

$15.01 USD 

5.06%

加密貨幣新聞文章

使用 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日 其他文章發表於