Skip to content

cli: allow to parallelize asset build #226

@tmokmss

Description

@tmokmss

Describe the feature

Currently in cdk deploy, assets are built serially one by one, making it slow when there are many container image assets.

The current behavior assumes that asset build is CPU-bound and will not get benefit from parallelism:

https://github.com/aws/aws-cdk/blob/bbdd42c8f45916d5c6945f3429916f6199d2ec66/packages/aws-cdk/lib/cdk-toolkit.ts#L559-L563

However, in our use cases, our assets are not fully cpu-bound and will actually get faster when parallelized. That is why I want to configure the concurrency parameter by CLI arguments.

Use Case

When a CDK app contains many docker images whose build process are not fully CPU-bound, we can make cdk deploy faster by parallelizing the asset build processes.

You can see the outcome by the following extreme example:

# docker/Dockerfile
FROM nginx
ARG DUMMY_ARG
RUN echo ${DUMMY_ARG}
# simulating IO-bound image
RUN sleep 10

And this CDK code:

// stack.ts
import * as cdk from 'aws-cdk-lib';
import { DockerImageCode, DockerImageFunction } from 'aws-cdk-lib/aws-lambda';
import { Construct } from 'constructs';

export class SlowDockerParallelTestStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    Array(5)
      .fill(0)
      .map((_, i) => {
        new DockerImageFunction(this, `Function${i}`, {
          code: DockerImageCode.fromImageAsset('./docker', {
            buildArgs: {
              DUMMY_ARG: `${i}_v1`,
            },
          }),
        });
      });
  }
}

When you run cdk deploy, you can see the five images are built serially, requiring more than 50 seconds to finish deployment.

Currently you can configure the parallelism parameter by directly editing node_modules/aws-cdk/lib/index.js, search for {"stack":concurrency,"asset-build":1 and replace it with {"stack":concurrency,"asset-build":5.

After updateing the parameter, run cdk deploy again (make sure to change DUMMY_ARG to invalidate caches), and all the images are built concurrently. It now takes about 10 seconds.

Proposed Solution

Expose CLI argument like asset-build-concurrency. Default is 1, and set the value here.

https://github.com/aws/aws-cdk/blob/bbdd42c8f45916d5c6945f3429916f6199d2ec66/packages/aws-cdk/lib/cdk-toolkit.ts#L559-L563

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.174.1

Environment details (OS name and version, etc.)

macOS

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions