Skip to content
v2.273.0

TagManager Class ​

TagManager facilitates a common implementation of tagging for Constructs.

Normally, you do not need to use this class, as the CloudFormation specification will indicate which resources are taggable. However, sometimes you will need this to make custom resources taggable. Used tagManager.renderedTags to obtain a value that will resolve to the tags at synthesis time.

Example ​

ts
class MyConstruct extends Resource implements ITaggable {
  public readonly tags = new TagManager(TagType.KEY_VALUE, 'Whatever::The::Type');

  constructor(scope: Construct, id: string) {
    super(scope, id);

    new CfnResource(this, 'Resource', {
      type: 'Whatever::The::Type',
      properties: {
        // ...
        Tags: this.tags.renderedTags,
      },
    });
  }
}
python
@jsii.implements(ITaggable)
class MyConstruct(Resource):

    def __init__(self, scope, id):
        super().__init__(scope, id)

        CfnResource(self, "Resource",
            type="Whatever::The::Type",
            properties={
                # ...
                "Tags": self.tags.rendered_tags
            }
        )
java
public class MyConstruct extends Resource implements ITaggable {
    public final Object tags;

    public MyConstruct(Construct scope, String id) {
        super(scope, id);

        CfnResource.Builder.create(this, "Resource")
                .type("Whatever::The::Type")
                .properties(Map.of(
                        // ...
                        "Tags", this.tags.getRenderedTags()))
                .build();
    }
}
csharp
// Install: dotnet add package Amazon.CDK.Lib
class MyConstruct : Resource, ITaggable
{
    public readonly void Tags = new TagManager(TagType.KEY_VALUE, "Whatever::The::Type");

    public MyConstruct(Construct scope, string id) : base(scope, id)
    {

        new CfnResource(this, "Resource", new CfnResourceProps {
            Type = "Whatever::The::Type",
            Properties = new Dictionary<string, object> {
                // ...
                { "Tags", Tags.RenderedTags }
            }
        });
    }
}
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
type myConstruct struct {
	Resource
	tags
}

func newMyConstruct(scope Construct, id *string) *myConstruct {
	this := &myConstruct{}
	newResource_Override(this, scope, id)

	awscdk.NewCfnResource(this, jsii.String("Resource"), &CfnResourceProps{
		Type: jsii.String("Whatever::The::Type"),
		Properties: map[string]interface{}{
			// ...
			"Tags": this.tags.renderedTags,
		},
	})
	return this
}

Initializer ​

ts
new TagManager(tagType: TagType, resourceTypeName: string, initialTags?: any, options?: TagManagerOptions)
python
TagManager(tag_type: TagType, resource_type_name: str, initial_tags: typing.Optional[Any] = None, options: typing.Optional[TagManagerOptions] = None)
java
new TagManager(TagType tagType, String resourceTypeName, Object initialTags, TagManagerOptions options)
csharp
new TagManager(TagType tagType, string resourceTypeName, object? initialTags = null, TagManagerOptions? options = null)
go
NewTagManager(TagType TagType, ResourceTypeName *string, InitialTags interface{}, Options TagManagerOptions)

Properties ​

renderedTagsIResolvablerendered_tagsIResolvablerenderedTagsIResolvableRenderedTagsIResolvableRenderedTagsIResolvablerequired#
A lazy value that represents the rendered tags at synthesis time.

If you need to make a custom construct taggable, use the value of this property to pass to the tags property of the underlying construct.

tagPropertyNamestringtag_property_namestrtagPropertyNameStringTagPropertyNamestringTagPropertyName*stringrequired#
The property name for tag values.

Normally this is tags but some resources choose a different name. Cognito UserPool uses UserPoolTags

Methods ​

applyTagAspectHere(include?: string[], exclude?: string[]): booleandef apply_tag_aspect_here(self, include: typing.Optional[typing.List[str]] = None, exclude: typing.Optional[typing.List[str]] = None) -> boolpublic Boolean applyTagAspectHere(List<String> include, List<String> exclude)public bool ApplyTagAspectHere(string[]? include = null, string[]? exclude = null)func ApplyTagAspectHere(Include *[]*string, Exclude *[]*string) *bool#
Determine if the aspect applies here.

Looks at the include and exclude resourceTypeName arrays to determine if the aspect applies here

hasTags(): booleandef has_tags(self, ) -> boolpublic Boolean hasTags()public bool HasTags()func HasTags() *bool#
Returns true if there are any tags defined.
isTaggable(construct: any): boolean@staticmethod def is_taggable(construct: Any) -> boolpublic static Boolean isTaggable(Object construct)public static bool IsTaggable(object construct)func IsTaggable(Construct interface{}) *boolstatic#
Check whether the given construct is Taggable.
isTaggableV2(construct: any): boolean@staticmethod def is_taggable_v2(construct: Any) -> boolpublic static Boolean isTaggableV2(Object construct)public static bool IsTaggableV2(object construct)func IsTaggableV2(Construct interface{}) *boolstatic#
Check whether the given construct is ITaggableV2.
of(construct: any): TagManager // chainable@staticmethod def of(construct: Any) -> TagManagerpublic static TagManager of(Object construct)public static TagManager Of(object construct)func Of(Construct interface{}) TagManagerstatic#
Return the TagManager associated with the given construct, if any.
removeTag(key: string, priority: number): voiddef remove_tag(self, key: str, priority: int | float) -> Nonepublic void removeTag(String key, Number priority)public void RemoveTag(string key, double priority)func RemoveTag(Key *string, Priority *float64)#
Removes the specified tag from the array if it exists.
2 parameters

Parameters

  • keykeykeykeyKey — The tag to remove.
  • prioritypriorityprioritypriorityPriority — The priority of the remove operation.
renderTags(combineWithTags?: any): anydef render_tags(self, combine_with_tags: typing.Optional[Any] = None) -> Anypublic Object renderTags(Object combineWithTags)public object RenderTags(object? combineWithTags = null)func RenderTags(CombineWithTags interface{}) interface{}#
Renders tags into the proper format based on TagType.

This method will eagerly render the tags currently applied. In most cases, you should be using tagManager.renderedTags instead, which will return a Lazy value that will resolve to the correct tags at synthesis time.

setTag(key: string, value: string, priority?: number, applyToLaunchedInstances?: boolean): voiddef set_tag(self, key: str, value: str, priority: typing.Optional[int | float] = None, apply_to_launched_instances: typing.Optional[bool] = None) -> Nonepublic void setTag(String key, String value, Number priority, Boolean applyToLaunchedInstances)public void SetTag(string key, string value, double? priority = null, bool? applyToLaunchedInstances = null)func SetTag(Key *string, Value *string, Priority *float64, ApplyToLaunchedInstances *bool)#
Adds the specified tag to the array of tags.
tagValues(): { [key: string]: string }def tag_values(self, ) -> typing.Mapping[str, str]public Map<String, String> tagValues()public IDictionary<string, string> TagValues()func TagValues() *map[string]*string#
Render the tags in a readable format.