Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion packages/@aws-cdk/aws-cloudformation/test/nested-stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Template } from '@aws-cdk/assertions';
import * as s3_assets from '@aws-cdk/aws-s3-assets';
import * as sns from '@aws-cdk/aws-sns';
import { describeDeprecated } from '@aws-cdk/cdk-build-tools';
import { App, CfnParameter, CfnResource, ContextProvider, LegacyStackSynthesizer, Names, Stack } from '@aws-cdk/core';
import { App, CfnParameter, CfnResource, ContextProvider, LegacyStackSynthesizer, Names, Stack, Tags } from '@aws-cdk/core';
import { NestedStack } from '../lib/nested-stack';

// keep this import separate from other imports to reduce chance for merge conflicts with v2-main
Expand Down Expand Up @@ -1085,4 +1085,49 @@ describeDeprecated('NestedStack', () => {
});
});

test('nested stack should get the tags added in root stack', () =>{
const app = new App();
const parentStack = new Stack(app, 'parent-stack');
const nestedStack = new NestedStack(parentStack, 'MyNestedStack');

// add tags
Tags.of(nestedStack).add('tag-1', '22');
Tags.of(nestedStack).add('tag-2', '33');

new sns.Topic(nestedStack, 'MyTopic');

// THEN
Template.fromStack(parentStack).hasResourceProperties(
'AWS::CloudFormation::Stack',
{
Tags: [
{
Key: 'tag-1',
Value: '22',
},
{
Key: 'tag-2',
Value: '33',
},
],
},
);

Template.fromStack(nestedStack).hasResourceProperties(
'AWS::SNS::Topic',
{
Tags: [
{
Key: 'tag-1',
Value: '22',
},
{
Key: 'tag-2',
Value: '33',
},
],
},
);
});

});
10 changes: 10 additions & 0 deletions packages/@aws-cdk/core/lib/nested-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ export class NestedStack extends Stack {
return false;
}

// When adding tags to nested stack, the tags need to be added to all the resources in
// in nested stack, which is handled by the `tags` property, But to tag the
// tags have to be added in the parent stack CfnStack resource. The CfnStack resource created
// by this class dont share the same TagManager as that of the one exposed by the `tag` property of the
// class, all the tags need to be copied to the CfnStack resource before synthesizing the resource.
// See https://github.com/aws/aws-cdk/pull/19128
Object.entries(this.tags.tagValues()).forEach(([key, value]) => {
this.resource.tags.setTag(key, value);
});

const cfn = JSON.stringify(this._toCloudFormation());
const templateHash = crypto.createHash('sha256').update(cfn).digest('hex');

Expand Down