Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: deploy-time stack tags cause synthesis to fail #32041

Merged
merged 2 commits into from
Nov 6, 2024
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
21 changes: 14 additions & 7 deletions packages/aws-cdk-lib/core/lib/stack-synthesizers/_shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,22 @@ export function addStackArtifactToAssembly(
// nested stack tags are applied at the AWS::CloudFormation::Stack resource
// level and are not needed in the cloud assembly.
if (Object.entries(stackTags).length > 0) {
for (const [k, v] of Object.entries(stackTags)) {
if (Token.isUnresolved(k) || Token.isUnresolved(v)) {
throw new Error(`Stack tags may not contain deploy-time values (tag: ${k}=${v}). Apply tags containing deploy-time values to resources only, avoid tagging stacks.`);
}
const resolvedTags = Object.entries(stackTags).filter(([k, v]) => !(Token.isUnresolved(k) || Token.isUnresolved(v)));
const unresolvedTags = Object.entries(stackTags).filter(([k, v]) => Token.isUnresolved(k) || Token.isUnresolved(v));

if (unresolvedTags.length > 0) {
const rendered = unresolvedTags.map(([k, v]) => `${Token.isUnresolved(k) ? '<TOKEN>': k}=${Token.isUnresolved(v) ? '<TOKEN>' : v}`).join(', ');
stack.node.addMetadata(
cxschema.ArtifactMetadataEntryType.WARN,
`Ignoring stack tags that contain deploy-time values (found: ${rendered}). Apply tags containing deploy-time values to resources only, avoid tagging stacks (for example using { excludeResourceTypes: ['aws:cdk:stack'] }).`,
);
}

stack.node.addMetadata(
cxschema.ArtifactMetadataEntryType.STACK_TAGS,
Object.entries(stackTags).map(([key, value]) => ({ Key: key, Value: value })));
if (resolvedTags.length > 0) {
stack.node.addMetadata(
cxschema.ArtifactMetadataEntryType.STACK_TAGS,
resolvedTags.map(([key, value]) => ({ Key: key, Value: value })));
}
}

const deps = [
Expand Down
11 changes: 9 additions & 2 deletions packages/aws-cdk-lib/core/test/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2075,7 +2075,7 @@ describe('stack', () => {
expect(asm.getStackArtifact(stack2.artifactId).tags).toEqual(expected);
});

test('stack tags may not contain tokens', () => {
test('warning when stack tags contain tokens', () => {
// GIVEN
const app = new App({
stackTraces: false,
Expand All @@ -2087,7 +2087,14 @@ describe('stack', () => {
},
});

expect(() => app.synth()).toThrow(/Stack tags may not contain deploy-time values/);
const asm = app.synth();
const stackArtifact = asm.stacks[0];
expect(stackArtifact.manifest.metadata?.['/stack1']).toEqual([
{
type: 'aws:cdk:warning',
data: expect.stringContaining('Ignoring stack tags that contain deploy-time values'),
},
]);
});

test('stack notification arns are reflected in the stack artifact properties', () => {
Expand Down