Cookie の設定を選択する

当社は、当社のサイトおよびサービスを提供するために必要な必須 Cookie および類似のツールを使用しています。当社は、パフォーマンス Cookie を使用して匿名の統計情報を収集することで、お客様が当社のサイトをどのように利用しているかを把握し、改善に役立てています。必須 Cookie は無効化できませんが、[カスタマイズ] または [拒否] をクリックしてパフォーマンス Cookie を拒否することはできます。

お客様が同意した場合、AWS および承認された第三者は、Cookie を使用して便利なサイト機能を提供したり、お客様の選択を記憶したり、関連する広告を含む関連コンテンツを表示したりします。すべての必須ではない Cookie を受け入れるか拒否するには、[受け入れる] または [拒否] をクリックしてください。より詳細な選択を行うには、[カスタマイズ] をクリックしてください。

AWS CDK コンストラクト

フォーカスモード
AWS CDK コンストラクト - AWS Cloud Development Kit (AWS CDK) v2

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

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

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

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

コンストラクトは、 AWS Cloud Development Kit (AWS CDK) アプリケーションの基本的な構成要素です。コンストラクトは、1 つ以上の AWS CloudFormation リソースとその設定を表すアプリケーション内のコンポーネントです。コンストラクトをインポートして設定することで、アプリケーションを 1 つずつ構築していきます。

コンストラクトをインポートして使用する

コンストラクトは、AWS コンストラクトライブラリ から CDK アプリケーションにインポートするクラスです。また、独自のコンストラクトを作成および配布することや、サードパーティーの開発者によって作成されたコンストラクトを使用することもできます。

コンストラクトは、コンストラクトプログラミングモデル (CPM) の一部です。CDK for Terraform (CDKtf)、CDK for Kubernetes (CDK8s)、Projen など、他のツールでも使用できます。

また、多数のサードパーティーが AWS CDKと互換性のあるコンストラクトを公開しています。 AWS CDK コンストラクトパートナーのエコシステムについては、Construct Hub をご覧ください。

コンストラクトレベル

コンストラクトライブラリからの AWS コンストラクトは、3 つのレベルに分類されます。各レベルは、抽象化のレベルが段階的に上がっていきます。抽象化が高いほど、設定が容易になり、必要な専門知識も少なくなります。抽象化が低いほど、より高度なカスタマイズが可能になりますが、より多くの専門知識が必要になります。

レベル 1 (L1) コンストラクト

L1 コンストラクトは、CFN リソース とも呼ばれ、最下位のコンストラクトであり、抽象化は行いません。各 L1 コンストラクトは、単一の AWS CloudFormation リソースに直接マッピングされます。L1 コンストラクトでは、特定の AWS CloudFormation リソースを表すコンストラクトをインポートします。次に、コンストラクトインスタンス内でリソースのプロパティを定義します。

L1 コンストラクトは、 AWS リソースプロパティの定義に慣れ AWS CloudFormation ており、完全に制御する必要がある場合に最適です。

AWS コンストラクトライブラリでは、L1 コンストラクトの名前は で始まりCfn、その後にそれが表す AWS CloudFormation リソースの識別子が続きます。例えば、 CfnBucketコンストラクトは AWS::S3::Bucket AWS CloudFormation リソースを表す L1 コンストラクトです。

L1 コンストラクトは、AWS CloudFormation リソース仕様から生成されます。リソースが に存在する場合は AWS CloudFormation、L1 コンストラクト AWS CDK として で使用できます。新しいリソースまたはプロパティが AWS コンストラクトライブラリで使用可能になるまでに最大 1 週間かかる場合があります。詳細については、「AWS CloudFormation ユーザーガイド」の「AWS リソースおよびプロパティタイプのリファレンス」を参照してください。

レベル 2 (L2) コンストラクト

キュレーションされたコンストラクトとも呼ばれる L2 コンストラクトは、CDK チームによって慎重に開発され、通常は最も広く使用されているコンストラクトタイプです。L2 コンストラクトは、L1 コンストラクトと同様に、単一の AWS CloudFormation リソースに直接マッピングされます。L1 コンストラクトと比較して、L2 コンストラクトは直感的なインテントベースの API を通してより高度な抽象化を提供します。L2 コンストラクトには、適切なデフォルトプロパティ設定、ベストプラクティスのセキュリティポリシー、および多くのボイラープレートコードやグルーロジックを生成する機能が含まれています。

L2 コンストラクトは、ほとんどのリソースにヘルパーメソッドも提供します。これにより、プロパティ、アクセス許可、リソース間のイベントベースのインタラクションなどをより簡単かつ迅速に定義できます。

s3.Bucket クラスは、Amazon Simple Storage Service (Amazon S3) バケットリソースの L2 コンストラクトの一例です。

AWS コンストラクトライブラリには、安定しており、本番環境で使用できるように指定された L2 コンストラクトが含まれています。開発中の L2 コンストラクトは実験的なものとして指定され、別個のモジュールとして提供されます。

レベル 3 (L3) コンストラクト

