Skip to content

Commit dc9536a

Browse files
authored
feat(core): allow disabling of LogicalID Metadata in case of large manifest (#20387)
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 #20211. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 51d89f8 commit dc9536a

File tree

3 files changed

+90
-6
lines changed

3 files changed

+90
-6
lines changed

packages/@aws-cdk/core/lib/cfn-element.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
2+
import * as cxapi from '@aws-cdk/cx-api';
23
import { Construct, Node } from 'constructs';
34

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

67-
Node.of(this).addMetadata(cxschema.ArtifactMetadataEntryType.LOGICAL_ID, this.logicalId, this.constructor);
68+
if (!this.node.tryGetContext(cxapi.DISABLE_LOGICAL_ID_METADATA)) {
69+
Node.of(this).addMetadata(cxschema.ArtifactMetadataEntryType.LOGICAL_ID, this.logicalId, this.constructor);
70+
}
6871
}
6972

7073
/**

packages/@aws-cdk/core/test/synthesis.test.ts

+80-5
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ describe('synthesis', () => {
3636
},
3737
}),
3838
});
39-
4039
});
4140

4241
test('synthesis respects disabling tree metadata', () => {
@@ -45,7 +44,87 @@ describe('synthesis', () => {
4544
});
4645
const assembly = app.synth();
4746
expect(list(assembly.directory)).toEqual(['cdk.out', 'manifest.json']);
47+
});
48+
49+
test('synthesis respects disabling logicalId metadata', () => {
50+
const app = new cdk.App({
51+
context: { 'aws:cdk:disable-logicalId-metadata': true },
52+
});
53+
const stack = new cdk.Stack(app, 'one-stack');
54+
new cdk.CfnResource(stack, 'MagicResource', { type: 'Resource::Type' });
55+
56+
// WHEN
57+
const session = app.synth();
58+
59+
// THEN
60+
expect(session.manifest).toEqual({
61+
version: cxschema.Manifest.version(),
62+
artifacts: {
63+
'Tree': {
64+
type: 'cdk:tree',
65+
properties: { file: 'tree.json' },
66+
},
67+
'one-stack': {
68+
type: 'aws:cloudformation:stack',
69+
environment: 'aws://unknown-account/unknown-region',
70+
properties: {
71+
templateFile: 'one-stack.template.json',
72+
validateOnSynth: false,
73+
},
74+
displayName: 'one-stack',
75+
// no metadata, because the only entry was a logicalId
76+
},
77+
},
78+
});
79+
});
80+
81+
test('synthesis respects disabling logicalId metadata, and does not disable other metadata', () => {
82+
const app = new cdk.App({
83+
context: { 'aws:cdk:disable-logicalId-metadata': true },
84+
stackTraces: false,
85+
});
86+
const stack = new cdk.Stack(app, 'one-stack', { tags: { boomTag: 'BOOM' } });
87+
new cdk.CfnResource(stack, 'MagicResource', { type: 'Resource::Type' });
88+
89+
// WHEN
90+
const session = app.synth();
4891

92+
// THEN
93+
expect(session.manifest).toEqual({
94+
version: cxschema.Manifest.version(),
95+
artifacts: {
96+
'Tree': {
97+
type: 'cdk:tree',
98+
properties: { file: 'tree.json' },
99+
},
100+
'one-stack': {
101+
type: 'aws:cloudformation:stack',
102+
environment: 'aws://unknown-account/unknown-region',
103+
properties: {
104+
templateFile: 'one-stack.template.json',
105+
validateOnSynth: false,
106+
tags: {
107+
boomTag: 'BOOM',
108+
},
109+
},
110+
displayName: 'one-stack',
111+
metadata: {
112+
'/one-stack': [
113+
{
114+
type: 'aws:cdk:stack-tags',
115+
data: [
116+
{
117+
key: 'boomTag',
118+
value: 'BOOM',
119+
},
120+
],
121+
},
122+
],
123+
},
124+
// no logicalId entry
125+
},
126+
},
127+
});
49128
});
50129

51130
test('single empty stack', () => {
@@ -58,7 +137,6 @@ describe('synthesis', () => {
58137

59138
// THEN
60139
expect(list(session.directory).includes('one-stack.template.json')).toEqual(true);
61-
62140
});
63141

64142
test('some random construct implements "synthesize"', () => {
@@ -112,7 +190,6 @@ describe('synthesis', () => {
112190
},
113191
},
114192
});
115-
116193
});
117194

118195
test('random construct uses addCustomSynthesis', () => {
@@ -172,7 +249,6 @@ describe('synthesis', () => {
172249
},
173250
},
174251
});
175-
176252
});
177253

178254
testDeprecated('it should be possible to synthesize without an app', () => {
@@ -220,7 +296,6 @@ describe('synthesis', () => {
220296
expect(stack.templateFile).toEqual('hey.json');
221297
expect(stack.parameters).toEqual({ paramId: 'paramValue', paramId2: 'paramValue2' });
222298
expect(stack.environment).toEqual({ region: 'us-east-1', account: 'unknown-account', name: 'aws://unknown-account/us-east-1' });
223-
224299
});
225300
});
226301

packages/@aws-cdk/cx-api/lib/app.ts

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export const DISABLE_ASSET_STAGING_CONTEXT = 'aws:cdk:disable-asset-staging';
3939
*/
4040
export const DISABLE_METADATA_STACK_TRACE = 'aws:cdk:disable-stack-trace';
4141

42+
/**
43+
* If this context key is set, the CDK will not store logical ID
44+
* metadata in the manifest.
45+
*/
46+
export const DISABLE_LOGICAL_ID_METADATA = 'aws:cdk:disable-logicalId-metadata';
47+
4248
/**
4349
* Run bundling for stacks specified in this context key
4450
*/

0 commit comments

Comments
 (0)