标记 - AWS Cloud Development Kit (AWS CDK) v2

这是 AWS CDK v2 开发者指南。较旧的 CDK v1 于 2022 年 6 月 1 日进入维护阶段,并于 2023 年 6 月 1 日终止支持。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

标记

标签是信息性键值元素,您可以将其添加到应用程序的构造中。 AWS CDK 应用于给定构造的标签也适用于其所有可标记的子构造。标签包含在从您的应用程序合成的 AWS CloudFormation 模板中,并应用于其部署的 AWS 资源。您可以使用标签对资源进行标识和分类,以实现以下目的:

  • 简化管理

  • 成本分配

  • 访问控制

  • 您设计的任何其他目的

提示

有关如何对 AWS 资源使用标签的更多信息,请参阅AWS 白皮书中的为 AWS 资源添加标签的最佳实践

使用标签

Tags类包括静态方法of(),通过该方法可以向指定构造添加标签或从中删除标签。

注意

标记是使用方面实现的。Aspects 是一种将操作(例如标记)应用于给定作用域内的所有构造的方法。

以下示例将带有的标签应用于构造。

TypeScript
Tags.of(myConstruct).add('key', 'value');
JavaScript
Tags.of(myConstruct).add('key', 'value');
Python
Tags.of(my_construct).add("key", "value")
Java
Tags.of(myConstruct).add("key", "value");
C#
Tags.Of(myConstruct).Add("key", "value");
Go
awscdk.Tags_Of(myConstruct).Add(jsii.String("key"), jsii.String("value"), &awscdk.TagProps{})

以下示例从构造中删除标签密钥

TypeScript
Tags.of(myConstruct).remove('key');
JavaScript
Tags.of(myConstruct).remove('key');
Python
Tags.of(my_construct).remove("key")
Java
Tags.of(myConstruct).remove("key");
C#
Tags.Of(myConstruct).Remove("key");
Go
awscdk.Tags_Of(myConstruct).Remove(jsii.String("key"), &awscdk.TagProps{})

如果您使用的是Stage构造,请在Stage级别或更低级别应用标签。标签不会跨Stage界应用。

标记优先级

递归 AWS CDK 应用和删除标签。如果存在冲突,则优先级最高的标记操作获胜。(使用可选priority属性设置优先级。) 如果两个操作的优先级相同,则最靠近构造树底部的标记操作获胜。默认情况下,应用标签的优先级为 100(直接添加到 AWS CloudFormation 资源的标签除外,其优先级为 50)。移除标签的默认优先级为 200。

以下内容将优先级为 300 的标签应用于构造。

TypeScript
Tags.of(myConstruct).add('key', 'value', { priority: 300 });
JavaScript
Tags.of(myConstruct).add('key', 'value', { priority: 300 });
Python
Tags.of(my_construct).add("key", "value", priority=300)
Java
Tags.of(myConstruct).add("key", "value", TagProps.builder() .priority(300).build());
C#
Tags.Of(myConstruct).Add("key", "value", new TagProps { Priority = 300 });
Go
awscdk.Tags_Of(myConstruct).Add(jsii.String("key"), jsii.String("value"), &awscdk.TagProps{ Priority: jsii.Number(300), })

可选属性

标签支持properties微调标签应用于资源或从资源中删除标签的方式。所有其他属性均为可选。

