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

(core/stack): stack.tags does not do anything #30055

Open
kaiz-io opened this issue May 3, 2024 · 4 comments
Open

(core/stack): stack.tags does not do anything #30055

kaiz-io opened this issue May 3, 2024 · 4 comments
Labels
@aws-cdk/aws-s3 Related to Amazon S3 bug This issue is a bug. documentation This is a problem with documentation. p3

Comments

@kaiz-io
Copy link
Contributor

kaiz-io commented May 3, 2024

Describe the bug

Possible regression. Creating this for a customer and they had this working. Waiting on them to give me the cdk version where this was working.

stack.tags does not do anything. I spot checked 2.1.0, 2.50.0, 2.100.0 cdk version and I cannot see where it worked.

Expected Behavior

When specifing stack.tags it will go out and tag the constructs that support tagging.

Current Behavior

stack.tags seemingly does nothing.

Reproduction Steps

#!/usr/bin/env node
import 'source-map-support/register';
import { Construct } from 'constructs';
import * as cdk from 'aws-cdk-lib';
import { Bucket } from 'aws-cdk-lib/aws-s3';

const app = new cdk.App();
class TagsStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    new Bucket(this, "bucket");

    cdk.Tags.of(this).add('tester','value'); //This works adding the tester tag to the s3 bucket

  }
}
new TagsStack(app, 'TagsStack', {
  tags: {
    'test': 'value' // This does not add the any tags to the s3 bucket.
  }
});

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.140.0

Framework Version

No response

Node.js Version

v18.17.1

OS

Ubuntu 22.04 - Linux

Language

TypeScript

Language Version

No response

Other information

No response

@kaiz-io kaiz-io added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels May 3, 2024
@github-actions github-actions bot added the @aws-cdk/aws-s3 Related to Amazon S3 label May 3, 2024
@nmussy
Copy link
Contributor

nmussy commented May 4, 2024

This seems to work fine for me, did you only check the cdk synth output? The Stack.tags do not appear there, but they're being properly applied

import { App, CfnOutput, Stack, type StackProps } from "aws-cdk-lib";
import { Bucket } from "aws-cdk-lib/aws-s3";
import { Topic } from "aws-cdk-lib/aws-sns";
import { Queue } from "aws-cdk-lib/aws-sqs";
import type { Construct } from "constructs";

const app = new App();
class TagsStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const bucket = new Bucket(this, "Bucket");
    const queue = new Queue(this, "Queue");
    const topic = new Topic(this, "Topic");

    new CfnOutput(this, "BucketName", { value: bucket.bucketName });
    new CfnOutput(this, "QueueUrl", { value: queue.queueUrl });
    new CfnOutput(this, "TopicArn", { value: topic.topicArn });
  }
}
new TagsStack(app, "TagsStack", {
  tags: { TAG_KEY: "TAG_VALUE" },
});
$ yarn cdk deploy
Outputs:
TagsStack.BucketName = tagsstack-bucket83908e77-w8nobhqbuytq
TagsStack.QueueUrl = https://sqs.us-east-1.amazonaws.com/339712719728/TagsStack-Queue4A7E3555-TIfUha8OSg9G
TagsStack.TopicArn = arn:aws:sns:us-east-1:339712719728:TagsStack-TopicBFC7AF6E-5fd7L5n4nsPh

$ aws s3api get-bucket-tagging --bucket tagsstack-bucket83908e77-w8nobhqbuytq | jq '.TagSet[0]'
{
  "Key": "TAG_KEY",
  "Value": "TAG_VALUE"
}

$ aws sqs list-queue-tags --queue-url https://sqs.us-east-1.amazonaws.com/339712719728/TagsStack-Queue4A7E3555-TIfUha8OSg9G | jq '.Tags'
{
  "TAG_KEY": "TAG_VALUE"
}

$ aws sns list-tags-for-resource --resource-arn arn:aws:sns:us-east-1:339712719728:TagsStack-TopicBFC7AF6E-5fd7L5n4nsPh | jq '.Tags[0]'
{
  "Key": "TAG_KEY",
  "Value": "TAG_VALUE"
}

@pahud pahud added the investigating This issue is being investigated and/or work is in progress to resolve the issue. label May 6, 2024
@pahud pahud self-assigned this May 6, 2024
@pahud
Copy link
Contributor

pahud commented May 6, 2024

Hi

That is stack-level tags and it would not show up in the synthesized template. Cloudformation would be responsible to propagate it to resources but please note:

Propagation of stack-level tags to resources, including automatically created tags, can vary by resource. For example, tags aren't propagated to Amazon EBS volumes that are created from block device mappings.

see - https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-resource-tags.html

We generally recommend using Tags.of(stack)add('key', 'value') instead in your CDK app.
see - https://docs.aws.amazon.com/cdk/v2/guide/tagging.html

@pahud pahud added p2 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels May 6, 2024
@pahud pahud removed their assignment May 6, 2024
@kaiz-io
Copy link
Contributor Author

kaiz-io commented May 6, 2024

Thanks!

Ask now would be to update the docs to better explain what stacks.tags does and how/link to other docs and that Tags.of would be preferred.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label May 6, 2024
@pahud pahud added documentation This is a problem with documentation. p3 and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. p2 labels Aug 7, 2024
@pahud
Copy link
Contributor

pahud commented Aug 7, 2024

Sure I am leaving this issue open as p3 and we welcome any PRs to update the doc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-s3 Related to Amazon S3 bug This issue is a bug. documentation This is a problem with documentation. p3
Projects
None yet
Development

No branches or pull requests

3 participants