

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

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

# 混合
<a name="mixins"></a>

混合是可重複使用的功能，您可以使用 `.with()`方法套用至建構。它們將功能新增至 L1 (CloudFormation 層級） 和 L2 （意圖型） 建構，例如版本控制、自動刪除物件或封鎖公開存取。每個混音適用於單一資源。若要連接兩個資源，請改用 [Facades](facades.md)。

每個混音都以特定資源類型為目標，並以該資源命名。例如， 以 Amazon S3 儲存貯體為`BucketVersioning`目標。您可以透過每個服務模組的`mixins`命名空間存取混合，例如 `s3.mixins`。

## 套用 混合
<a name="mixins-basic"></a>

您可以使用 方法套用混合，該`.with()`方法適用於所有建構。您可以將多個混音鏈結在一起：

**Example**  

```
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());
```

```
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());
```

```
import aws_cdk.aws_s3 as s3

bucket = s3.CfnBucket(self, "MyBucket") \
    .with_(s3.mixins.BucketVersioning()) \
    .with_(s3.mixins.BucketBlockPublicAccess())
```

```
import software.amazon.awscdk.services.s3.*;

CfnBucket bucket = new CfnBucket(this, "MyBucket");
bucket.with(new BucketVersioning());
bucket.with(new BucketBlockPublicAccess());
```

```
using Amazon.CDK.AWS.S3;

var bucket = new CfnBucket(this, "MyBucket");
bucket.With(new BucketVersioning());
bucket.With(new BucketBlockPublicAccess());
```

```
bucket := awss3.NewCfnBucket(stack, jsii.String("MyBucket"), nil)
bucket.With(awss3.NewBucketVersioning())
bucket.With(awss3.NewBucketBlockPublicAccess())
```

每個 mixin 都會宣告其支援的資源類型。如果您將混音套用至不支援的建構，則會無提示地略過該混音。這表示您可以安全地廣泛套用混合，而不必擔心類型不相符。如果您需要確保已套用混音，請使用 [`requireAll()`或 `requireAny()`](#mixins-advanced)。

## 使用混合搭配 L1 和 L2 建構
<a name="mixins-l1-l2"></a>

混合適用於 L1 和 L2 建構。當您將混音套用至 L2 建構時，也會套用至其後方的 L1 資源。

下列範例顯示如何將混合套用至 L1 和 L2 建構：

**Example**  

```
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());
```

```
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());
```

```
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())
```

```
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());
```

```
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());
```

```
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())
```

## 混合與建構屬性
<a name="mixins-vs-props"></a>

混合和建構屬性可一起運作。L2 建構屬性會在您建立資源時對其進行設定。混合可隨時套用。

在 時使用 L2 建構屬性  
您正在使用 L2 建構，且您需要的 屬性可用。這是最簡單的方法。

在 時使用 混合  
+ 您正在使用 L1 建構，並且想要L2-like 的功能。
+ 您想要將功能新增至無法做為 屬性使用的 L2 建構。
+ 您想要在不同類型的多個建構中套用相同的功能。

混合不會取代建構屬性。它們無法將必要的屬性設為選用或變更預設值。

## 將混合套用至多個建構
<a name="mixins-advanced"></a>

`Mixins.of()` API 提供更多控制跨範圍套用混合的方式。您可以一次將混音套用至堆疊或範圍中的所有相符建構，而不是`.with()`呼叫個別建構：

**Example**  

```
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());
```

```
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());
```

```
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())
```

```
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());
```

```
using Amazon.CDK;
using Amazon.CDK.AWS.S3;

// Apply to all supported constructs in the stack
Mixins.Of(stack).Apply(new BucketVersioning());
```

```
awscdk.Mixins_Of(stack, nil).Apply(awss3.NewBucketVersioning())
```

根據預設，不支援混音的建構會無提示地略過。使用 `requireAll()`宣告混音會套用到選取項目中的每個建構，或`requireAny()`宣告它至少套用到一個建構。這有助於強制 資源具有必要的組態：

**Example**  

```
// Throws an error if any construct in the scope doesn't support the mixin
Mixins.of(stack)
  .requireAll()
  .apply(new s3.mixins.BucketVersioning());
```

```
// Throws an error if any construct in the scope doesn't support the mixin
Mixins.of(stack)
  .requireAll()
  .apply(new s3.mixins.BucketVersioning());
```

```
# Throws an error if any construct in the scope doesn't support the mixin
Mixins.of(stack) \
    .require_all() \
    .apply(s3.mixins.BucketVersioning())
```

```
// Throws an error if any construct in the scope doesn't support the mixin
Mixins.of(stack)
        .requireAll()
        .apply(new BucketVersioning());
```

```
// Throws an error if any construct in the scope doesn't support the mixin
Mixins.Of(stack)
    .RequireAll()
    .Apply(new BucketVersioning());
```

```
awscdk.Mixins_Of(stack, nil).RequireAll().Apply(awss3.NewBucketVersioning())
```

## 混合和觀點
<a name="mixins-aspects"></a>

混合和[觀點](aspects.md)相關，但用途不同：
+  當您呼叫 時，會立即套用**混合**`.with()`。您可以選擇要套用建構的確切項目。
+  **在合成期間，視觀**表適用於範圍中的所有建構。將它們用於廣泛的政策和檢查。

使用 混合將功能新增至特定建構。使用 Aspects 在整個應用程式中強制執行規則或套用變更。

## 相關資源
<a name="mixins-related"></a>
+  [Facades](facades.md) – 將資源與 IAM 主體和其他 服務連線。
+  [面向](aspects.md) – 在整個應用程式中套用變更或驗證建構。
+  [建構](constructs.md) – 了解 L1, L2 和 L3 建構。
+  [自訂建構](cfn-layer.md) – 使用逃生艙和原始覆寫自訂建構。