applyToLaunchedInstances(Python:apply_to_launched_instances

仅适用于 add ()。默认情况下,标签应用于在 Auto Scaling 组中启动的实例。将此属性设置为 false 可忽略在 Auto Scaling 组中启动的实例。

includeResourceTypes/excludeResourceTypes(Python:include_resource_types/exclude_resource_types)

使用它们可以根据资源类型仅对资源子集操作标签。 AWS CloudFormation 默认情况下,该操作应用于构造子树中的所有资源,但可以通过包含或排除某些资源类型来更改此值。如果同时指定了两者,则排除优先于包含。

priority

使用它来设置此操作相对于其他Tags.add()Tags.remove()操作的优先级。较高的值优先于较低的值。添加操作的默认值为 100(直接应用于 AWS CloudFormation 资源的标签为 50),移除操作的默认值为 200。

以下示例将值为且优先级为 100标签 tagnam e 应用于构造AWS::Xxx::Yyy中类型的资源。它不会将标签应用于在 Amazon EC2 Auto Scaling 组中启动的实例或该类型的资源AWS::Xxx::Zzz。(它们是两种任意但不同的 AWS CloudFormation 资源类型的占位符。)

TypeScript
Tags.of(myConstruct).add('tagname', 'value', { applyToLaunchedInstances: false, includeResourceTypes: ['AWS::Xxx::Yyy'], excludeResourceTypes: ['AWS::Xxx::Zzz'], priority: 100, });
JavaScript
Tags.of(myConstruct).add('tagname', 'value', { applyToLaunchedInstances: false, includeResourceTypes: ['AWS::Xxx::Yyy'], excludeResourceTypes: ['AWS::Xxx::Zzz'], priority: 100 });
Python
Tags.of(my_construct).add("tagname", "value", apply_to_launched_instances=False, include_resource_types=["AWS::Xxx::Yyy"], exclude_resource_types=["AWS::Xxx::Zzz"], priority=100)
Java
Tags.of(myConstruct).add("tagname", "value", TagProps.builder() .applyToLaunchedInstances(false) .includeResourceTypes(Arrays.asList("AWS::Xxx::Yyy")) .excludeResourceTypes(Arrays.asList("AWS::Xxx::Zzz")) .priority(100).build());
C#
Tags.Of(myConstruct).Add("tagname", "value", new TagProps { ApplyToLaunchedInstances = false, IncludeResourceTypes = ["AWS::Xxx::Yyy"], ExcludeResourceTypes = ["AWS::Xxx::Zzz"], Priority = 100 });
Go
awscdk.Tags_Of(myConstruct).Add(jsii.String("tagname"), jsii.String("value"), &awscdk.TagProps{ ApplyToLaunchedInstances: jsii.Bool(false), IncludeResourceTypes: &[]*string{jsii.String("AWS::Xxx:Yyy")}, ExcludeResourceTypes: &[]*string{jsii.String("AWS::Xxx:Zzz")}, Priority: jsii.Number(100), })

以下示例从构造中类型的资源AWS::Xxx::Yyy中删除优先级为 200标签 tagnam e,但未从类型的AWS::Xxx::Zzz资源中移除。

TypeScript
Tags.of(myConstruct).remove('tagname', { includeResourceTypes: ['AWS::Xxx::Yyy'], excludeResourceTypes: ['AWS::Xxx::Zzz'], priority: 200, });
JavaScript
Tags.of(myConstruct).remove('tagname', { includeResourceTypes: ['AWS::Xxx::Yyy'], excludeResourceTypes: ['AWS::Xxx::Zzz'], priority: 200 });
Python
Tags.of(my_construct).remove("tagname", include_resource_types=["AWS::Xxx::Yyy"], exclude_resource_types=["AWS::Xxx::Zzz"], priority=200,)
Java
Tags.of((myConstruct).remove("tagname", TagProps.builder() .includeResourceTypes(Arrays.asList("AWS::Xxx::Yyy")) .excludeResourceTypes(Arrays.asList("AWS::Xxx::Zzz")) .priority(100).build());
C#
Tags.Of(myConstruct).Remove("tagname", new TagProps { IncludeResourceTypes = ["AWS::Xxx::Yyy"], ExcludeResourceTypes = ["AWS::Xxx::Zzz"], Priority = 100 });
Go
awscdk.Tags_Of(myConstruct).Remove(jsii.String("tagname"), &awscdk.TagProps{ IncludeResourceTypes: &[]*string{jsii.String("AWS::Xxx:Yyy")}, ExcludeResourceTypes: &[]*string{jsii.String("AWS::Xxx:Zzz")}, Priority: jsii.Number(200), })

示例

以下示例将StackType带有值TheBest的标签键添加到在Stack命名中创建的任何资源中MarketingSystem。然后,它会再次将其从除 Amazon EC2 VPC 子网之外的所有资源中删除。结果是只有子网应用了标记。

TypeScript
import { App, Stack, Tags } from 'aws-cdk-lib'; const app = new App(); const theBestStack = new Stack(app, 'MarketingSystem'); // Add a tag to all constructs in the stack Tags.of(theBestStack).add('StackType', 'TheBest'); // Remove the tag from all resources except subnet resources Tags.of(theBestStack).remove('StackType', { excludeResourceTypes: ['AWS::EC2::Subnet'] });
JavaScript
const { App, Stack, Tags } = require('aws-cdk-lib'); const app = new App(); const theBestStack = new Stack(app, 'MarketingSystem'); // Add a tag to all constructs in the stack Tags.of(theBestStack).add('StackType', 'TheBest'); // Remove the tag from all resources except subnet resources Tags.of(theBestStack).remove('StackType', { excludeResourceTypes: ['AWS::EC2::Subnet'] });
Python
from aws_cdk import App, Stack, Tags app = App(); the_best_stack = Stack(app, 'MarketingSystem') # Add a tag to all constructs in the stack Tags.of(the_best_stack).add("StackType", "TheBest") # Remove the tag from all resources except subnet resources Tags.of(the_best_stack).remove("StackType", exclude_resource_types=["AWS::EC2::Subnet"])
Java
import software.amazon.awscdk.App; import software.amazon.awscdk.Tags; // Add a tag to all constructs in the stack Tags.of(theBestStack).add("StackType", "TheBest"); // Remove the tag from all resources except subnet resources Tags.of(theBestStack).remove("StackType", TagProps.builder() .excludeResourceTypes(Arrays.asList("AWS::EC2::Subnet")) .build());
C#
using Amazon.CDK; var app = new App(); var theBestStack = new Stack(app, 'MarketingSystem'); // Add a tag to all constructs in the stack Tags.Of(theBestStack).Add("StackType", "TheBest"); // Remove the tag from all resources except subnet resources Tags.Of(theBestStack).Remove("StackType", new TagProps { ExcludeResourceTypes = ["AWS::EC2::Subnet"] });
Go
import "github.com/aws/aws-cdk-go/awscdk/v2" app := awscdk.NewApp(nil) theBestStack := awscdk.NewStack(app, jsii.String("MarketingSystem"), &awscdk.StackProps{}) // Add a tag to all constructs in the stack awscdk.Tags_Of(theBestStack).Add(jsii.String("StackType"), jsii.String("TheBest"), &awscdk.TagProps{}) // Remove the tag from all resources except subnet resources awscdk.Tags_Of(theBestStack).Add(jsii.String("StackType"), jsii.String("TheBest"), &awscdk.TagProps{ ExcludeResourceTypes: &[]*string{jsii.String("AWS::EC2::Subnet")}, })

以下代码实现了相同的结果。考虑哪种方法(包含或排除)可以使您的意图更加明确。

TypeScript
Tags.of(theBestStack).add('StackType', 'TheBest', { includeResourceTypes: ['AWS::EC2::Subnet']});
JavaScript
Tags.of(theBestStack).add('StackType', 'TheBest', { includeResourceTypes: ['AWS::EC2::Subnet']});
Python
Tags.of(the_best_stack).add("StackType", "TheBest", include_resource_types=["AWS::EC2::Subnet"])
Java
Tags.of(theBestStack).add("StackType", "TheBest", TagProps.builder() .includeResourceTypes(Arrays.asList("AWS::EC2::Subnet")) .build());
C#
Tags.Of(theBestStack).Add("StackType", "TheBest", new TagProps { IncludeResourceTypes = ["AWS::EC2::Subnet"] });
Go
awscdk.Tags_Of(theBestStack).Add(jsii.String("StackType"), jsii.String("TheBest"), &awscdk.TagProps{ IncludeResourceTypes: &[]*string{jsii.String("AWS::EC2::Subnet")}, })

标记单个构造

Tags.of(scope).add(key, value)是向中的构造添加标签的标准方法。 AWS CDK它的树木漫步行为以递归方式标记给定范围内的所有可标记资源,几乎总是你想要的。但是,有时你需要标记一个特定的任意构造(或多个构造)。

其中一种情况涉及应用标签,其值来自被标记的构造的某个属性。标准标记方法以递归方式将相同的键和值应用于作用域内所有匹配的资源。但是,这里每个标记构造的值可能不同。

标签是使用方面实现的,CDK 在您使用的指定范围内为每个构造调用Tags.of(scope)标签visit()的方法。我们可以Tag.visit()直接调用将标签应用于单个构造。

TypeScript
new cdk.Tag(key, value).visit(scope);
JavaScript
new cdk.Tag(key, value).visit(scope);
Python
cdk.Tag(key, value).visit(scope)
Java
Tag.Builder.create(key, value).build().visit(scope);
C#
new Tag(key, value).Visit(scope);
Go
awscdk.NewTag(key, value, &awscdk.TagProps{}).Visit(scope)

你可以标记作用域下的所有构造,但让标签的值从每个构造的属性中派生。为此,请编写一个方面并在该方面的visit()方法中应用标签,如前面的示例所示。然后,使用将宽高比添加到所需的范围Aspects.of(scope).add(aspect)

以下示例将标签应用于堆栈中包含资源路径的每个资源。

TypeScript
class PathTagger implements cdk.IAspect { visit(node: IConstruct) { new cdk.Tag("aws-cdk-path", node.node.path).visit(node); } } stack = new MyStack(app); cdk.Aspects.of(stack).add(new PathTagger())
JavaScript
class PathTagger { visit(node) { new cdk.Tag("aws-cdk-path", node.node.path).visit(node); } } stack = new MyStack(app); cdk.Aspects.of(stack).add(new PathTagger())
Python
@jsii.implements(cdk.IAspect) class PathTagger: def visit(self, node: IConstruct): cdk.Tag("aws-cdk-path", node.node.path).visit(node) stack = MyStack(app) cdk.Aspects.of(stack).add(PathTagger())
Java
final class PathTagger implements IAspect { public void visit(IConstruct node) { Tag.Builder.create("aws-cdk-path", node.getNode().getPath()).build().visit(node); } } stack stack = new MyStack(app); Aspects.of(stack).add(new PathTagger());
C#
public class PathTagger : IAspect { public void Visit(IConstruct node) { new Tag("aws-cdk-path", node.Node.Path).Visit(node); } } var stack = new MyStack(app); Aspects.Of(stack).Add(new PathTagger);
提示

类中内置了条件标记的逻辑,包括优先Tag级、资源类型等。在对任意资源应用标签时,您可以使用这些功能;如果不满足条件,则不会应用标签。此外,该Tag类仅标记可标记的资源,因此在应用标签之前,您无需测试构造是否可标记。