Skip to content

Commit

Permalink
feat(core): allow disabling of LogicalID Metadata in case of large m…
Browse files Browse the repository at this point in the history
…anifest (aws#20433)

Users have encountered an error resulting from the manifest being too large to stringify. This allows users to prevent this metadata from ever being added to the manifest. Fixes the issue that caused the previous iteration of this PR to fail in the v2 pipeline.

Fixes aws#20211.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
comcalvi authored and wphilipw committed May 23, 2022
1 parent aa6852c commit 6cd8af7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
5 changes: 4 additions & 1 deletion packages/@aws-cdk/core/lib/cfn-element.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import * as cxapi from '@aws-cdk/cx-api';
import { Construct, Node } from 'constructs';

// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch.
Expand Down Expand Up @@ -64,7 +65,9 @@ export abstract class CfnElement extends CoreConstruct {
displayHint: `${notTooLong(Node.of(this).path)}.LogicalID`,
});

Node.of(this).addMetadata(cxschema.ArtifactMetadataEntryType.LOGICAL_ID, this.logicalId, this.constructor);
if (!this.node.tryGetContext(cxapi.DISABLE_LOGICAL_ID_METADATA)) {
Node.of(this).addMetadata(cxschema.ArtifactMetadataEntryType.LOGICAL_ID, this.logicalId, this.constructor);
}
}

/**
Expand Down
51 changes: 46 additions & 5 deletions packages/@aws-cdk/core/test/synthesis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as os from 'os';
import * as path from 'path';
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
import * as cxapi from '@aws-cdk/cx-api';
import * as cdk from '../lib';

function createModernApp() {
Expand Down Expand Up @@ -36,7 +37,6 @@ describe('synthesis', () => {
},
}),
});

});

test('synthesis respects disabling tree metadata', () => {
Expand All @@ -45,7 +45,52 @@ describe('synthesis', () => {
});
const assembly = app.synth();
expect(list(assembly.directory)).toEqual(['cdk.out', 'manifest.json']);
});

test('synthesis respects disabling logicalId metadata', () => {
const app = new cdk.App({
context: {
[cxapi.DISABLE_LOGICAL_ID_METADATA]: true,
},
});
const stack = new cdk.Stack(app, 'one-stack');
new cdk.CfnResource(stack, 'MagicResource', { type: 'Resource::Type' });

// WHEN
const session = app.synth();

// THEN
expect(Object.keys((session.manifest.artifacts ?? {})['one-stack'])).not.toContain('metadata');
});

test('synthesis respects disabling logicalId metadata, and does not disable other metadata', () => {
const app = new cdk.App({
context: {
[cxapi.DISABLE_LOGICAL_ID_METADATA]: true,
},
stackTraces: false,
});
const stack = new cdk.Stack(app, 'one-stack', { tags: { boomTag: 'BOOM' } });
new cdk.CfnResource(stack, 'MagicResource', { type: 'Resource::Type' });

// WHEN
const session = app.synth();

// THEN
expect(session.manifest.artifacts?.['one-stack'].metadata).toEqual({
'/one-stack': [
{
type: 'aws:cdk:stack-tags',
data: [
{
key: 'boomTag',
value: 'BOOM',
},
],
},
],
// no logicalId entry
});
});

test('single empty stack', () => {
Expand All @@ -58,7 +103,6 @@ describe('synthesis', () => {

// THEN
expect(list(session.directory).includes('one-stack.template.json')).toEqual(true);

});

test('some random construct implements "synthesize"', () => {
Expand Down Expand Up @@ -112,7 +156,6 @@ describe('synthesis', () => {
},
},
});

});

test('random construct uses addCustomSynthesis', () => {
Expand Down Expand Up @@ -172,7 +215,6 @@ describe('synthesis', () => {
},
},
});

});

testDeprecated('it should be possible to synthesize without an app', () => {
Expand Down Expand Up @@ -220,7 +262,6 @@ describe('synthesis', () => {
expect(stack.templateFile).toEqual('hey.json');
expect(stack.parameters).toEqual({ paramId: 'paramValue', paramId2: 'paramValue2' });
expect(stack.environment).toEqual({ region: 'us-east-1', account: 'unknown-account', name: 'aws://unknown-account/us-east-1' });

});
});

Expand Down
6 changes: 6 additions & 0 deletions packages/@aws-cdk/cx-api/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ export const DISABLE_ASSET_STAGING_CONTEXT = 'aws:cdk:disable-asset-staging';
*/
export const DISABLE_METADATA_STACK_TRACE = 'aws:cdk:disable-stack-trace';

/**
* If this context key is set, the CDK will not store logical ID
* metadata in the manifest.
*/
export const DISABLE_LOGICAL_ID_METADATA = 'aws:cdk:disable-logicalId-metadata';

/**
* Run bundling for stacks specified in this context key
*/
Expand Down

0 comments on commit 6cd8af7

Please sign in to comment.