L3 コンストラクトは、パターンとも呼ばれ、最も高度な最高レベルです。各 L3 コンストラクトには、アプリケーション内の特定のタスクまたはサービスを達成するために連携するように設定された、複数リソースの集合を含めることができます。L3 コンストラクトは、アプリケーションの特定のユースケースの AWS アーキテクチャ全体を作成するために使用されます。

完全なシステム設計、または大規模なシステムの主要部分を提供するために、L3 コンストラクトは、独自の考えに基づくデフォルトのプロパティ設定を提供します。これらは、問題を解決し、解決策を提供するための特定のアプローチを軸に構築されています。L3 コンストラクトを使用すると、入力とコードの量を最小限に抑えながら、複数のリソースをすばやく作成して設定できます。

ecsPatterns.ApplicationLoadBalancedFargateService クラスは、Amazon Elastic Container Service (Amazon ECS) クラスターで実行され、Application Load Balancer によってフロントされる AWS Fargate サービスを表す L3 コンストラクトの例です。

L2 コンストラクトと同様に、本番環境で使用できる L3 AWS コンストラクトは コンストラクトライブラリに含まれています。開発中のものは、別個のモジュールで提供されます。

コンストラクトの定義

コンポジション

コンポジションは、コンストラクトを通して高レベルの抽象化を定義するための重要なパターンです。高レベルのコンストラクトは、任意の数の低レベルのコンストラクトから構成できます。ボトムアップの観点からは、 コンストラクトを使用して、デプロイする個々の AWS リソースを整理します。必要な階層数で、目的に応じて便利な抽象化を使用します。

コンポジションを使用することで、再利用可能なコンポーネントを定義し、他のコードと同様に共有することができます。たとえば、チームはバックアップ、グローバルレプリケーション、自動スケーリング、モニタリングなど、Amazon DynamoDB テーブルに対する会社のベストプラクティスを実装するコンストラクトを定義できます。チームは、コンストラクトを内部的に他のチームと共有することも、公開することもできます。

チームは、他のライブラリパッケージと同様にコンストラクトを使用できます。ライブラリが更新されると、開発者は他のコードライブラリと同様に、新しいバージョンの改善やバグ修正にアクセスできるようになります。

初期化

コンストラクトは、Construct 基底クラスを拡張したクラスとして実装されます。コンストラクトの定義は、クラスのインスタンス化によって行います。すべてのコンストラクトは、初期化時に以下の 3 つのパラメータを取ります。

  • scope – コンストラクトの親または所有者。これはスタックでも別のコンストラクトでもかまいません。スコープは、コンストラクトツリー内のコンストラクトの場所を決定します。通常は、現在のオブジェクトを表す this (Python ではself) をスコープとして渡すべきです。

  • id — スコープ内で一意でなければならない識別子です。識別子は、 コンストラクト内で定義されているすべての名前空間として機能します。リソース名や論理 IDs AWS CloudFormation などの一意の識別子を生成するために使用されます。

    識別子が一意である必要があるのはスコープ内のみです。これにより、含まれる可能性のあるコンストラクトと識別子を気にせずにコンストラクトをインスタンス化して再利用でき、コンストラクトを組み合わせてより高いレベルの抽象化を作成できるようになります。さらに、スコープを使用すると、コンストラクトのグループを一度に参照できます。例としては、タグ付けや、コンストラクトのデプロイ先の指定などが挙げられます。

  • props – 言語に応じて、コンストラクトの初期設定を定義するプロパティまたはキーワード引数のセット。高レベルのコンストラクトであるほど多くのデフォルト値が提供され、すべての prop 要素がオプションである場合は、props パラメータを完全に省略することができます。

設定

ほとんどのコンストラクトは、3 番目の引数 (Python ではキーワード引数) として props を受け入れます。これは、コンストラクトの設定を定義する名前と値のコレクションです。次の例では、 AWS Key Management Service (AWS KMS) 暗号化と静的ウェブサイトホスティングを有効にしたバケットを定義します。暗号化キーの明示的な指定はされていないため、Bucket コンストラクトは新しい kms.Key を定義してバケットに関連付けます。

TypeScript
new s3.Bucket(this, 'MyEncryptedBucket', { encryption: s3.BucketEncryption.KMS, websiteIndexDocument: 'index.html' });
JavaScript
new s3.Bucket(this, 'MyEncryptedBucket', { encryption: s3.BucketEncryption.KMS, websiteIndexDocument: 'index.html' });
Python
s3.Bucket(self, "MyEncryptedBucket", encryption=s3.BucketEncryption.KMS, website_index_document="index.html")
Java
Bucket.Builder.create(this, "MyEncryptedBucket") .encryption(BucketEncryption.KMS_MANAGED) .websiteIndexDocument("index.html").build();
C#
new Bucket(this, "MyEncryptedBucket", new BucketProps { Encryption = BucketEncryption.KMS_MANAGED, WebsiteIndexDocument = "index.html" });
Go
awss3.NewBucket(stack, jsii.String("MyEncryptedBucket"), &awss3.BucketProps{ Encryption: awss3.BucketEncryption_KMS, WebsiteIndexDocument: jsii.String("index.html"), })
new s3.Bucket(this, 'MyEncryptedBucket', { encryption: s3.BucketEncryption.KMS, websiteIndexDocument: 'index.html' });

