今日話すこと

  • AWS CDK とは?
  • AWS CDK の書き方
  • サンプル

はじめます

AWS CDK とは?

TL;DR

TS を書くと AWS 環境が出来上がる

何言ってる分からn

見てみよう

AWS CDK の書き方

最小ファイル構成

package.json

{
"name": "cdk-sample",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"cdk": "cdk",
"synth": "cdk synth",
"deploy": "cdk deploy"
},
"devDependencies": {
"@types/node": "^10.9.4",
"aws-cdk": "^0.30.0",
"ts-node": "^8.1.0",
"typescript": "^3.2.4"
},
"dependencies": {
"@aws-cdk/aws-s3": "*",
"@aws-cdk/cdk": "*"
}
}

cdk.json

{ "app": "ts-node index.ts" }

index.ts

import cdk = require("@aws-cdk/cdk");
import s3 = require("@aws-cdk/aws-s3");
class MyStack extends cdk.Stack {
constructor(parent: cdk.App, name: string) {
super(parent, name);
new s3.Bucket(this, "MyBucket", {
bucketName: "bucket-tte-baketsu-no-koto-nandane"
});
}
}
const app = new cdk.App();
new MyStack(app, "CdkAample");

cdk synth

cdk deploy

s3 bucket できた

やったね!

TS で書ける CFn(Cloud Formation) て感じ

CFn は(あの使いにくい)AWS コンソールをそのままいじっている感じ

CDK はより直感的

TS の型に導かれる

インフラ構成の変更をコードレビューできるよ!

注意 !!!

サンプル

Static Site

import cdk = require('@aws-cdk/cdk')
import cloudfront = require('@aws-cdk/aws-cloudfront')
import s3 = require('@aws-cdk/aws-s3')
import s3deploy = require('@aws-cdk/aws-s3-deployment')
class MyStack extends cdk.Stack {
constructor(parent: cdk.App, name: string) {
super(parent, name)
const siteBucket = new s3.Bucket(this, 'MyBucket', {
bucketName: 'bucket-tte-baketsu-no-koto-nandane',
websiteIndexDocument: 'index.html',
publicReadAccess: true,
})
new s3deploy.BucketDeployment(this, 'MySiteDeployment', {
source: s3deploy.Source.asset('./dist'),
destinationBucket: siteBucket,
})
const distribution = new cloudfront.CloudFrontWebDistribution(
this,
'MyWebDistribution',
{
originConfigs: [
{
s3OriginSource: { s3BucketSource: siteBucket },
behaviors: [{ isDefaultBehavior: true }],
},
],
},
)
new cdk.CfnOutput(this, 'DistributionId', {
value: distribution.distributionId,
})
new cdk.CfnOutput(this, 'DomainName', {
value: distribution.domainName,
})
}
}
const app = new cdk.App()
new MyStack(app, 'CdkAample')
できたページ
コードはこちら

Lambda Rest Api

import cdk = require('@aws-cdk/cdk')
import lambda = require('@aws-cdk/aws-lambda')
import apigateway = require('@aws-cdk/aws-apigateway')
class MyStack extends cdk.Stack {
constructor(parent: cdk.App, name: string) {
super(parent, name)
const lambdaFn = new lambda.Function(this, 'MyLambda', {
code: new lambda.AssetCode('./functions'),
handler: 'hoge.handler',
runtime: lambda.Runtime.NodeJS810,
})
const restApi = new apigateway.LambdaRestApi(this, 'MyRestApi', {
handler: lambdaFn,
})
new cdk.CfnOutput(this, 'functionName', {
value: lambdaFn.functionName,
})
new cdk.CfnOutput(this, 'restApiUrl', {
value: restApi.url,
})
}
}
const app = new cdk.App()
new MyStack(app, 'CdkLambdaSample')

hoge.js

module.exports.handler = async (event, context) => {
const now = new Date().toISOString()
console.info(`now is ${now} !!!`)
return {
isBase64Encoded: false,
statusCode: 200,
headers: {},
body: JSON.stringify({ now }),
}
}
できたAPI
コードはこちら

まとめ

AWS CDK はいいぞ!!

(プロダクトでのご利用は計画的に)

Cloud Formation もいいぞ!!

(プロダクトでガンガン使える)

  • CureApp
  • アプリで医療機器を作ってる
  • AWS, TS, react, RN, Node.js, MongoDB

おわり。

Links

  • Docs
  • Github
  • AWS Site
  • Examples

ご清聴ありがとうございました!!