這是 AWS CDK v2 開發人員指南。較舊的 CDK 第 1 版已於 2022 年 6 月 1 日進入維護,並於 2023 年 6 月 1 日結束支援。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
標記
標籤是您可以新增至應用程式中建構的 AWS CDK 資訊索引鍵值元素。套用至指定建構的標籤也會套用至其所有可加標籤的子系。標籤包含在從您的應用程序合成的 AWS CloudFormation 模板中,並應用於其部署的 AWS 資源。出於以下目的,您可以使用標籤來識別和分類資源:
-
簡化管理
-
成本分配
-
存取控制
-
您設計的任何其他目的
使用標籤
此Tags
類別包含靜態方法of()
,您可以透過該方法將標籤新增至指定的建構,或從中移除標籤。
標記是使用來實現的面向。方面是一種將操作(例如標記)應用於給定範圍內的所有構造的方法。
下列範例會將含值值的標籤鍵套用至建構。
- 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
)
-
僅適用於加入 ()。依預設,標籤會套用至在「自動調整比例」群組中啟動的執行個體。將此屬性設定為 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 的標籤 tagname 套用至建構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 的標籤 tagname,但不會從類型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
類僅標記可標記資源,因此在應用標籤之前不需要測試構造是否可標記。