コンストラクトの操作

コンストラクトは、基底クラスである Construct クラスを拡張したクラスです。コンストラクトをインスタンス化すると、コンストラクトオブジェクトは一連のメソッドとプロパティを公開します。これにより、コンストラクトを操作し、システムの他の部分への参照として渡すことができます。

AWS CDK フレームワークでは、コンストラクトの APIsに制限はありません。作成者は必要な API を定義できます。ただし、 などの AWS コンストラクトライブラリに含まれているコンストラクト AWS はs3.Bucket、ガイドラインと一般的なパターンに従います。これにより、すべての AWS リソースで一貫したエクスペリエンスが提供されます。

ほとんどの AWS コンストラクトには、そのコンストラクトに対する AWS Identity and Access Management (IAM) アクセス許可をプリンシパルに付与するために使用できる一連の付与メソッドがあります。以下の例では、IAM グループ data-science に対し、Amazon S3 バケット raw-data から読み取りを行うためのアクセス許可を付与します。

TypeScript
const rawData = new s3.Bucket(this, 'raw-data'); const dataScience = new iam.Group(this, 'data-science'); rawData.grantRead(dataScience);
JavaScript
const rawData = new s3.Bucket(this, 'raw-data'); const dataScience = new iam.Group(this, 'data-science'); rawData.grantRead(dataScience);
Python
raw_data = s3.Bucket(self, 'raw-data') data_science = iam.Group(self, 'data-science') raw_data.grant_read(data_science)
Java
Bucket rawData = new Bucket(this, "raw-data"); Group dataScience = new Group(this, "data-science"); rawData.grantRead(dataScience);
C#
var rawData = new Bucket(this, "raw-data"); var dataScience = new Group(this, "data-science"); rawData.GrantRead(dataScience);
Go
rawData := awss3.NewBucket(stack, jsii.String("raw-data"), nil) dataScience := awsiam.NewGroup(stack, jsii.String("data-science"), nil) rawData.GrantRead(dataScience, nil)
const rawData = new s3.Bucket(this, 'raw-data'); const dataScience = new iam.Group(this, 'data-science'); rawData.grantRead(dataScience);

もう 1 つの一般的なパターンは、 AWS コンストラクトが他の場所で提供されたデータからリソースの属性の 1 つを設定することです。属性には、Amazon リソースネーム (ARN)、名前、または URL などが含まれます。

次のコードは、 AWS Lambda 関数を定義し、環境変数のキューの URL を介して Amazon Simple Queue Service (Amazon SQS) キューに関連付けます。

