Skip to content

Commit 88ea829

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

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-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

+46-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as os from 'os';
33
import * as path from 'path';
44
import { testDeprecated } from '@aws-cdk/cdk-build-tools';
55
import * as cxschema from '@aws-cdk/cloud-assembly-schema';
6+
import * as cxapi from '@aws-cdk/cx-api';
67
import * as cdk from '../lib';
78

89
function createModernApp() {
@@ -36,7 +37,6 @@ describe('synthesis', () => {
3637
},
3738
}),
3839
});
39-
4040
});
4141

4242
test('synthesis respects disabling tree metadata', () => {
@@ -45,7 +45,52 @@ describe('synthesis', () => {
4545
});
4646
const assembly = app.synth();
4747
expect(list(assembly.directory)).toEqual(['cdk.out', 'manifest.json']);
48+
});
49+
50+
test('synthesis respects disabling logicalId metadata', () => {
51+
const app = new cdk.App({
52+
context: {
53+
[cxapi.DISABLE_LOGICAL_ID_METADATA]: true,
54+
},
55+
});
56+
const stack = new cdk.Stack(app, 'one-stack');
57+
new cdk.CfnResource(stack, 'MagicResource', { type: 'Resource::Type' });
58+
59+
// WHEN
60+
const session = app.synth();
61+
62+
// THEN
63+
expect(Object.keys((session.manifest.artifacts ?? {})['one-stack'])).not.toContain('metadata');
64+
});
65+
66+
test('synthesis respects disabling logicalId metadata, and does not disable other metadata', () => {
67+
const app = new cdk.App({
68+
context: {
69+
[cxapi.DISABLE_LOGICAL_ID_METADATA]: true,
70+
},
71+
stackTraces: false,
72+
});
73+
const stack = new cdk.Stack(app, 'one-stack', { tags: { boomTag: 'BOOM' } });
74+
new cdk.CfnResource(stack, 'MagicResource', { type: 'Resource::Type' });
4875

76+
// WHEN
77+
const session = app.synth();
78+
79+
// THEN
80+
expect(session.manifest.artifacts?.['one-stack'].metadata).toEqual({
81+
'/one-stack': [
82+
{
83+
type: 'aws:cdk:stack-tags',
84+
data: [
85+
{
86+
key: 'boomTag',
87+
value: 'BOOM',
88+
},
89+
],
90+
},
91+
],
92+
// no logicalId entry
93+
});
4994
});
5095

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

59104
// THEN
60105
expect(list(session.directory).includes('one-stack.template.json')).toEqual(true);
61-
62106
});
63107

64108
test('some random construct implements "synthesize"', () => {
@@ -112,7 +156,6 @@ describe('synthesis', () => {
112156
},
113157
},
114158
});
115-
116159
});
117160

118161
test('random construct uses addCustomSynthesis', () => {
@@ -172,7 +215,6 @@ describe('synthesis', () => {
172215
},
173216
},
174217
});
175-
176218
});
177219

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

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)