Deploy stacks in parallel with AWS CDK Pipelines

Update AWS CDK Pipelines to run multiple actions in parallel in one stage.

Subscribe to my newsletter and never miss my upcoming articles

The AWS CDK Pipelines is a higher level construct that creates a customized AWS Codepipeline specifically for deploying CDK stacks on AWS Accounts and automatically manages the following:

  • Stack dependency order.
  • Asset publishing.
  • Keeping the pipeline up-to-date as the CDK apps change.
  • Using stack outputs later on in the pipeline.

The following tutorial from the AWS docs helps you with setting up an AWS CDK Pipeline. The default CDK Pipeline contains example code to deploy a stack on a single stage.

But if you have multiple test accounts, then it would be nice to add them to the same stage in AWS CDK Pipeline. To do that, you first have to create a stage and add the first account that you want to deploy an application for:

    // Create a stage called Test
    const testStage = pipeline.addApplicationStage(
      new PipelineStage(this, "Test", {
        // Deploy to test account
        env: {
          account: process.env.TEST_AWS_ACCOUNT, 
          region: process.env.TEST_AWS_REGION,
        },
        vpcCidr: process.env.TEST_VPC_CIDR,
      })
    );

For the deployment of the first application, it does two actions in the stage; "prepare" and "deploy". These are linked to "RunOrder: 1" and "RunOrder: 2" respectively. Next, we need to add the second application that we want to add to the "Test" stage. But before we do we need to update the "RunOrder", otherwise, the actions that are linked to the second application will be triggered sequentially.

    // By subtracting nextSequentialRunOrder with -2 we reset the RunOrder back to 1
    testStage.nextSequentialRunOrder(-2);
    testStage.addApplication(
      new PipelineTGStage(this, "Test-tgw", {
        // Deploy to test transit gateway
        env: {
          account: process.env.TEST_TG_AWS_ACCOUNT,
          region: process.env.TEST_TG_AWS_REGION,
        },
      })
    );

If we commit the code it will update the CDK Pipelines automatically and the updated stage has two simultaneous stack deploys now:

AWS Console Codepipeline stage with multiple actions (stacks)


๐Ÿ‘‹ Liked this article? Follow me on Twitter to stay updated!

Interested in reading more such articles from Danny Steenman?

Support the author by donating an amount of your choice.

Recent sponsors
Catalin Pit's photo

I like this article! Informative, useful, short and to the point!

Danny Steenman's photo

Thanks Catalin Pit! My plan is to make a series of micro blogposts for CDK that answer a question that people would normally look for when they run up against difficulties.

I had this issue myself and couldn't find anything when I searched for it, so here I try to fill the gaps.

Jannik Wempe's photo

This helped me out, thanks!

But I don't really like how it works... if you'll add one action in between, e.g. App.Test, all of a sudden tg-transitgateway.Prepare would run in parallel with App.Deploy, right? Seems brittle to me handling the parallelism with sequential numbers.