TypeScript
const jobsQueue = new sqs.Queue(this, 'jobs'); const createJobLambda = new lambda.Function(this, 'create-job', { runtime: lambda.Runtime.NODEJS_18_X, handler: 'index.handler', code: lambda.Code.fromAsset('./create-job-lambda-code'), environment: { QUEUE_URL: jobsQueue.queueUrl } });
JavaScript
const jobsQueue = new sqs.Queue(this, 'jobs'); const createJobLambda = new lambda.Function(this, 'create-job', { runtime: lambda.Runtime.NODEJS_18_X, handler: 'index.handler', code: lambda.Code.fromAsset('./create-job-lambda-code'), environment: { QUEUE_URL: jobsQueue.queueUrl } });
Python
jobs_queue = sqs.Queue(self, "jobs") create_job_lambda = lambda_.Function(self, "create-job", runtime=lambda_.Runtime.NODEJS_18_X, handler="index.handler", code=lambda_.Code.from_asset("./create-job-lambda-code"), environment=dict( QUEUE_URL=jobs_queue.queue_url ) )
Java
final Queue jobsQueue = new Queue(this, "jobs"); Function createJobLambda = Function.Builder.create(this, "create-job") .handler("index.handler") .code(Code.fromAsset("./create-job-lambda-code")) .environment(java.util.Map.of( // Map.of is Java 9 or later "QUEUE_URL", jobsQueue.getQueueUrl()) .build();
C#
var jobsQueue = new Queue(this, "jobs"); var createJobLambda = new Function(this, "create-job", new FunctionProps { Runtime = Runtime.NODEJS_18_X, Handler = "index.handler", Code = Code.FromAsset(@".\create-job-lambda-code"), Environment = new Dictionary<string, string> { ["QUEUE_URL"] = jobsQueue.QueueUrl } });
Go
createJobLambda := awslambda.NewFunction(stack, jsii.String("create-job"), &awslambda.FunctionProps{ Runtime: awslambda.Runtime_NODEJS_18_X(), Handler: jsii.String("index.handler"), Code: awslambda.Code_FromAsset(jsii.String(".\\create-job-lambda-code"), nil), Environment: &map[string]*string{ "QUEUE_URL": jsii.String(*jobsQueue.QueueUrl()), }, })
const jobsQueue = new sqs.Queue(this, 'jobs'); const createJobLambda = new lambda.Function(this, 'create-job', { runtime: lambda.Runtime.NODEJS_18_X, handler: 'index.handler', code: lambda.Code.fromAsset('./create-job-lambda-code'), environment: { QUEUE_URL: jobsQueue.queueUrl } });

コンストラクトライブラリの最も一般的な API AWS パターンについては、「」を参照してくださいリソースと AWS CDK

アプリケーションとスタックのコンストラクト

AWS コンストラクトライブラリの Appおよび Stack クラスは一意のコンストラクトです。他のコンストラクトと比較して、リソース AWS は独自に設定されません。代わりに、他のコンストラクトにコンテキストを提供するために使用されます。 AWS リソースを表すすべてのコンストラクトは、直接または間接的に、Stack コンストラクトのスコープ内で定義する必要があります。Stack コンストラクトは、App コンストラクトのスコープ内で定義されます。

CDK アプリケーションの詳細については、「AWS CDK アプリ」を参照してください。CDK スタックの詳細については、「AWS CDK スタックの概要」を参照してください。

以下の例では、単一のスタックを持つアプリケーションを定義します。スタック内では、L2 コンストラクトを使用して Amazon S3 バケットリソースを設定します。

TypeScript
import { App, Stack, StackProps } from 'aws-cdk-lib'; import * as s3 from 'aws-cdk-lib/aws-s3'; class HelloCdkStack extends Stack { constructor(scope: App, id: string, props?: StackProps) { super(scope, id, props); new s3.Bucket(this, 'MyFirstBucket', { versioned: true }); } } const app = new App(); new HelloCdkStack(app, "HelloCdkStack");
JavaScript
const { App , Stack } = require('aws-cdk-lib'); const s3 = require('aws-cdk-lib/aws-s3'); class HelloCdkStack extends Stack { constructor(scope, id, props) { super(scope, id, props); new s3.Bucket(this, 'MyFirstBucket', { versioned: true }); } } const app = new App(); new HelloCdkStack(app, "HelloCdkStack");
Python
from aws_cdk import App, Stack import aws_cdk.aws_s3 as s3 from constructs import Construct class HelloCdkStack(Stack): def __init__(self, scope: Construct, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) s3.Bucket(self, "MyFirstBucket", versioned=True) app = App() HelloCdkStack(app, "HelloCdkStack")
Java

HelloCdkStack.java ファイルで定義されるスタック:

import software.constructs.Construct; import software.amazon.awscdk.Stack; import software.amazon.awscdk.StackProps; import software.amazon.awscdk.services.s3.*; public class HelloCdkStack extends Stack { public HelloCdkStack(final Construct scope, final String id) { this(scope, id, null); } public HelloCdkStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); Bucket.Builder.create(this, "MyFirstBucket") .versioned(true).build(); } }

HelloCdkApp.java ファイルで定義されるアプリケーション:

import software.amazon.awscdk.App; import software.amazon.awscdk.StackProps; public class HelloCdkApp { public static void main(final String[] args) { App app = new App(); new HelloCdkStack(app, "HelloCdkStack", StackProps.builder() .build()); app.synth(); } }
C#
using Amazon.CDK; using Amazon.CDK.AWS.S3; namespace HelloCdkApp { internal static class Program { public static void Main(string[] args) { var app = new App(); new HelloCdkStack(app, "HelloCdkStack"); app.Synth(); } } public class HelloCdkStack : Stack { public HelloCdkStack(Construct scope, string id, IStackProps props=null) : base(scope, id, props) { new Bucket(this, "MyFirstBucket", new BucketProps { Versioned = true }); } } }
Go
func NewHelloCdkStack(scope constructs.Construct, id string, props *HelloCdkStackProps) awscdk.Stack { var sprops awscdk.StackProps if props != nil { sprops = props.StackProps } stack := awscdk.NewStack(scope, &id, &sprops) awss3.NewBucket(stack, jsii.String("MyFirstBucket"), &awss3.BucketProps{ Versioned: jsii.Bool(true), }) return stack }
import { App, Stack, StackProps } from 'aws-cdk-lib'; import * as s3 from 'aws-cdk-lib/aws-s3'; class HelloCdkStack extends Stack { constructor(scope: App, id: string, props?: StackProps) { super(scope, id, props); new s3.Bucket(this, 'MyFirstBucket', { versioned: true }); } } const app = new App(); new HelloCdkStack(app, "HelloCdkStack");

コンストラクトの使用

L1 コンストラクトの使用

L1 コンストラクトは個々の AWS CloudFormation リソースに直接マッピングされます。設定に必要なリソースは自分で用意しなければなりません。

この例では、L1 CfnBucket コンストラクトを使用して bucket オブジェクトを作成します。

