Tagging
Tags are informational key-value elements that you can add to constructs in your AWS CDK app. A tag applied to a given construct also applies to all of its taggable children. Tags are included in the AWS CloudFormation template synthesized from your app and are applied to the AWS resources it deploys. You can use tags to identify and categorize resources to simplify management, in cost allocation, and for access control, as well as for any other purposes you devise.
For more information about how you can use tags with your AWS resources, see the white
paper Tagging
Best Practices
The Tags
class includes the static method of()
, through which
you can add tags to, or remove tags from, the specified construct.
-
Tags.of(
applies a new tag to the given construct and all of its children.SCOPE
).add() -
Tags.of(
removes a tag from the given construct and any of its children, including tags a child construct may have applied to itself.SCOPE
).remove()
Tagging is implemented using Aspects. Aspects are a way to apply an operation (such as tagging) to all constructs in a given scope.
The following example applies the tag key with the value value to a construct.
The following example deletes the tag key from a construct.
Tag priorities
The AWS CDK applies and removes tags recursively. If there are conflicts, the tagging
operation with the highest priority wins. (Priorities are set using the optional
priority
property.) If the priorities of two operations are the same, the
tagging operation closest to the bottom of the construct tree wins. By default, applying
a tag
has a priority of 100 (except for tags added directly to an AWS CloudFormation resource,
which has a
priority of 50) and removing a tag has a priority of 200.
The following applies a tag with a priority of 300 to a construct.
Optional properties
Tags support properties
that
fine-tune how tags are applied to, or removed from, resources. All properties are
optional.
applyToLaunchedInstances
(Python:apply_to_launched_instances
)-
Available for add() only. By default, tags are applied to instances launched in an Auto Scaling group. Set this property to false to ignore instances launched in an Auto Scaling group.
includeResourceTypes
/excludeResourceTypes
(Python:include_resource_types
/exclude_resource_types
)-
Use these to manipulate tags only on a subset of resources, based on AWS CloudFormation resource types. By default, the operation is applied to all resources in the construct subtree, but this can be changed by including or excluding certain resource types. Exclude takes precedence over include, if both are specified.
priority
-
Use this to set the priority of this operation with respect to other
Tags.add()
andTags.remove()
operations. Higher values take precedence over lower values. The default is 100 for add operations (50 for tags applied directly to AWS CloudFormation resources) and 200 for remove operations.
The following example applies the tag tagname with the value value and priority 100 to resources of type AWS::Xxx::Yyy in the construct, but not to instances launched in an Amazon EC2 Auto Scaling group or to resources of type AWS::Xxx::Zzz. (These are placeholders for two arbitrary but different AWS CloudFormation resource types.)
The following example removes the tag tagname with priority 200 from resources of type AWS::Xxx::Yyy in the construct, but not from resources of type AWS::Xxx::Zzz.
Example
The following example adds the tag key StackType with
value TheBest to any resource created within the Stack named
MarketingSystem
. Then it removes it again from all resources except Amazon EC2 VPC
subnets. The result is that only the subnets have the tag applied.
The following code achieves the same result. Consider which approach (inclusion or exclusion) makes your intent clearer.