简体   繁体   中英

How to deploy the same stack across multiple regions using AWS CDK

I'm new to AWS CDK . It looks like we can cdk bootstrap and provide multiple regions:

cdk bootstrap {accountID}/us-east1 {accountID}/us-west1

This creates buckets and roles in each region that are needed to deploy a stack (let's call it TestStack ).

What I'd like to do is deploy the same stack (aka TestStack ) in each region. Is this possible with a single cdk deploy command?

So I tried this and it worked for me, in this example I deployed a dynamodb in us-east-1 and north-east-1 region:-

lib/cdk-stackoverflow-stack.ts

import { CfnOutput, Duration, Stack, StackProps,RemovalPolicy } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { aws_dynamodb as db } from 'aws-cdk-lib' 

export class CdkStackoverflowStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);
 
    const table = new db.Table(this, 'Table', {
     removalPolicy: RemovalPolicy.DESTROY,
     partitionKey: { name: 'id', type: db.AttributeType.STRING },
    });

  }
}

bin/cdk-stackoverflow.ts file

#!/usr/bin/env node
import * as cdk from 'aws-cdk-lib';
import { CdkStackoverflowStack } from '../lib/cdk-stackoverflow-stack';

const tokyoStack = { region: 'ap-northeast-1' };
const usStack = { region: 'us-east-1' };
const app = new cdk.App();
new CdkStackoverflowStack(app, 'CdkStackoverflowStack1', { env: tokyoStack });
new CdkStackoverflowStack(app, 'CdkStackoverflowStack2', { env: usStack });
  • Bootstrap all the region where you need to deploy the stack cdk bootstrap aws://accountId/us-east-1

  • then deploy using --all options since it it has multiple stack

Command I used for confirmation

aws cloudformation list-stacks \
  --query 'StackSummaries[?contains(StackName, `CdkStackoverflowStack`)].StackName' \
  --stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE \
  --region ap-northeast-1

 //output

[
    
    "CdkStackoverflowStack1"
]

aws cloudformation list-stacks \
  --query 'StackSummaries[?contains(StackName, `CdkStackoverflowStack`)].StackName' \
  --stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE \
  --region us-east-1 

//output 

[
    "CdkStackoverflowStack2"
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM