파케이드 - AWS 클라우드 개발 키트(AWS CDK) v2

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

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

파케이드

Facades는 리소스를 애플리케이션의 다른 부분과 연결하는 클래스입니다. 각 Facade는 하나의 리소스 유형을 대상으로 합니다. 예를 들어 클래스는 Amazon S3 버킷에 대한 액세스 권한을 부여BucketGrants하기 때문에 이름이 지정됩니다. Facades는 L1(CloudFormation 수준) 및 L2(의도 기반) 구문 모두에서 작동합니다.

일부 Facade는 지표 및 반영 클래스와 같은 대부분의 리소스에 대해 생성되고 바로 사용할 수 있습니다. 다른 리소스는 Grants 클래스와 같이 사용자 지정 로직이 필요한 리소스에 대해 수동으로 작성됩니다.

클래스를 부여합니다.

가장 널리 사용되는 Facades는 Grants 클래스입니다. 간단한 방법을 사용하여 AWS 리소스에 대한 액세스 권한을 부여할 수 있습니다. 예를 들어 Amazon S3 버킷에 BucketGrants를 사용하고 Amazon SNS 주제에 TopicGrants를 사용할 수 있습니다.

L2 구문에는 쉽게 액세스할 수 있는 grants 속성이 있습니다. 공장 방법을 사용하여 L1 구문에서 Grants 클래스를 생성할 수도 있습니다. 다음 예제에서는 두 가지 접근 방식을 모두 보여줍니다.

TypeScript
import * as s3 from 'aws-cdk-lib/aws-s3'; import * as iam from 'aws-cdk-lib/aws-iam'; // myRole is an IAM role defined elsewhere in your app // Using grants on an L2 construct (via the grants property) const l2Bucket = new s3.Bucket(this, 'L2Bucket'); l2Bucket.grants.read(myRole); // Using grants on an L1 construct (via the factory method) const l1Bucket = new s3.CfnBucket(this, 'L1Bucket'); s3.BucketGrants.fromBucket(l1Bucket).read(myRole);
JavaScript
const s3 = require('aws-cdk-lib/aws-s3'); const iam = require('aws-cdk-lib/aws-iam'); // myRole is an IAM role defined elsewhere in your app // Using grants on an L2 construct (via the grants property) const l2Bucket = new s3.Bucket(this, 'L2Bucket'); l2Bucket.grants.read(myRole); // Using grants on an L1 construct (via the factory method) const l1Bucket = new s3.CfnBucket(this, 'L1Bucket'); s3.BucketGrants.fromBucket(l1Bucket).read(myRole);
Python
import aws_cdk.aws_s3 as s3 import aws_cdk.aws_iam as iam # my_role is an IAM role defined elsewhere in your app # Using grants on an L2 construct (via the grants property) l2_bucket = s3.Bucket(self, "L2Bucket") l2_bucket.grants.read(my_role) # Using grants on an L1 construct (via the factory method) l1_bucket = s3.CfnBucket(self, "L1Bucket") s3.BucketGrants.from_bucket(l1_bucket).read(my_role)
Java
import software.amazon.awscdk.services.s3.*; import software.amazon.awscdk.services.iam.*; // myRole is an IAM role defined elsewhere in your app // Using grants on an L2 construct (via the grants property) Bucket l2Bucket = new Bucket(this, "L2Bucket"); l2Bucket.getGrants().read(myRole); // Using grants on an L1 construct (via the factory method) CfnBucket l1Bucket = new CfnBucket(this, "L1Bucket"); BucketGrants.fromBucket(l1Bucket).read(myRole);
C#
using Amazon.CDK.AWS.S3; using Amazon.CDK.AWS.IAM; // myRole is an IAM role defined elsewhere in your app // Using grants on an L2 construct (via the grants property) var l2Bucket = new Bucket(this, "L2Bucket"); l2Bucket.Grants.Read(myRole); // Using grants on an L1 construct (via the factory method) var l1Bucket = new CfnBucket(this, "L1Bucket"); BucketGrants.FromBucket(l1Bucket).Read(myRole);
Go
import ( "github.com/aws/jsii-runtime-go" awss3 "github.com/aws/aws-cdk-go/awscdk/v2/awss3" ) // myRole is an IAM role defined elsewhere in your app l2Bucket := awss3.NewBucket(stack, jsii.String("L2Bucket"), nil) l2Bucket.Grants().Read(myRole, nil) l1Bucket := awss3.NewCfnBucket(stack, jsii.String("L1Bucket"), nil) awss3.BucketGrants_FromBucket(l1Bucket).Read(myRole, nil)

권한 부여 및 권한에 대한 자세한 내용은 권한 부여를 참조하세요.

Facades를 Mixins와 함께 사용

Facades를 Mixins와 결합하여 L2-like 경험을 얻을 수 있습니다. L1 Mixins를 사용하여 리소스를 설정하고 Facades를 사용하여 액세스 권한을 부여합니다.

TypeScript
import * as s3 from 'aws-cdk-lib/aws-s3'; import * as iam from 'aws-cdk-lib/aws-iam'; // Configure the resource with Mixins const bucket = new s3.CfnBucket(this, 'MyBucket') .with(new s3.mixins.BucketVersioning()) .with(new s3.mixins.BucketBlockPublicAccess()); // Grant permissions using a Facade const role = new iam.Role(this, 'MyRole', { assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'), }); s3.BucketGrants.fromBucket(bucket).read(role);
JavaScript
const s3 = require('aws-cdk-lib/aws-s3'); const iam = require('aws-cdk-lib/aws-iam'); // Configure the resource with Mixins const bucket = new s3.CfnBucket(this, 'MyBucket') .with(new s3.mixins.BucketVersioning()) .with(new s3.mixins.BucketBlockPublicAccess()); // Grant permissions using a Facade const role = new iam.Role(this, 'MyRole', { assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'), }); s3.BucketGrants.fromBucket(bucket).read(role);
Python
import aws_cdk.aws_s3 as s3 import aws_cdk.aws_iam as iam # Configure the resource with Mixins bucket = s3.CfnBucket(self, "MyBucket") \ .with_(s3.mixins.BucketVersioning()) \ .with_(s3.mixins.BucketBlockPublicAccess()) # Grant permissions using a Facade role = iam.Role(self, "MyRole", assumed_by=iam.ServicePrincipal("lambda.amazonaws.com"), ) s3.BucketGrants.from_bucket(bucket).read(role)
Java
import software.amazon.awscdk.services.s3.*; import software.amazon.awscdk.services.iam.*; // Configure the resource with Mixins CfnBucket bucket = new CfnBucket(this, "MyBucket"); bucket.with(new BucketVersioning()); bucket.with(new BucketBlockPublicAccess()); // Grant permissions using a Facade Role role = Role.Builder.create(this, "MyRole") .assumedBy(new ServicePrincipal("lambda.amazonaws.com")) .build(); BucketGrants.fromBucket(bucket).read(role);
C#
using Amazon.CDK.AWS.S3; using Amazon.CDK.AWS.IAM; // Configure the resource with Mixins var bucket = new CfnBucket(this, "MyBucket"); bucket.With(new BucketVersioning()); bucket.With(new BucketBlockPublicAccess()); // Grant permissions using a Facade var role = new Role(this, "MyRole", new RoleProps { AssumedBy = new ServicePrincipal("lambda.amazonaws.com") }); BucketGrants.FromBucket(bucket).Read(role);
Go
bucket := awss3.NewCfnBucket(stack, jsii.String("MyBucket"), nil) bucket.With(awss3.NewBucketVersioning()) bucket.With(awss3.NewBucketBlockPublicAccess()) role := awsiam.NewRole(stack, jsii.String("MyRole"), &awsiam.RoleProps{ AssumedBy: awsiam.NewServicePrincipal(jsii.String("lambda.amazonaws.com"), nil), }) awss3.BucketGrants_FromBucket(bucket).Read(role, nil)
  • Mixins - L1 및 L2 구문에 재사용 가능한 기능을 추가합니다.

  • 권한 부여 - 리소스 간에 권한을 부여합니다.

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