TypeScript
const bucket = new s3.CfnBucket(this, "amzn-s3-demo-bucket", { bucketName: "amzn-s3-demo-bucket" });
JavaScript
const bucket = new s3.CfnBucket(this, "amzn-s3-demo-bucket", { bucketName: "amzn-s3-demo-bucket" });
Python
bucket = s3.CfnBucket(self, "amzn-s3-demo-bucket", bucket_name="amzn-s3-demo-bucket")
Java
CfnBucket bucket = new CfnBucket.Builder().bucketName("amzn-s3-demo-bucket").build();
C#
var bucket = new CfnBucket(this, "amzn-s3-demo-bucket", new CfnBucketProps { BucketName= "amzn-s3-demo-bucket" });
Go
awss3.NewCfnBucket(stack, jsii.String("amzn-s3-demo-bucket"), &awss3.CfnBucketProps{ BucketName: jsii.String("amzn-s3-demo-bucket"), })
const bucket = new s3.CfnBucket(this, "amzn-s3-demo-bucket", { bucketName: "amzn-s3-demo-bucket" });

単純なブール語、文字列、数値、コンテナではないコンストラクトのプロパティは、サポートされている言語ごとに異なる方法で処理されます。

TypeScript
const bucket = new s3.CfnBucket(this, "amzn-s3-demo-bucket", { bucketName: "amzn-s3-demo-bucket", corsConfiguration: { corsRules: [{ allowedOrigins: ["*"], allowedMethods: ["GET"] }] } });
JavaScript
const bucket = new s3.CfnBucket(this, "amzn-s3-demo-bucket", { bucketName: "amzn-s3-demo-bucket", corsConfiguration: { corsRules: [{ allowedOrigins: ["*"], allowedMethods: ["GET"] }] } });
Python

Python では、これらのプロパティは L1 コンストラクトの内部クラスとして定義された型として表現されます。たとえば、CfnBucket のオプションプロパティ cors_configuration には、CfnBucket.CorsConfigurationProperty 型のラッパーが必要です。ここでは、CfnBucket インスタンスで cors_configuration を定義しています。

bucket = CfnBucket(self, "amzn-s3-demo-bucket", bucket_name="amzn-s3-demo-bucket", cors_configuration=CfnBucket.CorsConfigurationProperty( cors_rules=[CfnBucket.CorsRuleProperty( allowed_origins=["*"], allowed_methods=["GET"] )] ) )
Java

Java では、これらのプロパティは L1 コンストラクトの内部クラスとして定義された型として表現されます。たとえば、CfnBucket のオプションプロパティ corsConfiguration には、CfnBucket.CorsConfigurationProperty 型のラッパーが必要です。ここでは、CfnBucket インスタンスで corsConfiguration を定義しています。

CfnBucket bucket = CfnBucket.Builder.create(this, "amzn-s3-demo-bucket") .bucketName("amzn-s3-demo-bucket") .corsConfiguration(new CfnBucket.CorsConfigurationProperty.Builder() .corsRules(Arrays.asList(new CfnBucket.CorsRuleProperty.Builder() .allowedOrigins(Arrays.asList("*")) .allowedMethods(Arrays.asList("GET")) .build())) .build()) .build();
C#

C# では、これらのプロパティは L1 コンストラクトの内部クラスとして定義された型として表現されます。たとえば、CfnBucket のオプションプロパティ CorsConfiguration には、CfnBucket.CorsConfigurationProperty 型のラッパーが必要です。ここでは、CfnBucket インスタンスで CorsConfiguration を定義しています。

var bucket = new CfnBucket(this, "amzn-s3-demo-bucket", new CfnBucketProps { BucketName = "amzn-s3-demo-bucket", CorsConfiguration = new CfnBucket.CorsConfigurationProperty { CorsRules = new object[] { new CfnBucket.CorsRuleProperty { AllowedOrigins = new string[] { "*" }, AllowedMethods = new string[] { "GET" }, } } } });
Go

Go では、これらの型は L1 コンストラクトの名前、アンダースコア、プロパティ名を使用して命名されます。たとえば、CfnBucket のオプションプロパティ CorsConfiguration には、CfnBucket_CorsConfigurationProperty 型のラッパーが必要です。ここでは、CfnBucket インスタンスで CorsConfiguration を定義しています。

awss3.NewCfnBucket(stack, jsii.String("amzn-s3-demo-bucket"), &awss3.CfnBucketProps{ BucketName: jsii.String("amzn-s3-demo-bucket"), CorsConfiguration: &awss3.CfnBucket_CorsConfigurationProperty{ CorsRules: []awss3.CorsRule{ awss3.CorsRule{ AllowedOrigins: jsii.Strings("*"), AllowedMethods: &[]awss3.HttpMethods{"GET"}, }, }, }, })
const bucket = new s3.CfnBucket(this, "amzn-s3-demo-bucket", { bucketName: "amzn-s3-demo-bucket", corsConfiguration: { corsRules: [{ allowedOrigins: ["*"], allowedMethods: ["GET"] }] } });
重要

L1 コンストラクトで L2 プロパティタイプを使用することはできません。その逆も同様です。L1 コンストラクトを使用する場合は、使用している L1 コンストラクトに定義されているタイプを必ず使用してください。他の L1 コンストラクトの型を使用しないでください (名前が同じものもありますが、同じ型ではありません)。

