TypeScript
を使用言語とする。
$ npm i -g typescript
$ npm i -g aws-cdk
$ cdk --version
1.112.0 (build 2815a44)
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.
- Create the app from a template provided by the AWS CDK
- Add code to the app to create resources within stacks
- Build the app (optional; the AWS CDK Toolkit will do it for you if you forget)
- Synthesize one or more stacks in the app to create an AWS CloudFormation template
- Deploy one or more stacks to your AWS account
cdk init app --language typescript
npm run build
(実際は忘れてもCDKが自動で行ってくれるので省略可能)cdk synth --profile {{profile_name}}
synthesize
統合するcdk synth
を明示的に実行しなくてもdeploy
時にCDKが自動で実行するので必須ではないIt is optional (though good practice) to synthesize before deploying. The AWS CDK synthesizes your stack before each deployment.
-- https://docs.aws.amazon.com/cdk/v2/guide/hello_world.html
cdk diff --profile {{profile_name}}
cdk deploy --profile {{profile_name}}
cdk
コマンドはスタックを指定する--all
オプションを付与するref. AWS CDK Toolkit (cdk command)
$ cdk synth
CloudFormation
のコードを出力CloudFormation
テンプレートを格納app
: 一つ以上のstack
で定義されるstack
(CloudFormation
のstack
と等しい):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
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
:精選された
$ 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/ より引用。
- Create the app from a template provided by the AWS CDK
- Add code to the app to create resources within stacks
- Build the app (optional; the AWS CDK Toolkit will do it for you if you forget)
- Synthesize one or more stacks in the app to create an AWS CloudFormation template
- Deploy one or more stacks to your AWS account
- 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 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