Skip to content

Instantly share code, notes, and snippets.

@noid11
Last active August 2, 2020 03:53
Show Gist options
  • Save noid11/b832c04118a5dbc3f4e1f9540ffd1a05 to your computer and use it in GitHub Desktop.
Save noid11/b832c04118a5dbc3f4e1f9540ffd1a05 to your computer and use it in GitHub Desktop.
CDK (TypeScript) 入門メモ

CDK (TypeScript) 入門メモ

公式ドキュメントやサンプル見つつやっていく

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!

CDK アプリのビルド

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 のコンパイルを行っているようだ

CDK App のリスト確認

List the stacks in the app
https://docs.aws.amazon.com/cdk/latest/guide/hello_world.html#hello_world_tutorial_list_stacks

cdk ls

S3 バケット追加

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
    })
  }
}

シンセサイズ

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 に対するデプロイが行われる

CDK アプリの改修

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 どうするとか、その辺りが気になる
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment