Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a tag to an AWS-CDK construct

Tags:

aws-cdk

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

like image 592
Sma Ma Avatar asked Mar 04 '19 19:03

Sma Ma


People also ask

What AWS resources can tags be applied to?

With most AWS resources, you have the option of adding tags when you create the resource, whether it's an Amazon EC2 instance, an Amazon S3 bucket, or other resource. However, you can also add tags to multiple, supported resources at once by using Tag Editor.

What is a construct in AWS CDK?

Constructs are the basic building blocks of AWS CDK apps. A construct represents a "cloud component" and encapsulates everything AWS CloudFormation needs to create the component. Constructs are part of the Construct Programming Model (CPM).


2 Answers

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');
like image 142
Sma Ma Avatar answered Sep 26 '22 13:09

Sma Ma


The above answers use 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.

like image 36
Dzhuneyt Avatar answered Sep 23 '22 13:09

Dzhuneyt