This is a quick reference guide for adding tags in AWS CDK.
Introduction
When adding tags to resources in CDK, I often forget the exact method and end up searching for it again.
To avoid having to look it up every time, I'm documenting the tag assignment methods here.
Note: This article was translated from my original post.
Environment
The work was done in the following environment:
- CDK version: 1.92.0
- OS: Amazon Linux 2 (Cloud9)
Adding Tags in CDK
To add tags in CDK, the recommended approach is to use the Tags class.
Tags and the AWS CDK - AWS Cloud Development Kit (AWS CDK) v2
※There's also a method using the Tag class, but it's deprecated.
Let's look at how to add tags using Python as an example.
Examples
Tags are added in the following format:
Tags.of(SCOPE).add(key, value)
For SCOPE, specify a Construct or Stack, and for key and value, specify the tag key and value respectively.
Here are some concrete examples.
Adding Tags at the Stack Level
from aws_cdk import ( aws_s3 as s3, core ) class TagSampleStack(core.Stack): def __init__(self, scope: core.Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) s3_01 = s3.Bucket(self, "TagTestBucket01", bucket_name="tag-test-bucket-001") s3_02 = s3.Bucket(self, "TagTestBucket02", bucket_name="tag-test-bucket-002") app = core.App() tag_sample_stack = TagSampleStack(app, "tag-sample") # Add tags to the Stack core.Tags.of(tag_sample_stack).add("Project", "TagTest") app.synth()
When you add a tag to a Stack this way, the tag is applied to all resources contained in that Stack.
In the above case, both S3 buckets tag-test-bucket-001 and tag-test-bucket-002 included in the Stack will be tagged with Project: TagTest.
Adding Tags at the Construct Level
from aws_cdk import ( aws_s3 as s3, core ) class TagSampleStack(core.Stack): def __init__(self, scope: core.Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) s3_01 = s3.Bucket(self, "TagTestBucket01", bucket_name="tag-test-bucket-001") s3_02 = s3.Bucket(self, "TagTestBucket02", bucket_name="tag-test-bucket-002") # Add tags to the Construct core.Tags.of(s3_01).add("Project", "TagTest") app = core.App() tag_sample_stack = TagSampleStack(app, "tag-sample") app.synth()
When you add a tag to a specific Construct this way, the tag is applied only to that target Construct.
In the above case, only the S3 bucket tag-test-bucket-001 will be tagged with Project: TagTest.
Note: Deprecated Tag Assignment Method
While you can add tags using the Tag class as shown below, this method is deprecated.
Tag.add(SCOPE, key, value)
Code example:
from aws_cdk import ( aws_s3 as s3, core ) class TagSampleStack(core.Stack): def __init__(self, scope: core.Construct, construct_id: str, **kwargs) -> None: super().__init__(scope, construct_id, **kwargs) s3_01 = s3.Bucket(self, "TagTestBucket01", bucket_name="tag-test-bucket-001") s3_02 = s3.Bucket(self, "TagTestBucket02", bucket_name="tag-test-bucket-002") app = core.App() tag_sample_stack = TagSampleStack(app, "tag-sample") # Add tags using the Tag class core.Tag.add(tag_sample_stack, "Project", "TagTest") app.synth()
When you run the above code, you'll see a warning that using Tag.add is deprecated, and it recommends using Tags.of(scope).add(k,v) instead.
[Warning at /tag-sample] The API @aws-cdk/core.Tag.add(scope,k,v) is deprecated: Use "Tags.of(scope).add(k,v)" instead. This API will be removed in the next major release
Conclusion
In this article, I documented how to add tags in CDK.
While I used Python as an example, the same basic approach works for other languages as well.
I hope this helps someone out there.
[Related Articles]