22

How to add a tag to an AWS-CDK specific construct or even better one tag definition to all ressources created within the stack?

Sma Ma
  • 3,343
  • 2
  • 31
  • 39

5 Answers5

47

The accepted answer uses the deprecated syntax.

Newer version of tagging the whole App:

const app = new cdk.App();
new SomeStack(app, 'SomeStack');
Tags.of(app).add("app", "my-app-name-here");

You could also tag individual stacks only:

const app = new cdk.App();
const stack = new SomeStack(app, 'SomeStack');
Tags.of(stack).add("stack-name", "SomeStack");

Or individual Constructs:

const usersTable = new dynamodb.Table(this, 'Users');
Tags.of(usersTable).add("owner", "team-andromeda");

Tags will apply to sub-Constructs hierarchically.

Dzhuneyt
  • 8,437
  • 14
  • 64
  • 118
  • Thank you for the detailed explanation! This works. – captainblack Feb 10 '21 at 00:53
  • How does the construct know what it's children are? Or rather -- why can the tags NOT be propagated to SOME resources in the stack? – Tomasz Gandor Nov 22 '22 at 23:23
  • @TomaszGandor the whole CDK app is a huge construct tree - like a pyramid, with the "CDK App" at the top, followed by Stacks as direct descendants, and then there are Constructs, with some more Constructs inside, infinite levels deep. As to how the code knows what are the children, every construct has methods that allow you to query for its list of children. I forgot what the method name was but imaging it being like `constructA.getChildren()`. – Dzhuneyt Nov 23 '22 at 17:32
14

According to aws-cdk doc you can add a tag to all constructs/ressources. Tags will inherits to constructs within same "tree". That's pretty cool.

Example using aws-cdk based on java:

MyStack stack = new MyStack(app, "nameOfStack");
Tag.add(stack, "tag_foo", "tag_foo");

AWS-Doc CDK Tagging

AWS CDK Reference: Tag

Tags can be applied to any construct. Tags are inherited, based on the scope. If you tag construct A, and A contains construct B, construct B inherits the tag.

Example from aws-cdk doc:

import { App, Stack, Tag } from require('@aws-cdk/core');

const app = new App();
const theBestStack = new Stack(app, 'MarketingSystem');

// Add a tag to all constructs in the stack
Tag.add(theBestStack, 'StackType', 'TheBest');
Sma Ma
  • 3,343
  • 2
  • 31
  • 39
4

You can add tags to your CDK v2 app in Python, like so:

import aws_cdk as cdk

app = cdk.App()
cdk.Tags.of(app).add("TEAM", "TeamA") # add tags to the entire app (all resources created by this app)

lambda_stack = LambdaStack(app, 'lambda-stack')

cdk.Tags.of(lambda_stack).add("TEAM", "TeamA") # add tags to the entire stack (all resources of this stack)

...or to a construct:

lambda_role = iam.Role(self,
    assumed_by=iam.ServicePrincipal(service='lambda.amazonaws.com'),
    role_name='lambda-role'
)
cdk.Tags.of(lambda_role).add("TEAM", "TeamA") # add tags to this construct (add tags to just this role)
captainblack
  • 4,107
  • 5
  • 50
  • 60
  • Thanks. This is now quite old and not valid anymore with the current CDK. Import packages changed and I would suggest to rewrite the example like: ``` from aws_cdk import App, Tags app = App() Tags.of(app).add("TEAM", "TeamA") ... ``` – Felix Aug 12 '22 at 11:58
  • Thanks, I have updated the answer to CDK v2. – captainblack Sep 12 '22 at 16:45
1

Because you'll likely want to add more than one Tag to a construct, its handy to pass an object of tags. You can use aspects in cdk to descend nodes looking for node of your type and applying whatever you want to apply to said node. The following example adds tags.

export class TagAspect implements cdk.IAspect {
 private readonly tags: Object;

 constructor(tags: Object) {
   this.tags = tags;
 }

public visit(node: cdk.IConstruct): void {
  if (node instanceof cdk.Stack) {
      Object.entries(this.tags).forEach( ([key, value]) => {
      cdk.Tag.add(node,key,value); 
  });
 }}}

Then in the Stack you want to apply an aspect to run this.node.applyAspect(new TagAspect({"tag1":"mytag1","tag2":"another tag","tag3":"andanother"}));

user1412523
  • 198
  • 1
  • 6
0

Using the Java SDK:

public class CdkInitClusterApp {
    public static void main(final String[] args) {
        final App app = new App();
        final CdkInitClusterStack cdkInitClusterStack = new CdkInitClusterStack(app, "CdkInitClusterStack");
        Tag.add(cdkInitClusterStack, "Project", "Value");
        app.synth();
    }
}

Then recreate the jar

mvn clean compile package

And run the cdk diff for verify the changes, the output will be similar to the following one:

(base) [alessiosavi@localhost cdk-init-cluster]$ cdk diff
                   CdkInitClusterStack
Resources
[~] AWS::DynamoDB::Table cdk-test-table cdktesttableB0274F47 
 └─ [+] Tags
     └─ [{"Key":"Project","Value":"Value"}]
alessiosavi
  • 2,753
  • 2
  • 19
  • 38