

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

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

# AWS CDK とは
<a name="home"></a>

AWS Cloud Development Kit (AWS CDK) は、コードでクラウドインフラストラクチャを定義し、AWS CloudFormation を通じてプロビジョニングするための、オープンソースのソフトウェア開発フレームワークです。

AWS CDK は 2 つの主要な部分で構成されます。
+  **[AWS CDK コンストラクトライブラリ](constructs.md)** – コンストラクトと呼ばれる、事前記述されたモジュール式で再利用可能なコードのコレクション。インフラストラクチャを短時間で開発するために使用、変更、統合できます。AWS CDK コンストラクトライブラリの目的は、AWS でアプリケーションを構築するときに AWS サービスの定義および統合に必要な複雑さを低減することです。
+  AWS CDK Toolkit - 合成やデプロイの実行など、CDK アプリの管理と操作に使用できるツール。CDK Toolkit は、コマンドラインツール ([CDK CLI](ref-cli-cmd.md)) とプログラムによるライブラリ ([CDK Toolkit Library](toolkit-library.md)) で構成されています。

AWS CDK は TypeScript、JavaScript、Python、Java、C\$1/.Net、および Go をサポートしています。これらのサポートされている任意のプログラミング言語を使用し、[コンストラクト](constructs.md)と呼ばれる再利用可能なクラウドコンポーネントを定義できます。これらをまとめて[スタック](stacks.md)および[アプリ](apps.md)に構成します。次に、CDK アプリケーションを AWS CloudFormation を介してデプロイし、リソースをプロビジョニングまたは更新します。

