混合 - AWS 雲端開發套件 (AWS CDK) v2

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

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

混合

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

每個混音都以特定資源類型為目標,並以該資源命名。例如, 以 Amazon S3 儲存貯體為BucketVersioning目標。您可以透過每個服務模組的mixins命名空間存取混合,例如 s3.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 都會宣告其支援的資源類型。如果您將混音套用至不支援的建構,則會無提示地略過該混音。這表示您可以安全地廣泛套用混合,而不必擔心類型不相符。如果您需要確保已套用混音,請使用 requireAll()或 requireAny()

使用混合搭配 L1 和 L2 建構

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

下列範例顯示如何將混合套用至 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())

混合與建構屬性

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

在 時使用 L2 建構屬性

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

在 時使用 混合
  • 您正在使用 L1 建構,並且想要L2-like 的功能。

  • 您想要將功能新增至無法做為 屬性使用的 L2 建構。

  • 您想要在不同類型的多個建構中套用相同的功能。

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

將混合套用至多個建構

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

範例
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())

根據預設,不支援混音的建構會無提示地略過。使用 requireAll()宣告混音會套用到選取項目中的每個建構,或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())

混合和觀點

混合和觀點相關,但用途不同:

  • 當您呼叫 時,會立即套用混合.with()。您可以選擇要套用建構的確切項目。

  • 在合成期間,視觀表適用於範圍中的所有建構。將它們用於廣泛的政策和檢查。

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

  • Facades – 將資源與 IAM 主體和其他 服務連線。

  • 面向 – 在整個應用程式中套用變更或驗證建構。

  • 建構 – 了解 L1, L2 和 L3 建構。

  • 自訂建構 – 使用逃生艙和原始覆寫自訂建構。