Cobra Kai Dojo
Cobra Kai Dojo

Reputation: 1076

Is it possible to add multiple tags on a AWS CDK construct?

In python I am doing something like this to add tags to all resources of a construct: core.Tag.add(construct, 'Key', 'Value')

My question is, does the CDK support adding multiple tags in one command or do I have to iterate to add them one pair at a time. Something like this:

tags = {'Key1': 'Value1', 'Key2': 'Value2'}
for key, value in tags.items():
    core.Tag.add(construct, key, value)

I couldn't find anything in the documentation...

Upvotes: 4

Views: 2971

Answers (2)

MederD
MederD

Reputation: 75

This solved the multiple tagging in one line:

#!/usr/bin/env python3
import os
import aws_cdk as cdk
from cdk_sns.cdk_sns_stack import CdkSnsStack
from aws_cdk import (Tags, Stack)


app = cdk.App()

CdkSnsStack(app, "...",
    description="...",
    env=cdk.Environment(account=os.environ["CDK_DEFAULT_ACCOUNT"], region=os.environ["CDK_DEFAULT_REGION"]),
    )

# Add tags
tags = {'Key1': 'Value1', 'Key2': 'Value2', 'Key3':'Value3', 'Key4':'Value4', 'Key5':'Value5','Key6':'Value6'}
for key, value in tags.items():
    Tags.of(app).add(key, value)

app.synth()

For reference: https://docs.aws.amazon.com/cdk/v2/guide/tagging.html

Upvotes: 1

Simon
Simon

Reputation: 915

You are correct. There is currently no way in CDK to add multiple tags using 1 method. But you can try to figure out a way to manipulate aws_cdk.core.TagManager directly. I don't recommend this because the Python CDK uses JSII to interact with the actual CDK written in TypeScript. Here's what core.Tag.add looks like

    @jsii.member(jsii_name="add")
    @classmethod
    def add(cls, scope: "Construct", key: str, value: str, *, apply_to_launched_instances: typing.Optional[bool]=None, exclude_resource_types: typing.Optional[typing.List[str]]=None, include_resource_types: typing.Optional[typing.List[str]]=None, priority: typing.Optional[jsii.Number]=None) -> None:
        """add tags to the node of a construct and all its the taggable children.

        :param scope: -
        :param key: -
        :param value: -
        :param props: -
        :param apply_to_launched_instances: Whether the tag should be applied to instances in an AutoScalingGroup. Default: true
        :param exclude_resource_types: An array of Resource Types that will not receive this tag. An empty array will allow this tag to be applied to all resources. A non-empty array will apply this tag only if the Resource type is not in this array. Default: []
        :param include_resource_types: An array of Resource Types that will receive this tag. An empty array will match any Resource. A non-empty array will apply this tag only to Resource types that are included in this array. Default: []
        :param priority: Priority of the tag operation. Higher or equal priority tags will take precedence. Setting priority will enable the user to control tags when they need to not follow the default precedence pattern of last applied and closest to the construct in the tree. Default: Default priorities: - 100 for {@link SetTag} - 200 for {@link RemoveTag} - 50 for tags added directly to CloudFormation resources
        """
        props = TagProps(apply_to_launched_instances=apply_to_launched_instances, exclude_resource_types=exclude_resource_types, include_resource_types=include_resource_types, priority=priority)

        return jsii.sinvoke(cls, "add", [scope, key, value, props])

Upvotes: 2

Related Questions