公式ドキュメントやサンプル見つつやっていく
Your first AWS CDK app - AWS Cloud Development Kit (AWS CDK)
https://docs.aws.amazon.com/ja_jp/cdk/latest/guide/hello_world.html
CDK TypeScript Reference
https://docs.aws.amazon.com/cdk/api/latest/typescript/api/index.html
API Reference · AWS CDK
https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.html
aws/aws-cdk: The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://github.com/aws/aws-cdk
Create the app
https://docs.aws.amazon.com/cdk/latest/guide/hello_world.html#hello_world_tutorial_create_app
mkdir hello-cdk
cd hello-cdk
npm init -y
sudo npm install -g aws-cdk
cdk init app --language typescript
project local に aws-cdk install して動かせないのかな?と思ったら cdk init が失敗した
global にインストールしたくないなぁ
% npx cdk init app --language typescript
`cdk init` cannot be run in a non-empty directory!
Build the app
https://docs.aws.amazon.com/cdk/latest/guide/hello_world.html#hello_world_tutorial_build
以下のコマンドで OK
npm run build
build で何しているのかというと・・・
cdk init 時に、以下のような package.json ファイルが生成されている
{
"name": "hello-cdk",
"version": "0.1.0",
"bin": {
"hello-cdk": "bin/hello-cdk.js"
},
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"test": "jest",
"cdk": "cdk"
},
"devDependencies": {
"@aws-cdk/assert": "1.56.0",
"@types/jest": "^25.2.1",
"@types/node": "10.17.5",
"jest": "^25.5.0",
"ts-jest": "^25.3.1",
"aws-cdk": "1.56.0",
"ts-node": "^8.1.0",
"typescript": "~3.7.2"
},
"dependencies": {
"@aws-cdk/core": "1.56.0",
"source-map-support": "^0.5.16"
}
}
"build": "tsc",
とあるので、 TypeScript のコンパイルを行っているようだ
List the stacks in the app
https://docs.aws.amazon.com/cdk/latest/guide/hello_world.html#hello_world_tutorial_list_stacks
cdk ls
Add an Amazon S3 bucket
https://docs.aws.amazon.com/cdk/latest/guide/hello_world.html#hello_world_tutorial_add_bucket
npm install @aws-cdk/aws-s3
lib/hello-cdk-stack.ts
を以下のように編集
import * as cdk from '@aws-cdk/core';
import * as s3 from '@aws-cdk/aws-s3';
export class HelloCdkStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
new s3.Bucket(this, 'my-first-bucket', {
versioned: true
})
}
}
@aws-cdk/core
が CDK のコアとなるパッケージcdk.Stack
は CloudFormation Stack とは違うっぽい- reference https://docs.aws.amazon.com/cdk/api/latest/typescript/api/core/stack.html#core_Stack
- constructor
- reference https://docs.aws.amazon.com/cdk/api/latest/typescript/api/core/stack.html#constructors
- code https://github.com/aws/aws-cdk/blob/cd25812e93fe69500ec84e373e9f0f8afae70524/packages/%40aws-cdk/core/lib/stack.ts#L307
scope: cdk.Construct
- node を生成する範囲のようだ
- どの CloudFormation Stack で管理するか、といったスコープかも
- code https://github.com/aws/aws-cdk/blob/cd25812e93fe69500ec84e373e9f0f8afae70524/packages/%40aws-cdk/core/lib/construct-compat.ts#L68
id: string
props?: cdk.StackProps
s3.Bucket
Synthesize an AWS CloudFormation template
https://docs.aws.amazon.com/cdk/latest/guide/hello_world.html#hello_world_tutorial_synth
npm run build
cdk synth
cdk synth
すると、コード内容を元に Cloudformation template が yaml 形式で出力される- cdk におけるシンセサイズは、コード内容から Cloudformation template を合成する。という意味合いっぽい
Deploying the stack
https://docs.aws.amazon.com/cdk/latest/guide/hello_world.html#hello_world_tutorial_deploy
npm run build
cdk deploy
- これで実際に Cloudformation に対するデプロイが行われる
Modifying the app
https://docs.aws.amazon.com/cdk/latest/guide/hello_world.html#hello_world_tutorial_modify
lib/hello-cdk-stack.ts
を以下のように編集
import * as cdk from '@aws-cdk/core';
import * as s3 from '@aws-cdk/aws-s3';
export class HelloCdkStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
new s3.Bucket(this, 'my-first-bucket', {
versioned: true,
removalPolicy: cdk.RemovalPolicy.DESTROY
})
}
}
差分を見る
npm run build
cdk diff
diff 結果の例
% cdk diff
Stack HelloCdkStack
Resources
[~] AWS::S3::Bucket my-first-bucket my-first-bucketxxx
├─ [~] DeletionPolicy
│ ├─ [-] Retain
│ └─ [+] Delete
└─ [~] UpdateReplacePolicy
├─ [-] Retain
└─ [+] Delete
確認したらデプロイ
npm run build
cdk deploy
Destroying the app's resources
https://docs.aws.amazon.com/cdk/latest/guide/hello_world.html#hello_world_tutorial_destroy
cdk destroy
- とりあえず動かす所まではできた。
- CDK の update が激しいのか、ドキュメントに載っているコードがそのままでは動かなかったりするので多少コード読む力が必要になる印象
- まぁ個人的に TypeScript できるようになりたい気持ちもあるのでちょうどよい
- CDK における概念とか、テストどうすればいいとか、 CI/CD どうするとか、その辺りが気になる