From 6858edf2344f2e2a125151f1599106825264da12 Mon Sep 17 00:00:00 2001 From: Kaizen Conroy <36202692+kaizencc@users.noreply.github.com> Date: Tue, 20 Jun 2023 17:55:14 -0400 Subject: [PATCH] chore(app-staging-synthesizer): reduce the size of DefaultStagingStack template by 5KB (#26024) Since `DefaultStagingStack` is a stack that creates staging resources, it is bound to the CloudFormation [limit](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-limits.html) of 51KB. This means that there is an upper limit on how many ECR repositories (and in turn, unique images) are allowed in the CDK App. In an effort to increase this limit, this PR does the following: - Remove path metadata to save ~2KB - Remove `AWS::CDK::Metadata` construct to save ~3KB - Insert a dummy `UsingDefaultStagingStack` construct to the stacks that _use_ the `DefaultStagingStack` for analytics purposes. In all, this should decrease the size of the DefaultStagingStack template by ~5KB, or ~10% of the overall size allowed. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../lib/default-staging-stack.ts | 25 +++++++++++++++++++ .../test/app-staging-synthesizer.test.ts | 6 +++++ .../cdk.out | 2 +- .../integ.json | 2 +- ...efaultTestDeployAssert44C8D370.assets.json | 2 +- .../manifest.json | 11 +++++++- .../synthesize-default-resources.assets.json | 2 +- .../tree.json | 14 ++++++++--- 8 files changed, 56 insertions(+), 8 deletions(-) diff --git a/packages/@aws-cdk/app-staging-synthesizer-alpha/lib/default-staging-stack.ts b/packages/@aws-cdk/app-staging-synthesizer-alpha/lib/default-staging-stack.ts index 9e75755f344c6..86699ab115805 100644 --- a/packages/@aws-cdk/app-staging-synthesizer-alpha/lib/default-staging-stack.ts +++ b/packages/@aws-cdk/app-staging-synthesizer-alpha/lib/default-staging-stack.ts @@ -17,11 +17,27 @@ import { StackProps, } from 'aws-cdk-lib/core'; import { StringSpecializer } from 'aws-cdk-lib/core/lib/helpers-internal'; +import * as cxapi from 'aws-cdk-lib/cx-api'; +import { Construct } from 'constructs'; import { BootstrapRole } from './bootstrap-roles'; import { FileStagingLocation, IStagingResources, IStagingResourcesFactory, ImageStagingLocation } from './staging-stack'; export const DEPLOY_TIME_PREFIX = 'deploy-time/'; +/** + * This is a dummy construct meant to signify that a stack is utilizing + * the AppStagingSynthesizer. It does not do anything, and is not meant + * to be created on its own. This construct will be a part of the + * construct tree only and not the Cfn template. The construct tree is + * then encoded in the AWS::CDK::Metadata resource of the stack and + * injested in our metrics like every other construct. + */ +export class UsingAppStagingSynthesizer extends Construct { + constructor(scope: Construct, id: string) { + super(scope, id); + } +} + /** * User configurable options to the DefaultStagingStack. */ @@ -128,6 +144,12 @@ export class DefaultStagingStack extends Stack implements IStagingResources { throw new Error(`Stack ${stack.stackName} must be part of an App`); } + // Because we do not keep metrics in the DefaultStagingStack, we will inject + // a dummy construct into the stack using the DefaultStagingStack instead. + if (cxapi.ANALYTICS_REPORTING_ENABLED_CONTEXT) { + new UsingAppStagingSynthesizer(stack, `UsingAppStagingSynthesizer/${stack.stackName}`); + } + const stackId = `StagingStack-${appId}-${context.environmentString}`; return new DefaultStagingStack(app, stackId, { ...options, @@ -199,7 +221,10 @@ export class DefaultStagingStack extends Stack implements IStagingResources { super(scope, id, { ...props, synthesizer: new BootstraplessSynthesizer(), + analyticsReporting: false, // removing AWS::CDK::Metadata construct saves ~3KB }); + // removing path metadata saves ~2KB + this.node.setContext(cxapi.PATH_METADATA_ENABLE_CONTEXT, false); this.appId = this.validateAppId(props.appId); this.dependencyStack = this; diff --git a/packages/@aws-cdk/app-staging-synthesizer-alpha/test/app-staging-synthesizer.test.ts b/packages/@aws-cdk/app-staging-synthesizer-alpha/test/app-staging-synthesizer.test.ts index 28ea67de70ef0..f71114c2888c6 100644 --- a/packages/@aws-cdk/app-staging-synthesizer-alpha/test/app-staging-synthesizer.test.ts +++ b/packages/@aws-cdk/app-staging-synthesizer-alpha/test/app-staging-synthesizer.test.ts @@ -121,6 +121,12 @@ describe(AppStagingSynthesizer, () => { expect(depStack.stackName).toEqual(`StagingStack-${APP_ID}`); }); + test('stack has dummy construct for metrics', () => { + // WHEN + const dummyConstruct = stack.node.tryFindChild(`UsingAppStagingSynthesizer/${stack.stackName}`); + expect(dummyConstruct).toBeDefined(); + }); + test('add file asset', () => { // WHEN const location = stack.synthesizer.addFileAsset({ diff --git a/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/cdk.out b/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/cdk.out index 7925065efbcc4..f0b901e7c06e5 100644 --- a/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/cdk.out +++ b/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/cdk.out @@ -1 +1 @@ -{"version":"31.0.0"} \ No newline at end of file +{"version":"32.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/integ.json b/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/integ.json index 9eeaef0dfe700..7a9fc1a5f4be8 100644 --- a/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/integ.json +++ b/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/integ.json @@ -1,5 +1,5 @@ { - "version": "31.0.0", + "version": "32.0.0", "testCases": { "integ-tests/DefaultTest": { "stacks": [ diff --git a/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/integtestsDefaultTestDeployAssert44C8D370.assets.json b/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/integtestsDefaultTestDeployAssert44C8D370.assets.json index 7526fee9ff76c..a8e3e80a7e7fa 100644 --- a/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/integtestsDefaultTestDeployAssert44C8D370.assets.json +++ b/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/integtestsDefaultTestDeployAssert44C8D370.assets.json @@ -1,5 +1,5 @@ { - "version": "31.0.0", + "version": "32.0.0", "files": { "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { "source": { diff --git a/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/manifest.json b/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/manifest.json index 95fbe49a0ecb9..b19112cce4b58 100644 --- a/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/manifest.json +++ b/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/manifest.json @@ -1,5 +1,5 @@ { - "version": "31.0.0", + "version": "32.0.0", "artifacts": { "synthesize-default-resources.assets": { "type": "cdk:asset-manifest", @@ -78,6 +78,15 @@ "type": "aws:cdk:logicalId", "data": "lambdaecr2615DAF68" } + ], + "UsingDefaultStagingStacksynthesizedefaultresources": [ + { + "type": "aws:cdk:logicalId", + "data": "UsingDefaultStagingStacksynthesizedefaultresources", + "trace": [ + "!!DESTRUCTIVE_CHANGES: WILL_DESTROY" + ] + } ] }, "displayName": "synthesize-default-resources" diff --git a/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/synthesize-default-resources.assets.json b/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/synthesize-default-resources.assets.json index 7fb937ee99e39..5be26263eb8bd 100644 --- a/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/synthesize-default-resources.assets.json +++ b/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/synthesize-default-resources.assets.json @@ -1,5 +1,5 @@ { - "version": "31.0.0", + "version": "32.0.0", "files": { "68539effc3f7ad46fff9765606c2a01b7f7965833643ab37e62799f19a37f650": { "source": { diff --git a/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/tree.json b/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/tree.json index cafa81ab2083c..346aa11c699fe 100644 --- a/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/tree.json +++ b/packages/@aws-cdk/app-staging-synthesizer-alpha/test/integ.synth-default-resources.js.snapshot/tree.json @@ -8,6 +8,14 @@ "id": "synthesize-default-resources", "path": "synthesize-default-resources", "children": { + "UsingAppStagingSynthesizer--synthesize-default-resources": { + "id": "UsingAppStagingSynthesizer--synthesize-default-resources", + "path": "synthesize-default-resources/UsingAppStagingSynthesizer--synthesize-default-resources", + "constructInfo": { + "fqn": "@aws-cdk/app-staging-synthesizer-alpha.UsingAppStagingSynthesizer", + "version": "0.0.0" + } + }, "lambda-s3": { "id": "lambda-s3", "path": "synthesize-default-resources/lambda-s3", @@ -1150,7 +1158,7 @@ } }, "constructInfo": { - "fqn": "aws-cdk-lib.Stack", + "fqn": "@aws-cdk/app-staging-synthesizer-alpha.DefaultStagingStack", "version": "0.0.0" } }, @@ -1167,7 +1175,7 @@ "path": "integ-tests/DefaultTest/Default", "constructInfo": { "fqn": "constructs.Construct", - "version": "10.2.26" + "version": "10.2.52" } }, "DeployAssert": { @@ -1213,7 +1221,7 @@ "path": "Tree", "constructInfo": { "fqn": "constructs.Construct", - "version": "10.2.26" + "version": "10.2.52" } } },