面向 - AWS Cloud Development Kit (AWS CDK) V2

這是 AWS CDK v2 開發人員指南。較舊的 CDK 第 1 版已於 2022 年 6 月 1 日進入維護,並於 2023 年 6 月 1 日結束支援。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

面向

方面是一種將操作應用於給定範圍內的所有構造的方法。該方面可以修改結構,例如通過添加標籤。或者它可以驗證有關構造狀態的內容,例如確保所有存儲桶都已加密。

若要將縱橫比套用至建構及相同範圍內的所有建構,請Aspects.of(SCOPE).add()使用新的縱橫比呼叫,如下列範例所示。

TypeScript
Aspects.of(myConstruct).add(new SomeAspect(...));
JavaScript
Aspects.of(myConstruct).add(new SomeAspect(...));
Python
Aspects.of(my_construct).add(SomeAspect(...))
Java
Aspects.of(myConstruct).add(new SomeAspect(...));
C#
Aspects.Of(myConstruct).add(new SomeAspect(...));
Go
awscdk.Aspects_Of(stack).Add(awscdk.NewTag(...))

AWS CDK 使用方面來標記資源,但框架也可以用於其他目的。例如,您可以使用它來驗證或變更更高層級建構為您定義的 AWS CloudFormation 資源。

各個方面的細節

各個方面採用訪問者模式。一個方面是一個實現以下接口的類。

TypeScript
interface IAspect { visit(node: IConstruct): void;}
JavaScript

JavaScript 沒有作為語言功能的接口。因此,一個方面只是具有接受要操作的節點的visit方法的類的一個實例。

Python

Python 沒有接口作為一種語言功能。因此,一個方面只是具有接受要操作的節點的visit方法的類的一個實例。

Java
public interface IAspect { public void visit(Construct node); }
C#
public interface IAspect { void Visit(IConstruct node); }
Go
type IAspect interface { Visit(node constructs.IConstruct) }

當您調用時Aspects.of(SCOPE).add(...),構造將方面添加到內部方面列表中。您可以使用取得清單Aspects.of(SCOPE)

準備階段,會以由上而下的順序 AWS CDK 呼叫建構的物件及其每個子項的visit方法。

visit方法可以自由地更改構造中的任何內容。在強類型語言中,在訪問特定於構造的屬性或方法之前,將接收的構造轉換為更特定的類型。

方面不會跨越Stage構造邊界傳播,因為在定義之後Stages是獨立且不可變的。如果您希望它們訪問. Stage Stage

範例

下列範例會驗證堆疊中建立的所有值區是否已啟用版本控制。該方面將錯誤註釋添加到驗證失敗的構造中。這會導致synth作業失敗,並防止部署產生的雲端組件。

TypeScript
class BucketVersioningChecker implements IAspect { public visit(node: IConstruct): void { // See that we're dealing with a CfnBucket if (node instanceof s3.CfnBucket) { // Check for versioning property, exclude the case where the property // can be a token (IResolvable). if (!node.versioningConfiguration || (!Tokenization.isResolvable(node.versioningConfiguration) && node.versioningConfiguration.status !== 'Enabled')) { Annotations.of(node).addError('Bucket versioning is not enabled'); } } } } // Later, apply to the stack Aspects.of(stack).add(new BucketVersioningChecker());
JavaScript
class BucketVersioningChecker { visit(node) { // See that we're dealing with a CfnBucket if ( node instanceof s3.CfnBucket) { // Check for versioning property, exclude the case where the property // can be a token (IResolvable). if (!node.versioningConfiguration || !Tokenization.isResolvable(node.versioningConfiguration) && node.versioningConfiguration.status !== 'Enabled')) { Annotations.of(node).addError('Bucket versioning is not enabled'); } } } } // Later, apply to the stack Aspects.of(stack).add(new BucketVersioningChecker());
Python
@jsii.implements(cdk.IAspect) class BucketVersioningChecker: def visit(self, node): # See that we're dealing with a CfnBucket if isinstance(node, s3.CfnBucket): # Check for versioning property, exclude the case where the property # can be a token (IResolvable). if (not node.versioning_configuration or not Tokenization.is_resolvable(node.versioning_configuration) and node.versioning_configuration.status != "Enabled"): Annotations.of(node).add_error('Bucket versioning is not enabled') # Later, apply to the stack Aspects.of(stack).add(BucketVersioningChecker())
Java
public class BucketVersioningChecker implements IAspect { @Override public void visit(Construct node) { // See that we're dealing with a CfnBucket if (node instanceof CfnBucket) { CfnBucket bucket = (CfnBucket)node; Object versioningConfiguration = bucket.getVersioningConfiguration(); if (versioningConfiguration == null || !Tokenization.isResolvable(versioningConfiguration.toString()) && !versioningConfiguration.toString().contains("Enabled")) Annotations.of(bucket.getNode()).addError("Bucket versioning is not enabled"); } } } // Later, apply to the stack Aspects.of(stack).add(new BucketVersioningChecker());
C#
class BucketVersioningChecker : Amazon.Jsii.Runtime.Deputy.DeputyBase, IAspect { public void Visit(IConstruct node) { // See that we're dealing with a CfnBucket if (node is CfnBucket) { var bucket = (CfnBucket)node; if (bucket.VersioningConfiguration is null || !Tokenization.IsResolvable(bucket.VersioningConfiguration) && !bucket.VersioningConfiguration.ToString().Contains("Enabled")) Annotations.Of(bucket.Node).AddError("Bucket versioning is not enabled"); } } } // Later, apply to the stack Aspects.Of(stack).add(new BucketVersioningChecker());