![\[CDK アプリとプロセスの概要\]](http://docs.aws.amazon.com/ja_jp/cdk/v2/guide/images/AppStacks.png)


## AWS CDK の利点
<a name="home-benefits"></a>

AWS CDK を使用し、プログラミング言語の優れた表現力を活かした信頼性が高い、スケーラブル、コスト効率の高いアプリケーションをクラウドで開発できます。このアプローチには、次の内容を含む多くの利点があります。<a name="home-benefits-iac"></a>

 **Infrastructure as Code (IaC) の開発と管理**   
*コードとしてのインフラストラクチャ*を実践し、プログラム的、説明的、宣言的な方法でインフラストラクチャを作成、デプロイ、維持します。IaC を使用すると、デベロッパーがコードを扱う方法と同じようにインフラストラクチャを扱います。インフラストラクチャの管理にスケーラブルで構造化されたアプローチが得られます。IaC の詳細については、「*AWSホワイトペーパーの DevOps 入門*」の「[コードとしてのインフラストラクチャ](https://docs.aws.amazon.com/whitepapers/latest/introduction-devops-aws/infrastructure-as-code.html)」を参照してください。  
AWS CDK を使用すると、インフラストラクチャ、アプリケーションコード、設定をすべて 1 か所に配置し、すべてのマイルストーンで完全なクラウドデプロイ可能なシステムを確保できます。コードレビュー、ユニットテスト、ソースコントロールなどのソフトウェアエンジニアリングのベストプラクティスを採用し、インフラストラクチャをより堅牢にします。<a name="home-benefits-languages"></a>

 **汎用プログラミング言語を使用してクラウドインフラストラクチャの定義**   
AWS CDK を使用すると、次のいずれかのプログラミング言語を使用し、クラウドインフラストラクチャの TypeScript、JavaScript、Python、Java、C\$1/.Net、および Go を定義できます。希望する言語を選択し、パラメータ、条件、ループ、構成、継承などのプログラミング要素を使用して、インフラストラクチャの望ましい結果を定義します。  
同じプログラミング言語を使用し、インフラストラクチャおよびアプリケーションロジックを定義します。  
構文の強調表示やインテリジェントコード補完など、任意の IDE (統合開発環境) でインフラストラクチャを開発する利点が得られます。  

![\[VPC および Fargate サービス設定を使用した ECS クラスターの CDK 設定を示すコードスニペット。\]](http://docs.aws.amazon.com/ja_jp/cdk/v2/guide/images/CodeCompletion.png)
<a name="home-benefits-cfn"></a>

 **AWS CloudFormation を介してインフラストラクチャをデプロイする**   
 AWS CDK は AWS CloudFormation と統合して、インフラストラクチャを AWS にデプロイおよびプロビジョニングします。AWSCloudFormation は、AWS でサービスをプロビジョニングするためのリソースおよびプロパティ設定を広範囲にサポートするマネージド AWS サービスです。AWS CloudFormation を使用すると、エラー時にロールバックし、インフラストラクチャのデプロイを予測可能かつ繰り返し実行できます。AWS CloudFormation をすでに理解している場合、AWS CDK の使用を開始するときに新しい IaC 管理サービスについて知る必要はありません。<a name="home-benefits-constructs"></a>

 **コンストラクトを使用してアプリケーションの開発をすばやく開始**   
コンストラクトと呼ばれる再利用可能なコンポーネントを使用および共有することにより、開発を高速化できます。低レベルのコンストラクトを使用し、個々の AWS CloudFormation リソースおよびそのプロパティを定義します。高レベルのコンストラクトを使用してアプリケーションのより大きなコンポーネントをすばやく定義し、AWS リソースの賢明で安全なデフォルトを使用して、少ないコードで多くのインフラストラクチャを定義します。  
ユニークのユースケースに合わせてカスタマイズされた独自のコンストラクトを作成し、組織全体またはパブリックと共有します。

## AWS CDK の例
<a name="home-example"></a>

次の内容は、AWS CDK コンストラクトライブラリを使用して AWS Fargate 起動タイプを持つ Amazon Elastic Container Service (Amazon ECS) サービスを作成する例です。この例の詳細については、「[例: AWS CDK を使用して AWS Fargate サービスを作成する](ecs-example.md)」を参照してください。

**Example**  

```
export class MyEcsConstructStack extends Stack {
  constructor(scope: App, id: string, props?: StackProps) {
    super(scope, id, props);

    const vpc = new ec2.Vpc(this, "MyVpc", {
      maxAzs: 3 // Default is all AZs in region
    });

    const cluster = new ecs.Cluster(this, "MyCluster", {
      vpc: vpc
    });

    // Create a load-balanced Fargate service and make it public
    new ecs_patterns.ApplicationLoadBalancedFargateService(this, "MyFargateService", {
      cluster: cluster, // Required
      cpu: 512, // Default is 256
      desiredCount: 6, // Default is 1
      taskImageOptions: { image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample") },
      memoryLimitMiB: 2048, // Default is 512
      publicLoadBalancer: true // Default is false
    });
  }
}
```

```
class MyEcsConstructStack extends Stack {
  constructor(scope, id, props) {
    super(scope, id, props);

    const vpc = new ec2.Vpc(this, "MyVpc", {
      maxAzs: 3 // Default is all AZs in region
    });

    const cluster = new ecs.Cluster(this, "MyCluster", {
      vpc: vpc
    });

    // Create a load-balanced Fargate service and make it public
    new ecs_patterns.ApplicationLoadBalancedFargateService(this, "MyFargateService", {
      cluster: cluster, // Required
      cpu: 512, // Default is 256
      desiredCount: 6, // Default is 1
      taskImageOptions: { image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample") },
      memoryLimitMiB: 2048, // Default is 512
      publicLoadBalancer: true // Default is false
    });
  }
}

module.exports = { MyEcsConstructStack }
```

```
class MyEcsConstructStack(Stack):

  def __init__(self, scope: Construct, id: str, **kwargs) -> None:
    super().__init__(scope, id, **kwargs)

    vpc = ec2.Vpc(self, "MyVpc", max_azs=3)     # default is all AZs in region

    cluster = ecs.Cluster(self, "MyCluster", vpc=vpc)

    ecs_patterns.ApplicationLoadBalancedFargateService(self, "MyFargateService",
      cluster=cluster,            # Required
      cpu=512,                    # Default is 256
      desired_count=6,            # Default is 1
      task_image_options=ecs_patterns.ApplicationLoadBalancedTaskImageOptions(
          image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample")),
      memory_limit_mib=2048,      # Default is 512
      public_load_balancer=True)  # Default is False
```

```
public class MyEcsConstructStack extends Stack {

  public MyEcsConstructStack(final Construct scope, final String id) {
    this(scope, id, null);
  }

  public MyEcsConstructStack(final Construct scope, final String id,
      StackProps props) {
    super(scope, id, props);

    Vpc vpc = Vpc.Builder.create(this, "MyVpc").maxAzs(3).build();

    Cluster cluster = Cluster.Builder.create(this, "MyCluster")
            .vpc(vpc).build();

    ApplicationLoadBalancedFargateService.Builder.create(this, "MyFargateService")
            .cluster(cluster)
            .cpu(512)
            .desiredCount(6)
            .taskImageOptions(
                    ApplicationLoadBalancedTaskImageOptions.builder()
                            .image(ContainerImage
                                    .fromRegistry("amazon/amazon-ecs-sample"))
                            .build()).memoryLimitMiB(2048)
            .publicLoadBalancer(true).build();
  }
}
```

```
public class MyEcsConstructStack : Stack
{
    public MyEcsConstructStack(Construct scope, string id, IStackProps props=null) : base(scope, id, props)
    {
        var vpc = new Vpc(this, "MyVpc", new VpcProps
        {
            MaxAzs = 3
        });

        var cluster = new Cluster(this, "MyCluster", new ClusterProps
        {
            Vpc = vpc
        });

        new ApplicationLoadBalancedFargateService(this, "MyFargateService",
            new ApplicationLoadBalancedFargateServiceProps
        {
            Cluster = cluster,
            Cpu = 512,
            DesiredCount = 6,
            TaskImageOptions = new ApplicationLoadBalancedTaskImageOptions
            {
                Image = ContainerImage.FromRegistry("amazon/amazon-ecs-sample")
            },
            MemoryLimitMiB = 2048,
            PublicLoadBalancer = true,
        });
    }
}
```

```
func NewMyEcsConstructStack(scope constructs.Construct, id string, props *MyEcsConstructStackProps) awscdk.Stack {

	var sprops awscdk.StackProps

	if props != nil {
		sprops = props.StackProps
	}

	stack := awscdk.NewStack(scope, &id, &sprops)

	vpc := awsec2.NewVpc(stack, jsii.String("MyVpc"), &awsec2.VpcProps{
		MaxAzs: jsii.Number(3), // Default is all AZs in region
	})

	cluster := awsecs.NewCluster(stack, jsii.String("MyCluster"), &awsecs.ClusterProps{
		Vpc: vpc,
	})

	awsecspatterns.NewApplicationLoadBalancedFargateService(stack, jsii.String("MyFargateService"),
		&awsecspatterns.ApplicationLoadBalancedFargateServiceProps{
			Cluster:        cluster,           // required
			Cpu:            jsii.Number(512),  // default is 256
			DesiredCount:   jsii.Number(5),    // default is 1
			MemoryLimitMiB: jsii.Number(2048), // Default is 512
			TaskImageOptions: &awsecspatterns.ApplicationLoadBalancedTaskImageOptions{
				Image: awsecs.ContainerImage_FromRegistry(jsii.String("amazon/amazon-ecs-sample"), nil),
			},
			PublicLoadBalancer: jsii.Bool(true), // Default is false
		})

	return stack

}
```

このクラスは、AWS CloudFormation に 500 行を超えるテンプレートを生成します。AWS CDK アプリをデプロイすると、次のタイプの 50 を超えるリソースが生成されます。
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-eip.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-eip.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-internetgateway.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-internetgateway.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-natgateway.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-natgateway.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-route.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-route.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-routetable.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-routetable.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-security-group.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-security-group.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet-route-table-assoc.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-subnet-route-table-assoc.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpc-gateway-attachment.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpc-gateway-attachment.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpc.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ec2-vpc.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-cluster.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-cluster.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-service.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-service.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-taskdefinition.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-taskdefinition.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-listener.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-listener.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-loadbalancer.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-loadbalancer.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-targetgroup.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticloadbalancingv2-targetgroup.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-policy.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-policy.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-role.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-role.html) 
+  [https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-loggroup.html](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-loggroup.html) 

## AWS CDK の機能
<a name="home-features"></a>

### AWS CDK GitHub リポジトリ
<a name="home-features-repo"></a>

公式の AWS CDK GitHub リポジトリについては、[aws-cdk](https://github.com/aws/aws-cdk) を参照してください。こちらでは、[問題](https://github.com/aws/aws-cdk/issues)の送信、[ライセンス](https://github.com/aws/aws-cdk/blob/main/LICENSE)の確認、[リリース](https://github.com/aws/aws-cdk/releases)の追跡などを行うことができます。

AWS CDK はオープンソースであるため、さらに優れたツールにするには、チームはユーザーが貢献することを奨励します。詳細については、「[AWS Cloud Development Kit (AWS CDK) への寄与](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)」を参照してください。

### AWS CDK API リファレンス
<a name="home-features-api"></a>

CDK アプリケーションを定義してアプリケーションに CDK コンストラクトを追加するため、AWS CDK コンストラクトライブラリは API を提供します。詳細については、「[AWS CDK API リファレンス](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-construct-library.html)」を参照してください。

### コンストラクトプログラミングモデル
<a name="home-features-cpm"></a>

コンストラクトプログラミングモデル (CPM) は、AWS CDK にちなんだ概念を追加のドメインに拡張します。CPM を使用するその他のツールには次のものが含まれます。
+  [CDK for Terraform](https://www.terraform.io/docs/cdktf/index.html) (CDKtf)
+  [CDK for Kubernetes](https://cdk8s.io/)(CDK8s)
+  プロジェクト設定を構築するための [Projen](https://github.com/projen/projen)

### コンストラクトハブ
<a name="home-features-hub"></a>

[Construct Hub](https://constructs.dev/) は、オープンソース AWS CDK ライブラリを検索、公開、共有できるオンラインレジストリです。

## 次のステップ
<a name="home-next"></a>

AWS CDK の使用を開始するには、「[AWS CDK の使用を開始する](getting-started.md)」を参照してください。

## 詳細
<a name="home-learn"></a>

AWS CDK の詳細については、以下を参照してください。
+  **[AWS CDK のコアコンセプトを知る](core-concepts.md)** – AWS CDK の重要な概念と用語。
+  **[AWS CDK ワークショップ](https://cdkworkshop.com/)** – AWS CDK を知って使用する実践的なワークショップ。
+  **[AWS CDK パターン](https://cdkpatterns.com/)** – AWS エキスパートによって AWS CDK のために構築された AWS サーバーレスアーキテクチャパターンのオープンソースコレクション。
+  **[AWS CDK コード例](https://github.com/aws-samples/aws-cdk-examples)** – AWS CDK プロジェクトの例の GitHub リポジトリ。
+  **[cdk.dev](https://cdk.dev/)** – コミュニティ Slack ワークスペースを含む AWS CDK のコミュニティ駆動型ハブ。
+  **[Awesome CDK](https://github.com/kalaiser/awesome-cdk)** - AWS CDK オープンソースプロジェクト、ガイド、ブログ、およびその他のリソースの厳選されたリストを含む GitHub リポジトリ。
+  **[AWS ソリューションコンストラクト](https://aws.amazon.com/solutions/constructs/)** – 本番稼働用のアプリケーションに簡単に組み立てることができる精査された Infrastructure as Code (IaC) 設定パターン。
+  **[AWS デベロッパーツールブログ](https://aws.amazon.com/blogs/developer/category/developer-tools/aws-cloud-development-kit/)** – AWS CDK 用にフィルタリングされたブログ記事。
+  **[Stack Overflow での AWS CDK](https://stackoverflow.com/questions/tagged/aws-cdk) ** – Stack Overflow の **aws-cdk** でタグ付けされた質問。
+  **[AWS Cloud9 の AWS CDK チュートリアル](https://docs.aws.amazon.com/cloud9/latest/user-guide/sample-cdk.html)** – AWS Cloud9 開発環境で AWS CDK を使用するチュートリアル。

AWS CDK の関連トピックの詳細については、次の内容を参照してください。
+  **[AWS CloudFormation の概念](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-whatis-concepts.html)** – AWS CDK は AWS CloudFormation と連携するように構築されているため、主要な AWS CloudFormation の概念を知って理解することをお勧めします。
+  **[AWS 用語集](https://docs.aws.amazon.com/general/latest/gr/glos-chap.html)** – AWS 全体で使用される主要な用語の定義。

サーバーレスアプリケーションの開発およびデプロイを簡素化するために使用できる AWS CDK に関連するツールの詳細については、次の内容を参照してください。
+  **[AWS サーバーレスアプリケーションモデル](https://aws.amazon.com/serverless/sam/)** – AWS でサーバーレスアプリケーションを構築および実行するエクスペリエンスを簡素化および改善するオープンソースの開発者ツール。
+  **[AWS Chalice](https://github.com/aws/chalice)** – Python でサーバーレスアプリを記述するためのフレームワーク。