Miscelazioni - AWS Cloud Development Kit (AWS CDK) v2

Questa è la AWS CDK v2 Developer Guide. Il vecchio CDK v1 è entrato in manutenzione il 1° giugno 2022 e ha terminato il supporto il 1° giugno 2023.

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Miscelazioni

I mixin sono funzionalità riutilizzabili che si applicano ai costrutti utilizzando il metodo. .with() Aggiungono funzionalità ai costrutti L1 (CloudFormation-level) e L2 (intent-based), come il controllo delle versioni, l'eliminazione automatica degli oggetti o il blocco dell'accesso pubblico. Ogni mixin funziona su una singola risorsa. Per connettere due risorse, usa invece Facades.

Ogni mixin si rivolge a un tipo di risorsa specifico e prende il nome da quella risorsa. Ad esempio, è BucketVersioning destinato ai bucket Amazon S3. È possibile accedere ai mixin tramite lo spazio dei mixins nomi di ciascun modulo di servizio, ad esempio. s3.mixins

Applica Mixins

I mixin vengono applicati utilizzando il .with() metodo, disponibile su tutti i costrutti. Puoi concatenare più mixin:

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

Ogni mixin dichiara quali tipi di risorse supporta. Se applicate un mixin a un costrutto che non supporta, viene ignorato silenziosamente. Ciò significa che potete applicare in modo sicuro i mixin in modo ampio senza preoccuparvi dei disallineamenti tra i tipi. Se devi assicurarti che venga applicato un mixin, usa o. requireAll()requireAny()

Usa Mixins con costrutti L1 e L2

I mixin funzionano con i costrutti L1 e L2. Quando applicate un mixin a un costrutto L2, si applica anche alla risorsa L1 sottostante.

L'esempio seguente mostra come applicare i mixin a entrambi i costrutti L1 e L2:

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

Mixins e proprietà di costruzione

I mixin e le proprietà di costruzione funzionano insieme. Le proprietà del costrutto L2 impostano una risorsa al momento della creazione. I mixin possono essere applicati in qualsiasi momento.

Utilizzate le proprietà del costrutto L2 quando

Stai utilizzando un costrutto L2 e la proprietà che ti serve è disponibile. Questo è l'approccio più semplice.

Usa Mixins quando
  • Stai lavorando con un costrutto L1 e desideri funzionalità simili a L2.

  • Volete aggiungere una funzionalità a un costrutto L2 che non è disponibile come proprietà.

  • Desiderate applicare la stessa funzionalità a più costrutti di tipi diversi.

I mixin non sostituiscono le proprietà del costrutto. Non possono rendere facoltativa una proprietà obbligatoria o modificare i valori predefiniti.

Applica Mixins a più costrutti

L'Mixins.of()API offre un maggiore controllo sul modo in cui i mixin vengono applicati in un ambito. Invece di richiamare .with() singoli costrutti, puoi applicare un mixin a tutti i costrutti corrispondenti in uno stack o in un ambito contemporaneamente:

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

Per impostazione predefinita, i costrutti che non supportano il mixin vengono ignorati silenziosamente. Si usa requireAll() per affermare che il mixin viene applicato a ogni costrutto della selezione o per affermare che viene applicato requireAny() ad almeno uno. Questo è utile per far sì che le risorse abbiano una configurazione richiesta:

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

Miscele e aspetti

Mixin e Aspects sono correlati ma hanno scopi diversi:

  • I mixin vengono applicati immediatamente quando si chiama. .with() Scegliete esattamente a quali costrutti applicarli.

  • Gli aspetti si applicano durante la sintesi a tutti i costrutti di un ambito. Usali per politiche e controlli generali.

Usa Mixins per aggiungere una funzionalità a costrutti specifici. Usa Aspects per applicare regole o applicare modifiche all'intera applicazione.