2 min read | by Jordi Prats
When working with AWS CDK, it's essential to tag your resources to help you manage and organize them effectively. Tags are key-value pairs that you can attach to AWS resources to categorize and track them. By adding tags to your resources, you can easily identify, filter, and manage them based on specific criteria.
We can always add tags at the resource level, but if we want to add tags to all resources in a stack, we can simply pass the entire stack to the Tags.of() method. This method allows us to add tags to all resources in a stack:
import 'source-map-support/register'; import * as cdk from 'aws-cdk-lib'; import { ExampleStack } from '../lib/example-stack'; const app = new cdk.App(); const example = new ExampleStack(app, 'ExampleStack', {}); cdk.Tags.of(example).add('tag', 'value');
If we run the cdk synth command, we can see that the tags are added to all resources in the stack:
$ cdk synth -ci Resources: MyTestBucketPet2CattleS3Bucket3A425C06: Type: AWS::S3::Bucket Properties: BucketName: my-test-bucket Tags: - Key: tag Value: value UpdateReplacePolicy: Delete DeletionPolicy: Delete Metadata: aws:cdk:path: CdkCustomLibraryAppStack/MyTestBucket/Pet2CattleS3Bucket/Resource CDKMetadata: Type: AWS::CDK::Metadata Properties: Analytics: v2:deflate64:H4sIAAAAAAAA/zPSMzQ10zNQTCwv1k1OydbNyUzSqw4uSUzO1gEKxRcb61U7lSZnp5boOKflQVi1IGZQanF+aVFyKojtnJ+XklmSmZ9Xq5OXn5Kql1WsX2ZkpGcBNDarODNTt6g0ryQzN1UvCEIDAFw8WjtzAAAA Metadata: aws:cdk:path: CdkCustomLibraryAppStack/CDKMetadata/Default Condition: CDKMetadataAvailable (...)
Posted on 23/09/2024