方面 - AWS Cloud Development Kit (AWS CDK) v2

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

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

方面

Aspects 是一种将操作应用于给定作用域内所有构造的方法。该方面可以修改结构,例如通过添加标签。或者它可以验证一些关于构造状态的信息,例如确保所有存储桶都已加密。

要将某个方面应用于同一个作用域内的构造和所有构造,请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());