如何将标签添加到 AWS-CDK 构造
How to add a tag to an AWS-CDK construct
如何将标签添加到特定于 AWS-CDK 的构造,或者如何更好地为堆栈中创建的所有资源添加一个标签定义?
根据 aws-cdk 文档,您可以向所有 constructs/ressources 添加标签。标签将继承到相同 "tree" 内的构造。太棒了。
基于java使用aws-cdk的示例:
MyStack stack = new MyStack(app, "nameOfStack");
Tag.add(stack, "tag_foo", "tag_foo");
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.
来自 aws-cdk 文档的示例:
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');
因为您可能希望向构造添加多个标签,
传递标签对象很方便。您可以使用 cdk 中的方面来下降节点以查找您类型的节点并将您想要应用的任何内容应用到所述节点。
以下示例添加标签。
export class TagAspect implements cdk.IAspect {
private readonly tags: Object;
constructor(tags: Object) {
this.tags = tags;
}
public visit(node: cdk.IConstruct): void {
if (node instanceof cdk.Stack) {
Object.entries(this.tags).forEach( ([key, value]) => {
cdk.Tag.add(node,key,value);
});
}}}
然后在 Stack 中,您要将方面应用到 运行
this.node.applyAspect(new TagAspect({"tag1":"mytag1","tag2":"another tag","tag3":"andanother"}));
使用 Java
SDK:
public class CdkInitClusterApp {
public static void main(final String[] args) {
final App app = new App();
final CdkInitClusterStack cdkInitClusterStack = new CdkInitClusterStack(app, "CdkInitClusterStack");
Tag.add(cdkInitClusterStack, "Project", "Value");
app.synth();
}
}
然后重新创建 jar
mvn clean compile package
和运行 cdk diff
验证更改,输出将类似于以下内容:
(base) [alessiosavi@localhost cdk-init-cluster]$ cdk diff
CdkInitClusterStack
Resources
[~] AWS::DynamoDB::Table cdk-test-table cdktesttableB0274F47
└─ [+] Tags
└─ [{"Key":"Project","Value":"Value"}]
以上答案使用了已弃用的语法。
标记整个应用程序的较新版本:
const app = new cdk.App();
new SomeStack(app, 'SomeStack');
Tags.of(app).add("app", "my-app-name-here");
您也可以只标记单个堆栈:
const app = new cdk.App();
const stack = new SomeStack(app, 'SomeStack');
Tags.of(stack).add("stack-name", "SomeStack");
或个别构造体:
const usersTable = new dynamodb.Table(this, 'Users');
Tags.of(usersTable).add("owner", "team-andromeda");
标签将按层次应用于 sub-Constructs。
您可以在 Python
中向您的 CDK 应用添加标签,如下所示:
from aws_cdk import core
app = core.App()
core.Tags.of(app).add("TEAM", "TeamA") # add tags to the entire app (all resources created by this app)
lambda_stack = LambdaStack(app, 'lambda-stack')
core.Tags.of(lambda_stack).add("TEAM", "TeamA") # add tags to the entire stack (all resources of this stack)
...或构造:
lambda_role = iam.Role(self,
assumed_by=iam.ServicePrincipal(service='lambda.amazonaws.com'),
role_name='lambda-role'
)
core.Tags.of(lambda_role).add("TEAM", "TeamA") # add tags to this construct (add tags to just this role)
如何将标签添加到特定于 AWS-CDK 的构造,或者如何更好地为堆栈中创建的所有资源添加一个标签定义?
根据 aws-cdk 文档,您可以向所有 constructs/ressources 添加标签。标签将继承到相同 "tree" 内的构造。太棒了。
基于java使用aws-cdk的示例:
MyStack stack = new MyStack(app, "nameOfStack");
Tag.add(stack, "tag_foo", "tag_foo");
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.
来自 aws-cdk 文档的示例:
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');
因为您可能希望向构造添加多个标签, 传递标签对象很方便。您可以使用 cdk 中的方面来下降节点以查找您类型的节点并将您想要应用的任何内容应用到所述节点。 以下示例添加标签。
export class TagAspect implements cdk.IAspect {
private readonly tags: Object;
constructor(tags: Object) {
this.tags = tags;
}
public visit(node: cdk.IConstruct): void {
if (node instanceof cdk.Stack) {
Object.entries(this.tags).forEach( ([key, value]) => {
cdk.Tag.add(node,key,value);
});
}}}
然后在 Stack 中,您要将方面应用到 运行
this.node.applyAspect(new TagAspect({"tag1":"mytag1","tag2":"another tag","tag3":"andanother"}));
使用 Java
SDK:
public class CdkInitClusterApp {
public static void main(final String[] args) {
final App app = new App();
final CdkInitClusterStack cdkInitClusterStack = new CdkInitClusterStack(app, "CdkInitClusterStack");
Tag.add(cdkInitClusterStack, "Project", "Value");
app.synth();
}
}
然后重新创建 jar
mvn clean compile package
和运行 cdk diff
验证更改,输出将类似于以下内容:
(base) [alessiosavi@localhost cdk-init-cluster]$ cdk diff
CdkInitClusterStack
Resources
[~] AWS::DynamoDB::Table cdk-test-table cdktesttableB0274F47
└─ [+] Tags
└─ [{"Key":"Project","Value":"Value"}]
以上答案使用了已弃用的语法。
标记整个应用程序的较新版本:
const app = new cdk.App();
new SomeStack(app, 'SomeStack');
Tags.of(app).add("app", "my-app-name-here");
您也可以只标记单个堆栈:
const app = new cdk.App();
const stack = new SomeStack(app, 'SomeStack');
Tags.of(stack).add("stack-name", "SomeStack");
或个别构造体:
const usersTable = new dynamodb.Table(this, 'Users');
Tags.of(usersTable).add("owner", "team-andromeda");
标签将按层次应用于 sub-Constructs。
您可以在 Python
中向您的 CDK 应用添加标签,如下所示:
from aws_cdk import core
app = core.App()
core.Tags.of(app).add("TEAM", "TeamA") # add tags to the entire app (all resources created by this app)
lambda_stack = LambdaStack(app, 'lambda-stack')
core.Tags.of(lambda_stack).add("TEAM", "TeamA") # add tags to the entire stack (all resources of this stack)
...或构造:
lambda_role = iam.Role(self,
assumed_by=iam.ServicePrincipal(service='lambda.amazonaws.com'),
role_name='lambda-role'
)
core.Tags.of(lambda_role).add("TEAM", "TeamA") # add tags to this construct (add tags to just this role)