言語固有の API リファレンスの中には、現在 L1 プロパティタイプへのパスにエラーがあるか、これらのクラスがまったく記載されていないものがあります。これは近いうちに修正したいと考えています。それまでの間、このような型は常に、それらが使用される L1 コンストラクトの内部クラスであることに注意してください。

L2 コンストラクトの使用

以下の例では、Bucket L2 コンストラクトからオブジェクトを作成して、Amazon S3 バケットを定義します。

TypeScript
import * as s3 from 'aws-cdk-lib/aws-s3'; // "this" is HelloCdkStack new s3.Bucket(this, 'MyFirstBucket', { versioned: true });
JavaScript
const s3 = require('aws-cdk-lib/aws-s3'); // "this" is HelloCdkStack new s3.Bucket(this, 'MyFirstBucket', { versioned: true });
Python
import aws_cdk.aws_s3 as s3 # "self" is HelloCdkStack s3.Bucket(self, "MyFirstBucket", versioned=True)
Java
import software.amazon.awscdk.services.s3.*; public class HelloCdkStack extends Stack { public HelloCdkStack(final Construct scope, final String id) { this(scope, id, null); } public HelloCdkStack(final Construct scope, final String id, final StackProps props) { super(scope, id, props); Bucket.Builder.create(this, "MyFirstBucket") .versioned(true).build(); } }
C#
using Amazon.CDK.AWS.S3; // "this" is HelloCdkStack new Bucket(this, "MyFirstBucket", new BucketProps { Versioned = true });
Go
import ( "github.com/aws/aws-cdk-go/awscdk/v2/awss3" "github.com/aws/jsii-runtime-go" ) // stack is HelloCdkStack awss3.NewBucket(stack, jsii.String("MyFirstBucket"), &awss3.BucketProps{ Versioned: jsii.Bool(true), })>
import * as s3 from 'aws-cdk-lib/aws-s3'; // "this" is HelloCdkStack new s3.Bucket(this, 'MyFirstBucket', { versioned: true });

MyFirstBucket は、 が AWS CloudFormation 作成するバケットの名前ではありません。これは、CDK アプリケーションのコンテキスト内で新しいコンストラクトに与えられる論理識別子です。physicalName 値は、 AWS CloudFormation リソースに名前を付けるために使用されます。

サードパーティーのコンストラクトの使用

Construct Hub は、、サードパーティー AWS、オープンソース CDK コミュニティから追加のコンストラクトを発見するのに役立つリソースです。

独自のコンストラクトの作成

既存のコンストラクトを使用するだけでなく、独自のコンストラクトを記述し、誰でもアプリで使用できるようにすることもできます。 AWS CDKにおいて、すべてのコンストラクトは対等です。コンストラクトライブラリからの AWS コンストラクトは、NPM、、Mavenまたは を介して公開されたサードパーティーライブラリからのコンストラクトと同じように扱われますPyPI。また、社内のパッケージリポジトリに公開されたコンストラクトも同じように扱われます。

新しいコンストラクトを宣言するには、Construct 基底クラスを拡張するクラスを constructs パッケージで作成し、初期化引数のパターンに従います。

以下の例は、Amazon S3 バケットを表すコンストラクトを宣言する方法を示しています。S3 バケットは、誰かがファイルをアップロードするたびに Amazon Simple Notification Service (Amazon SNS) 通知を送信します。

