diff --git a/packages/@aws-cdk/core/lib/cfn-element.ts b/packages/@aws-cdk/core/lib/cfn-element.ts index 9bb08746c4a47..84126add4e13c 100644 --- a/packages/@aws-cdk/core/lib/cfn-element.ts +++ b/packages/@aws-cdk/core/lib/cfn-element.ts @@ -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. @@ -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); + } } /** diff --git a/packages/@aws-cdk/core/test/synthesis.test.ts b/packages/@aws-cdk/core/test/synthesis.test.ts index 496fd76fdbd91..bb0a87afa0ea4 100644 --- a/packages/@aws-cdk/core/test/synthesis.test.ts +++ b/packages/@aws-cdk/core/test/synthesis.test.ts @@ -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() { @@ -36,7 +37,6 @@ describe('synthesis', () => { }, }), }); - }); test('synthesis respects disabling tree metadata', () => { @@ -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', () => { @@ -58,7 +103,6 @@ describe('synthesis', () => { // THEN expect(list(session.directory).includes('one-stack.template.json')).toEqual(true); - }); test('some random construct implements "synthesize"', () => { @@ -112,7 +156,6 @@ describe('synthesis', () => { }, }, }); - }); test('random construct uses addCustomSynthesis', () => { @@ -172,7 +215,6 @@ describe('synthesis', () => { }, }, }); - }); testDeprecated('it should be possible to synthesize without an app', () => { @@ -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' }); - }); }); diff --git a/packages/@aws-cdk/cx-api/lib/app.ts b/packages/@aws-cdk/cx-api/lib/app.ts index 41c03f374b408..c08fd5868207b 100644 --- a/packages/@aws-cdk/cx-api/lib/app.ts +++ b/packages/@aws-cdk/cx-api/lib/app.ts @@ -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 */