这是 AWS CDK v2 开发者指南。旧版 CDK v1 于 2022 年 6 月 1 日进入维护阶段,并于 2023 年 6 月 1 日终止支持。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
mixins
Mixin 是可重复使用的功能,你可以使用该.with()方法将其应用于构造。它们为 L1(CloudFormation级别)和 L2(基于意图)结构添加了功能,例如版本控制、自动删除对象或阻止公共访问。每个 mixin 都在单个资源上运行。要连接两个资源,请改用 Facades。
每个 mixin 都以特定的资源类型为目标,并以该资源命名。例如,以 Amazon S3 存储桶为BucketVersioning目标。您可以通过每个服务模块的mixins命名空间访问 mixin,例如s3.mixins。
应用 Mixins
您可以使用该方法应用混合,该.with()方法适用于所有构造。你可以将多个混音链接在一起:
例
- TypeScript
-
import * as s3 from 'aws-cdk-lib/aws-s3';
const bucket = new s3.CfnBucket(this, 'MyBucket')
.with(new s3.mixins.BucketVersioning())
.with(new s3.mixins.BucketBlockPublicAccess());
- JavaScript
-
const s3 = require('aws-cdk-lib/aws-s3');
const bucket = new s3.CfnBucket(this, 'MyBucket')
.with(new s3.mixins.BucketVersioning())
.with(new s3.mixins.BucketBlockPublicAccess());
- Python
-
import aws_cdk.aws_s3 as s3
bucket = s3.CfnBucket(self, "MyBucket") \
.with_(s3.mixins.BucketVersioning()) \
.with_(s3.mixins.BucketBlockPublicAccess())
- Java
-
import software.amazon.awscdk.services.s3.*;
CfnBucket bucket = new CfnBucket(this, "MyBucket");
bucket.with(new BucketVersioning());
bucket.with(new BucketBlockPublicAccess());
- C#
-
using Amazon.CDK.AWS.S3;
var bucket = new CfnBucket(this, "MyBucket");
bucket.With(new BucketVersioning());
bucket.With(new BucketBlockPublicAccess());
- Go
-
bucket := awss3.NewCfnBucket(stack, jsii.String("MyBucket"), nil)
bucket.With(awss3.NewBucketVersioning())
bucket.With(awss3.NewBucketBlockPublicAccess())
每个 mixin 都声明它支持哪些资源类型。如果你将 mixin 应用于它不支持的构造,它将被静默跳过。这意味着您可以安全地广泛应用混合,而不必担心类型不匹配。如果您需要确保使用混合,请使用requireAll()或requireAny()。
使用带有 L1 和 L2 结构的 Mixins
Mixin 可与 L1 和 L2 结构一起使用。当你将 mixin 应用于 L2 构造时,它也适用于其背后的 L1 资源。
以下示例说明如何将 mixin 应用于 L1 和 L2 构造:
例
- TypeScript
-
import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';
// Using a mixin with an L1 construct
new s3.CfnBucket(this, 'L1Bucket')
.with(new s3.mixins.BucketVersioning());
// Using a mixin with an L2 construct
new s3.Bucket(this, 'L2Bucket', {
removalPolicy: cdk.RemovalPolicy.DESTROY,
}).with(new s3.mixins.BucketAutoDeleteObjects());
- JavaScript
-
const cdk = require('aws-cdk-lib');
const s3 = require('aws-cdk-lib/aws-s3');
// Using a mixin with an L1 construct
new s3.CfnBucket(this, 'L1Bucket')
.with(new s3.mixins.BucketVersioning());
// Using a mixin with an L2 construct
new s3.Bucket(this, 'L2Bucket', {
removalPolicy: cdk.RemovalPolicy.DESTROY,
}).with(new s3.mixins.BucketAutoDeleteObjects());
- Python
-
import aws_cdk as cdk
import aws_cdk.aws_s3 as s3
# Using a mixin with an L1 construct
s3.CfnBucket(self, "L1Bucket") \
.with_(s3.mixins.BucketVersioning())
# Using a mixin with an L2 construct
s3.Bucket(self, "L2Bucket",
removal_policy=cdk.RemovalPolicy.DESTROY,
).with_(s3.mixins.BucketAutoDeleteObjects())
- Java
-
import software.amazon.awscdk.*;
import software.amazon.awscdk.services.s3.*;
// Using a mixin with an L1 construct
CfnBucket l1Bucket = new CfnBucket(this, "L1Bucket");
l1Bucket.with(new BucketVersioning());
// Using a mixin with an L2 construct
Bucket l2Bucket = Bucket.Builder.create(this, "L2Bucket")
.removalPolicy(RemovalPolicy.DESTROY)
.build();
l2Bucket.with(new BucketAutoDeleteObjects());
- C#
-
using Amazon.CDK;
using Amazon.CDK.AWS.S3;
// Using a mixin with an L1 construct
var l1Bucket = new CfnBucket(this, "L1Bucket");
l1Bucket.With(new BucketVersioning());
// Using a mixin with an L2 construct
var l2Bucket = new Bucket(this, "L2Bucket", new BucketProps
{
RemovalPolicy = RemovalPolicy.DESTROY
});
l2Bucket.With(new BucketAutoDeleteObjects());
- Go
-
l1Bucket := awss3.NewCfnBucket(stack, jsii.String("L1Bucket"), nil)
l1Bucket.With(awss3.NewBucketVersioning())
l2Bucket := awss3.NewBucket(stack, jsii.String("L2Bucket"), &awss3.BucketProps{
RemovalPolicy: awscdk.RemovalPolicy_DESTROY,
})
l2Bucket.With(awss3.NewBucketAutoDeleteObjects())
混合物与构造属性
Mixin 和构造属性可以协同工作。L2 构造属性会在您创建资源时对其进行设置。Mixins 可以随时使用。
- 在以下情况下使用 L2 构造属性
-
您使用的是 L2 构造,并且所需的属性已可用。这是最简单的方法。
- 在以下情况下使用 Mixins
-
-
你正在使用 L1 构造,想要类似 L2 的功能。
-
您想向 L2 构造中添加不可用作属性的功能。
-
你想在多个不同类型的构造中应用相同的功能。
Mixin 不能取代构造属性。他们不能将必需的属性设为可选属性或更改默认值。
将 Mixins 应用于多个构造
该 Mixins.of() API 可以更好地控制如何跨作用域应用混合。与其调用.with()单个构造,不如同时将 mixin 应用于堆栈或作用域中的所有匹配构造:
例
- TypeScript
-
import { Mixins } from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';
// Apply to all supported constructs in the stack
Mixins.of(stack).apply(new s3.mixins.BucketVersioning());
- JavaScript
-
const { Mixins } = require('aws-cdk-lib');
const s3 = require('aws-cdk-lib/aws-s3');
// Apply to all supported constructs in the stack
Mixins.of(stack).apply(new s3.mixins.BucketVersioning());
- Python
-
from aws_cdk import Mixins
import aws_cdk.aws_s3 as s3
# Apply to all supported constructs in the stack
Mixins.of(stack).apply(s3.mixins.BucketVersioning())
- Java
-
import software.amazon.awscdk.Mixins;
import software.amazon.awscdk.services.s3.*;
// Apply to all supported constructs in the stack
Mixins.of(stack).apply(new BucketVersioning());
- C#
-
using Amazon.CDK;
using Amazon.CDK.AWS.S3;
// Apply to all supported constructs in the stack
Mixins.Of(stack).Apply(new BucketVersioning());
- Go
-
awscdk.Mixins_Of(stack, nil).Apply(awss3.NewBucketVersioning())
默认情况下,不支持 mixin 的构造会被静默跳过。requireAll()用于断言 mixin 已应用于选择中的每个构造,或者requireAny()断言它已应用于至少一个构造。这对于强制资源具有必需的配置很有用:
例
- TypeScript
-
// Throws an error if any construct in the scope doesn't support the mixin
Mixins.of(stack)
.requireAll()
.apply(new s3.mixins.BucketVersioning());
- JavaScript
-
// Throws an error if any construct in the scope doesn't support the mixin
Mixins.of(stack)
.requireAll()
.apply(new s3.mixins.BucketVersioning());
- Python
-
# Throws an error if any construct in the scope doesn't support the mixin
Mixins.of(stack) \
.require_all() \
.apply(s3.mixins.BucketVersioning())
- Java
-
// Throws an error if any construct in the scope doesn't support the mixin
Mixins.of(stack)
.requireAll()
.apply(new BucketVersioning());
- C#
-
// Throws an error if any construct in the scope doesn't support the mixin
Mixins.Of(stack)
.RequireAll()
.Apply(new BucketVersioning());
- Go
-
awscdk.Mixins_Of(stack, nil).RequireAll().Apply(awss3.NewBucketVersioning())
混音和方面
Mixins 和 As p ects 是相关的,但用途不同:
使用 Mixins 向特定构造添加功能。使用 Aspects 在整个应用程序中强制执行规则或应用更改。