Skip to content

Commit 6bb02c0

Browse files
committed
fix: apply tags to nested stack
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 resource that was created by it and not rendering the tags that were added when creating the AWS::CloudFormation::Stack. This change updates the `_prepareTemplateAsset` to get all the tags added to the nested stacks instance TagManager and add them to the CfnStack before rendering the assets fixes #17463
1 parent e2f6918 commit 6bb02c0

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

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

Lines changed: 46 additions & 1 deletion
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ export class NestedStack extends Stack {
205205
return false;
206206
}
207207

208+
Object.entries(this.tags.tagValues()).forEach(([key, value]) => {
209+
this.resource.tags.setTag(key, value);
210+
});
211+
208212
const cfn = JSON.stringify(this._toCloudFormation());
209213
const templateHash = crypto.createHash('sha256').update(cfn).digest('hex');
210214

0 commit comments

Comments
 (0)