#
ドキュメント

Document

自分のための備忘録です。

AWS CDK

環境構築

TypeScriptを使用言語とする。

$ npm i -g typescript
$ npm i -g aws-cdk

$ cdk --version
1.112.0 (build 2815a44)

ワークフロー

概要

Your first AWS CDK app

The standard AWS CDK development workflow is similar to the workflow you're already familiar with as a developer, just with a few extra steps.

  1. Create the app from a template provided by the AWS CDK
  2. Add code to the app to create resources within stacks
  3. Build the app (optional; the AWS CDK Toolkit will do it for you if you forget)
  4. Synthesize one or more stacks in the app to create an AWS CloudFormation template
  5. Deploy one or more stacks to your AWS account

ワークフロー詳細

  1. cdk init app --language typescript
  2. コードを変更
  3. npm run build(実際は忘れてもCDKが自動で行ってくれるので省略可能)
  4. cdk synth --profile {{profile_name}}
  5. (既にデプロイされている場合) cdk diff --profile {{profile_name}}
  6. cdk deploy --profile {{profile_name}}

フロー備考

  • cdkコマンドはスタックを指定する
  • スタックが一つの場合は省略できる
  • すべてのスタックを指定するには--allオプションを付与する

ワークフローで使用するコマンド

ref. AWS CDK Toolkit (cdk command)

cdk synth

$ cdk synth
  • 標準出力にCloudFormationのコードを出力
  • cdk.outディレクトリにJSONのCloudFormationテンプレートを格納

cdk deploy

  • スタック作成 or スタック更新

CDKプログラミングガイド

  • app: 一つ以上のstackで定義される
  • stackCloudFormationstackと等しい):1つ以上のconstructを含む

    Stacks (equivalent to AWS CloudFormation stacks) contain constructs, each of which defines one or more concrete AWS resources, such as Amazon S3 buckets, Lambda functions, Amazon DynamoDB tables, and so on.

    https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html

  • construct

    A construct represents a "cloud component" and encapsulates everything AWS CloudFormation needs to create the component. A construct can represent a single resource, such as an Amazon Simple Storage Service (Amazon S3) bucket, or it can represent a higher-level component consisting of multiple AWS resources.

    https://docs.aws.amazon.com/cdk/latest/guide/constructs.html

    A construct can represent a single resource, such as an Amazon Simple Storage Service (Amazon S3) bucket, or it can represent a higher-level component consisting of multiple AWS resources

    https://docs.aws.amazon.com/cdk/latest/guide/constructs.html

Constructsの分類

ref. https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html

  • AWS CloudFormation-only or L1 (short for "level 1"):CloudFormationと直接結びついている

    AWS CloudFormation resources always have names that begin with Cfn.

  • Curated or L2:AWS CDKチームにより特定のユースケースに対応し、インフラストラクチャ開発を簡素化するためにL1をカプセル化したもの。

    These constructs are carefully developed by the AWS CDK team to address specific use cases and simplify infrastructure development. For the most part, they encapsulate L1 modules, providing sensible defaults and best-practice security policies. For example, in the Amazon S3 module, Bucket is the L2 module for an Amazon S3 bucket.

  • Patterns or L3:複数のリソースをカプセル化 選ばれたパラメーターを設定するだけで適切に作成 L1,L2モジュールからは分離されている

    Patterns declare multiple resources to create entire AWS architectures for particular use cases. All the plumbing is already hooked up, and configuration is boiled down to a few important parameters. In the AWS Construct Library, patterns are in separate modules from L1 and L2 constructs.

Curated:精選された

AWS Construct Library

CDK/TypeScriptをインストール

$ npm -g list
/Users/shiroshi/.nvm/versions/node/v15.13.0/lib
├── @wordpress/env@4.0.0
├── aws-cdk@1.115.0
├── npm@7.19.1
└── typescript@4.3.5

環境整備

https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html

$ npm install -g aws-cdk

アカウントで初めてCDKを使用する場合は以下コマンドでCloudFormationにスタックCDKToolkitが作成されて、
CDKで必要S3バケットやポリシーなどのリソースが作成される。

$ cdk bootstrap aws://ACCOUNT-NUMBER/REGION
$ cdk bootstrap aws://ACCOUNT-NUMBER/ap-northeast-1
 ⏳  Bootstrapping environment aws://ACCOUNT-NUMBER/ap-northeast-1...
CDKToolkit: creating CloudFormation changeset...

 ✅  Environment aws://ACCOUNT-NUMBER/ap-northeast-1 bootstrapped.

S3にcdktoolkit-stagingbucket-xxxxxxxxxxというバケットが作成。

開発手順

https://aws.amazon.com/jp/visualstudiocode/ より引用。

  1. Create the app from a template provided by the AWS CDK
  2. Add code to the app to create resources within stacks
  3. Build the app (optional; the AWS CDK Toolkit will do it for you if you forget)
  4. Synthesize one or more stacks in the app to create an AWS CloudFormation template
  5. Deploy one or more stacks to your AWS account

最初のチュートリアル

Constructsの引数

  • scope: Tells the bucket that the stack is its parent: it is defined within the scope of the stack. You can define constructs inside of constructs, creating a hierarchy (tree).
  • Id: The logical ID of the Bucket within your AWS CDK app. This (plus a hash based on the bucket's location within the stack) uniquely identifies the bucket across deployments so the AWS CDK can update it if you change how it's defined in your app. Buckets can also have a name, which is separate from this ID (it's the bucketName property).
  • props: A bundle of values that define properties of the bucket. Here we've defined only one property: versioned, which enables versioning for the files in the bucket.

重要箇所

Familiarity with AWS CloudFormation is also useful, as the output of an AWS CDK program is a AWS CloudFormation template

https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html

An app defines one or more stacks. Stacks (equivalent to AWS CloudFormation stacks) contain constructs, each of which defines one or more concrete AWS resources, such as Amazon S3 buckets, Lambda functions, Amazon DynamoDB tables, and so on.

https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html

  • constructs:構成(名詞)
  • construct:構成する(動詞)
Constructs come in three fundamental flavors:

AWS CloudFormation-only or L1 (short for "level 1"). These constructs correspond directly to resource types defined by AWS CloudFormation. In fact, these constructs are automatically generated from the AWS CloudFormation specification, so when a new AWS service is launched, the AWS CDK supports it as soon as AWS CloudFormation does.

AWS CloudFormation resources always have names that begin with Cfn. For example, in the Amazon S3 module, CfnBucket is the L1 module for an Amazon S3 bucket.

Curated or L2. These constructs are carefully developed by the AWS CDK team to address specific use cases and simplify infrastructure development. For the most part, they encapsulate L1 modules, providing sensible defaults and best-practice security policies. For example, in the Amazon S3 module, Bucket is the L2 module for an Amazon S3 bucket.

L2 modules may also define supporting resources needed by the primary resource. Some services have more than one L2 module in the Construct Library for organizational purposes.

Patterns or L3. Patterns declare multiple resources to create entire AWS architectures for particular use cases. All the plumbing is already hooked up, and configuration is boiled down to a few important parameters. In the AWS Construct Library, patterns are in separate modules from L1 and L2 constructs.

https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html

Ref.