Skip to content

Commit 3af329b

Browse files
authored
fix: apply tags to nested stack (#19128)
When a nested stack gets a custom tags the tag gets applied only to the resources defined in the stack and not the stack itself. This change fixes this issue and applies the tag to the nested stack as well. This was caused by NestedStack subclassing Stack but not using the NestedStack.tags in the `CfnStack` (as it accepts only `CfnTag[]` and not `TagManager`) This change updates the `_prepareTemplateAsset` to get all the tags added to the `CfnStack` from `NestedStack.tag` before rendering the stack fixes #17463 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 8f727d1 commit 3af329b

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

packages/@aws-cdk/aws-cloudformation/test/nested-stack.test.ts

+46-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Template } from '@aws-cdk/assertions';
44
import * as s3_assets from '@aws-cdk/aws-s3-assets';
55
import * as sns from '@aws-cdk/aws-sns';
66
import { describeDeprecated } from '@aws-cdk/cdk-build-tools';
7-
import { App, CfnParameter, CfnResource, ContextProvider, LegacyStackSynthesizer, Names, Stack } from '@aws-cdk/core';
7+
import { App, CfnParameter, CfnResource, ContextProvider, LegacyStackSynthesizer, Names, Stack, Tags } from '@aws-cdk/core';
88
import { NestedStack } from '../lib/nested-stack';
99

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

1088+
test('nested stack should get the tags added in root stack', () =>{
1089+
const app = new App();
1090+
const parentStack = new Stack(app, 'parent-stack');
1091+
const nestedStack = new NestedStack(parentStack, 'MyNestedStack');
1092+
1093+
// add tags
1094+
Tags.of(nestedStack).add('tag-1', '22');
1095+
Tags.of(nestedStack).add('tag-2', '33');
1096+
1097+
new sns.Topic(nestedStack, 'MyTopic');
1098+
1099+
// THEN
1100+
Template.fromStack(parentStack).hasResourceProperties(
1101+
'AWS::CloudFormation::Stack',
1102+
{
1103+
Tags: [
1104+
{
1105+
Key: 'tag-1',
1106+
Value: '22',
1107+
},
1108+
{
1109+
Key: 'tag-2',
1110+
Value: '33',
1111+
},
1112+
],
1113+
},
1114+
);
1115+
1116+
Template.fromStack(nestedStack).hasResourceProperties(
1117+
'AWS::SNS::Topic',
1118+
{
1119+
Tags: [
1120+
{
1121+
Key: 'tag-1',
1122+
Value: '22',
1123+
},
1124+
{
1125+
Key: 'tag-2',
1126+
Value: '33',
1127+
},
1128+
],
1129+
},
1130+
);
1131+
});
1132+
10881133
});

packages/@aws-cdk/core/lib/nested-stack.ts

+10
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ export class NestedStack extends Stack {
205205
return false;
206206
}
207207

208+
// When adding tags to nested stack, the tags need to be added to all the resources in
209+
// in nested stack, which is handled by the `tags` property, But to tag the
210+
// tags have to be added in the parent stack CfnStack resource. The CfnStack resource created
211+
// by this class dont share the same TagManager as that of the one exposed by the `tag` property of the
212+
// class, all the tags need to be copied to the CfnStack resource before synthesizing the resource.
213+
// See https://github.com/aws/aws-cdk/pull/19128
214+
Object.entries(this.tags.tagValues()).forEach(([key, value]) => {
215+
this.resource.tags.setTag(key, value);
216+
});
217+
208218
const cfn = JSON.stringify(this._toCloudFormation());
209219
const templateHash = crypto.createHash('sha256').update(cfn).digest('hex');
210220

0 commit comments

Comments
 (0)