Skip to content

Commit

Permalink
chore(app-staging-synthesizer): reduce the size of DefaultStagingStac…
Browse files Browse the repository at this point in the history
…k 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*
  • Loading branch information
kaizencc committed Jun 20, 2023
1 parent a8768f4 commit 6858edf
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"31.0.0"}
{"version":"32.0.0"}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "31.0.0",
"version": "32.0.0",
"testCases": {
"integ-tests/DefaultTest": {
"stacks": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "31.0.0",
"version": "32.0.0",
"files": {
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
"source": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "31.0.0",
"version": "32.0.0",
"artifacts": {
"synthesize-default-resources.assets": {
"type": "cdk:asset-manifest",
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "31.0.0",
"version": "32.0.0",
"files": {
"68539effc3f7ad46fff9765606c2a01b7f7965833643ab37e62799f19a37f650": {
"source": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -1150,7 +1158,7 @@
}
},
"constructInfo": {
"fqn": "aws-cdk-lib.Stack",
"fqn": "@aws-cdk/app-staging-synthesizer-alpha.DefaultStagingStack",
"version": "0.0.0"
}
},
Expand All @@ -1167,7 +1175,7 @@
"path": "integ-tests/DefaultTest/Default",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.2.26"
"version": "10.2.52"
}
},
"DeployAssert": {
Expand Down Expand Up @@ -1213,7 +1221,7 @@
"path": "Tree",
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.2.26"
"version": "10.2.52"
}
}
},
Expand Down

0 comments on commit 6858edf

Please sign in to comment.