TypeScript
export interface NotifyingBucketProps { prefix?: string; } export class NotifyingBucket extends Construct { constructor(scope: Construct, id: string, props: NotifyingBucketProps = {}) { super(scope, id); const bucket = new s3.Bucket(this, 'bucket'); const topic = new sns.Topic(this, 'topic'); bucket.addObjectCreatedNotification(new s3notify.SnsDestination(topic), { prefix: props.prefix }); } }
JavaScript
class NotifyingBucket extends Construct { constructor(scope, id, props = {}) { super(scope, id); const bucket = new s3.Bucket(this, 'bucket'); const topic = new sns.Topic(this, 'topic'); bucket.addObjectCreatedNotification(new s3notify.SnsDestination(topic), { prefix: props.prefix }); } } module.exports = { NotifyingBucket }
Python
class NotifyingBucket(Construct): def __init__(self, scope: Construct, id: str, *, prefix=None): super().__init__(scope, id) bucket = s3.Bucket(self, "bucket") topic = sns.Topic(self, "topic") bucket.add_object_created_notification(s3notify.SnsDestination(topic), s3.NotificationKeyFilter(prefix=prefix))
Java
public class NotifyingBucket extends Construct { public NotifyingBucket(final Construct scope, final String id) { this(scope, id, null, null); } public NotifyingBucket(final Construct scope, final String id, final BucketProps props) { this(scope, id, props, null); } public NotifyingBucket(final Construct scope, final String id, final String prefix) { this(scope, id, null, prefix); } public NotifyingBucket(final Construct scope, final String id, final BucketProps props, final String prefix) { super(scope, id); Bucket bucket = new Bucket(this, "bucket"); Topic topic = new Topic(this, "topic"); if (prefix != null) bucket.addObjectCreatedNotification(new SnsDestination(topic), NotificationKeyFilter.builder().prefix(prefix).build()); } }
C#
public class NotifyingBucketProps : BucketProps { public string Prefix { get; set; } } public class NotifyingBucket : Construct { public NotifyingBucket(Construct scope, string id, NotifyingBucketProps props = null) : base(scope, id) { var bucket = new Bucket(this, "bucket"); var topic = new Topic(this, "topic"); bucket.AddObjectCreatedNotification(new SnsDestination(topic), new NotificationKeyFilter { Prefix = props?.Prefix }); } }
Go
type NotifyingBucketProps struct { awss3.BucketProps Prefix *string } func NewNotifyingBucket(scope constructs.Construct, id *string, props *NotifyingBucketProps) awss3.Bucket { var bucket awss3.Bucket if props == nil { bucket = awss3.NewBucket(scope, jsii.String(*id+"Bucket"), nil) } else { bucket = awss3.NewBucket(scope, jsii.String(*id+"Bucket"), &props.BucketProps) } topic := awssns.NewTopic(scope, jsii.String(*id+"Topic"), nil) if props == nil { bucket.AddObjectCreatedNotification(awss3notifications.NewSnsDestination(topic)) } else { bucket.AddObjectCreatedNotification(awss3notifications.NewSnsDestination(topic), &awss3.NotificationKeyFilter{ Prefix: props.Prefix, }) } return bucket }
export interface NotifyingBucketProps { prefix?: string; } export class NotifyingBucket extends Construct { constructor(scope: Construct, id: string, props: NotifyingBucketProps = {}) { super(scope, id); const bucket = new s3.Bucket(this, 'bucket'); const topic = new sns.Topic(this, 'topic'); bucket.addObjectCreatedNotification(new s3notify.SnsDestination(topic), { prefix: props.prefix }); } }
注記

NotifyingBucket コンストラクトは、Bucket ではなく、Construct を継承しています。これは、Amazon S3 バケットと Amazon SNS トピックをバンドルするために、継承ではなく構成を使用しているためです。一般的に、 AWS CDK コンストラクトを開発するときは、継承よりもコンポジションが優先されます。

NotifyingBucket コンストラクターは、scopeidprops という典型的なコンストラクトの署名を持っています。最後に挙げた引数 props は、props はすべてオプションであるため、オプション (デフォルト値 {}) になります。(規定クラス Constructprops 引数を取りません)。このコンストラクトのインスタンスは、props なしでアプリに定義できます。たとえば以下のようになります。

TypeScript
new NotifyingBucket(this, 'MyNotifyingBucket');
JavaScript
new NotifyingBucket(this, 'MyNotifyingBucket');
Python
NotifyingBucket(self, "MyNotifyingBucket")
Java
new NotifyingBucket(this, "MyNotifyingBucket");
C#
new NotifyingBucket(this, "MyNotifyingBucket");
Go
NewNotifyingBucket(stack, jsii.String("MyNotifyingBucket"), nil)
new NotifyingBucket(this, 'MyNotifyingBucket');

または、props (Java では追加パラメータ) を使用して、フィルタリングするパスプレフィックスを指定できます。たとえば以下のようになります。

TypeScript
new NotifyingBucket(this, 'MyNotifyingBucket', { prefix: 'images/' });
JavaScript
new NotifyingBucket(this, 'MyNotifyingBucket', { prefix: 'images/' });
Python
NotifyingBucket(self, "MyNotifyingBucket", prefix="images/")
Java
new NotifyingBucket(this, "MyNotifyingBucket", "/images");
C#
new NotifyingBucket(this, "MyNotifyingBucket", new NotifyingBucketProps { Prefix = "/images" });
Go
NewNotifyingBucket(stack, jsii.String("MyNotifyingBucket"), &NotifyingBucketProps{ Prefix: jsii.String("images/"), })
new NotifyingBucket(this, 'MyNotifyingBucket', { prefix: 'images/' });

通常、コンストラクト上のいくつかのプロパティまたはメソッドも公開する必要があります。コンストラクトの背後にトピックが隠れてしまう形では、コンストラクトのユーザーがサブスクライブできなくなるため、あまり有益とは言えません。topic プロパティを追加すると、以下の例に示すように、利用者が内部トピックにアクセスできるようになります。

