amazon-web-servicesaws-cloudformationaws-cdk

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?


Solution

  • 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 });
    

    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"
    ]