믹싱 - AWS 클라우드 개발 키트(AWS CDK) v2

CDK AWS v2 개발자 안내서입니다. 이전 CDK v1은 2022년 6월 1일에 유지 관리에 들어갔으며 2023년 6월 1일에 지원이 종료되었습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

믹싱

Mixins는 .with() 메서드를 사용하여 구문에 적용하는 재사용 가능한 기능입니다. 버전 관리, 객체 자동 삭제 또는 퍼블릭 액세스 차단과 같은 기능을 L1(CloudFormation 수준) 및 L2(의도 기반) 구성 모두에 추가합니다. 각 혼합기는 단일 리소스에서 작동합니다. 두 리소스를 연결하려면 대신 Facades를 사용합니다.

각 믹싱은 특정 리소스 유형을 대상으로 하며 해당 리소스의 이름을 따서 명명됩니다. 예를 들어는 Amazon S3 버킷을 BucketVersioning 대상으로 합니다. 와 같은 각 서비스 모듈의 mixins 네임스페이스를 통해 믹싱에 액세스합니다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은 지원하는 리소스 유형을 선언합니다. 지원하지 않는 구문에 혼합기를 적용하면 자동으로 건너뜁니다. 즉, 유형 불일치에 대한 걱정 없이 믹싱을 광범위하게 안전하게 적용할 수 있습니다. 믹싱이 적용되었는지 확인해야 하는 경우 requireAll() 또는 requireAny()를 사용합니다.

L1 및 L2 구문과 함께 Mixins 사용

믹서는 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())

Mixins 대 구문 속성

혼합 및 구문 속성이 함께 작동합니다. L2 구문 속성은 리소스를 생성할 때 리소스를 설정합니다. 믹서는 언제든지 적용할 수 있습니다.

다음과 같은 경우 L2 구문 속성을 사용합니다.

L2 구문을 사용 중이며 필요한 속성을 사용할 수 있습니다. 가장 간단한 접근 방식입니다.

다음과 같은 경우 Mixins를 사용합니다.
  • L1 구문으로 작업 중이며 L2-like 기능을 원합니다.

  • 속성으로 사용할 수 없는 L2 구문에 기능을 추가하려고 합니다.

  • 서로 다른 유형의 여러 구문에 동일한 기능을 적용하려고 합니다.

Mixins는 구문 속성을 대체하지 않습니다. 필수 속성을 선택 사항으로 설정하거나 기본값을 변경할 수 없습니다.

여러 구문에 Mixins 적용

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(). 적용할 구문을 정확히 선택합니다.

  • 측면은 합성 중에 범위의 모든 구문에 적용됩니다. 광범위한 정책 및 검사에 사용합니다.

Mixins를 사용하여 특정 구문에 기능을 추가합니다. Aspects를 사용하여 규칙을 적용하거나 전체 애플리케이션에 변경 사항을 적용합니다.

  • Facades - 리소스를 IAM 보안 주체 및 기타 서비스와 연결합니다.

  • 측면 - 전체 애플리케이션에 변경 사항을 적용하거나 구문을 검증합니다.

  • 구문 - L1, L2 및 L3 구문에 대해 알아봅니다.

  • 구문 사용자 지정 - 이스케이프 해치 및 원시 재정의를 사용하여 구문을 사용자 지정합니다.