TypeScript
export class NotifyingBucket extends Construct { public readonly topic: sns.Topic; constructor(scope: Construct, id: string, props: NotifyingBucketProps) { super(scope, id); const bucket = new s3.Bucket(this, 'bucket'); this.topic = new sns.Topic(this, 'topic'); bucket.addObjectCreatedNotification(new s3notify.SnsDestination(this.topic), { prefix: props.prefix }); } }
JavaScript
class NotifyingBucket extends Construct { constructor(scope, id, props) { super(scope, id); const bucket = new s3.Bucket(this, 'bucket'); this.topic = new sns.Topic(this, 'topic'); bucket.addObjectCreatedNotification(new s3notify.SnsDestination(this.topic), { prefix: props.prefix }); } } module.exports = { NotifyingBucket };
Python
class NotifyingBucket(Construct): def __init__(self, scope: Construct, id: str, *, prefix=None, **kwargs): super().__init__(scope, id) bucket = s3.Bucket(self, "bucket") self.topic = sns.Topic(self, "topic") bucket.add_object_created_notification(s3notify.SnsDestination(self.topic), s3.NotificationKeyFilter(prefix=prefix))
Java
public class NotifyingBucket extends Construct { public Topic topic = null; public NotifyingBucket(final Construct scope, final String id) { this(scope, id, null, null); } public NotifyingBucket(final Construct scope, final String id, final BucketProps props) { this(scope, id, props, null); } public NotifyingBucket(final Construct scope, final String id, final String prefix) { this(scope, id, null, prefix); } public NotifyingBucket(final Construct scope, final String id, final BucketProps props, final String prefix) { super(scope, id); Bucket bucket = new Bucket(this, "bucket"); topic = new Topic(this, "topic"); if (prefix != null) bucket.addObjectCreatedNotification(new SnsDestination(topic), NotificationKeyFilter.builder().prefix(prefix).build()); } }
C#
public class NotifyingBucket : Construct { public readonly Topic topic; public NotifyingBucket(Construct scope, string id, NotifyingBucketProps props = null) : base(scope, id) { var bucket = new Bucket(this, "bucket"); topic = new Topic(this, "topic"); bucket.AddObjectCreatedNotification(new SnsDestination(topic), new NotificationKeyFilter { Prefix = props?.Prefix }); } }
Go

Go でこれを実行するには、少し追加の準備が必要です。元の NewNotifyingBucket 関数は、そのままでは awss3.Bucket を返します。そこで、NotifyingBucket 構造体を作成して、topic メンバーを含めるように Bucket を拡張する必要があります。そうすることで、この関数はこの型を返すようになります。

type NotifyingBucket struct { awss3.Bucket topic awssns.Topic } func NewNotifyingBucket(scope constructs.Construct, id *string, props *NotifyingBucketProps) NotifyingBucket { var bucket awss3.Bucket if props == nil { bucket = awss3.NewBucket(scope, jsii.String(*id+"Bucket"), nil) } else { bucket = awss3.NewBucket(scope, jsii.String(*id+"Bucket"), &props.BucketProps) } topic := awssns.NewTopic(scope, jsii.String(*id+"Topic"), nil) if props == nil { bucket.AddObjectCreatedNotification(awss3notifications.NewSnsDestination(topic)) } else { bucket.AddObjectCreatedNotification(awss3notifications.NewSnsDestination(topic), &awss3.NotificationKeyFilter{ Prefix: props.Prefix, }) } var nbucket NotifyingBucket nbucket.Bucket = bucket nbucket.topic = topic return nbucket }
export class NotifyingBucket extends Construct { public readonly topic: sns.Topic; constructor(scope: Construct, id: string, props: NotifyingBucketProps) { super(scope, id); const bucket = new s3.Bucket(this, 'bucket'); this.topic = new sns.Topic(this, 'topic'); bucket.addObjectCreatedNotification(new s3notify.SnsDestination(this.topic), { prefix: props.prefix }); } }

これで、利用者はトピックをサブスクライブできるようになりました。たとえば以下のようになります。

TypeScript
const queue = new sqs.Queue(this, 'NewImagesQueue'); const images = new NotifyingBucket(this, '/images'); images.topic.addSubscription(new sns_sub.SqsSubscription(queue));
JavaScript
const queue = new sqs.Queue(this, 'NewImagesQueue'); const images = new NotifyingBucket(this, '/images'); images.topic.addSubscription(new sns_sub.SqsSubscription(queue));
Python
queue = sqs.Queue(self, "NewImagesQueue") images = NotifyingBucket(self, prefix="Images") images.topic.add_subscription(sns_sub.SqsSubscription(queue))
Java
NotifyingBucket images = new NotifyingBucket(this, "MyNotifyingBucket", "/images"); images.topic.addSubscription(new SqsSubscription(queue));
C#
var queue = new Queue(this, "NewImagesQueue"); var images = new NotifyingBucket(this, "MyNotifyingBucket", new NotifyingBucketProps { Prefix = "/images" }); images.topic.AddSubscription(new SqsSubscription(queue));
Go
queue := awssqs.NewQueue(stack, jsii.String("NewImagesQueue"), nil) images := NewNotifyingBucket(stack, jsii.String("MyNotifyingBucket"), &NotifyingBucketProps{ Prefix: jsii.String("/images"), }) images.topic.AddSubscription(awssnssubscriptions.NewSqsSubscription(queue, nil))
const queue = new sqs.Queue(this, 'NewImagesQueue'); const images = new NotifyingBucket(this, '/images'); images.topic.addSubscription(new sns_sub.SqsSubscription(queue));

詳細

以下のビデオでは、CDK コンストラクトの包括的な概要と、CDK アプリケーションでの CDK コンストラクトの使用方法について説明します。

プライバシーサイト規約Cookie の設定
© 2025, Amazon Web Services, Inc. or its affiliates.All rights reserved.