Skip to content

Commit

Permalink
fix(cli): improve asset publishing times by up to 30% (#17409)
Browse files Browse the repository at this point in the history
Part of #17266

Improves the performance of asset publishing by caching the AWS SDK clients that are used, based on which roles need to be assumed. This way, most roles only need to be assumed once per `cdk deploy`, instead once per asset.

In https://github.com/cdklabs/construct-hub, we have a CDK stack that includes 20+ assets (at least 15 of which are Lambda code bundles). I tested out the updated CLI, and it sped up the publishing step (when no assets needed to be changed) from 29.8 seconds to 19.6 seconds.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Chriscbr authored Nov 16, 2021
1 parent 21c2d2b commit 40d6a48
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/aws-cdk/lib/util/asset-publishing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export async function publishAssets(manifest: cdk_assets.AssetManifest, sdk: Sdk
}

class PublishingAws implements cdk_assets.IAws {
private sdkCache: Map<String, ISDK> = new Map();

constructor(
/**
* The base SDK to work with
Expand Down Expand Up @@ -66,16 +68,30 @@ class PublishingAws implements cdk_assets.IAws {
/**
* Get an SDK appropriate for the given client options
*/
private sdk(options: cdk_assets.ClientOptions): Promise<ISDK> {
private async sdk(options: cdk_assets.ClientOptions): Promise<ISDK> {
const env = {
...this.targetEnv,
region: options.region ?? this.targetEnv.region, // Default: same region as the stack
};

return this.aws.forEnvironment(env, Mode.ForWriting, {
const cacheKey = JSON.stringify({
env, // region, name, account
assumeRuleArn: options.assumeRoleArn,
assumeRoleExternalId: options.assumeRoleExternalId,
});

const maybeSdk = this.sdkCache.get(cacheKey);
if (maybeSdk) {
return maybeSdk;
}

const sdk = await this.aws.forEnvironment(env, Mode.ForWriting, {
assumeRoleArn: options.assumeRoleArn,
assumeRoleExternalId: options.assumeRoleExternalId,
});
this.sdkCache.set(cacheKey, sdk);

return sdk;
}
}

Expand Down

0 comments on commit 40d6a48

Please sign in to comment.