ファサード - AWS クラウド開発キット (AWS CDK) v2

これは AWS CDK v2 デベロッパーガイドです。旧版の CDK v1 は 2022 年 6 月 1 日にメンテナンスを開始し、2023 年 6 月 1 日にサポートを終了しました。

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

ファサード

ファサードは、リソースをアプリケーションの他の部分に接続するクラスです。各ファサードは 1 つのリソースタイプを対象としています。例えば、クラス は Amazon S3 バケットへのアクセスを許可するBucketGrantsため、 という名前が付けられます。ファサードは、L1 (CloudFormation レベル) コンストラクトと L2 (インテントベース) コンストラクトの両方で機能します。

一部のファサードが生成され、メトリクスやリフレクションクラスなど、ほとんどのリソースで使用できるようになります。その他の は、 Grants クラスなど、カスタムロジックを必要とするリソースに対して手動で作成されます。

クラスを付与します

最も広く使用されているファサードは Grants クラスです。これにより、シンプルな方法を使用して AWS リソースへのアクセスを許可できます。例えば、Amazon S3 バケットBucketGrantsには を、Amazon SNS トピックTopicGrantsには を使用できます。 Amazon SNS

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 と組み合わせることで、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 コンストラクトに再利用可能な機能を追加します。

  • Grants – リソース間のアクセス許可を付与します。

  • コンストラクト – L1, L2、L3 コンストラクトについて説明します。