こんにちは、技術1課の加藤です。
つい先日のアップデートで AWS Solutions Constructs が発表されました。
AWS CDK の Constructs として提供され、これを使えばAWS のアーキテクトが作成した Well-Architected な構成を簡単に構築できるとのこと。
早速試してみました。
AWS Solutions Constructs
AWS CDK の Constructs というのは、1つ以上のサービスをまとめた最小構成を表す概念です。
例えば S3 + CloudFront で静的ファイルの配信を行う、など、意味のあるまとまりを定義して使い回すことができます。
今回発表された Solutions Constructs はAWS のアーキテクトが監修した AWS CDK の Constructs です。
Well-Architected Framework に則り、AWS のベストプラクティスとなる設定がされた Constructs 集となっており、ユーザーはこれを使うことで非常に簡単にシステム構成を作ることができるようになります。
現時点で 25 の構成が用意されていました。以下に列挙しておきます。
今回作成する環境
今回は AWS Solutions Constructs に用意されている
を用いて、 API Gateway + Lambda の簡単な API を作成していきます。APIを叩くと、
Hello, AWS Solutions Constructs! You've hit ${path}
という形で叩いたパス情報とともに Hello と返してくれるよう設定していきましょう。
基本的に以下のドキュメントの手順に沿って作成を進めていきます。
Walkthrough - Part 1 - AWS Solutions Constructs
準備
AWS Solutions Constructs は AWS CDK 1.46.0 以上でサポートされています。
cdk のバージョンを確認して 1.46.0 より小さかった方はアップデートをしておいてください。
npm でアップデートする記事があったので貼っておきます。
AWS CDK CLI のバージョンアップを行う方法 – サーバーワークスエンジニアブログ
ちなみに僕は brew で入れていたので以下コマンドでアップデートしました。
$ brew upgrade aws-cdk $ cdk version 1.46.0 (build 63860b2)
プロジェクト作成
では CDK で管理するプロジェクトを作成します。
言語は TypeScript で進めていきます。
$ mkdir hello-constructs $ cd hello-constructs $ cdk init --language typescript
AWS CDK のバージョンを 1.46.0 に指定
package.json にて CDK のバージョンが 1.46.0 以上に指定されていることを確認します。
{
"name": "hello-constructs",
"version": "0.1.0",
"bin": {
"hello-constructs": "bin/hello-constructs.js"
},
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"test": "jest",
"cdk": "cdk"
},
"devDependencies": {
"@aws-cdk/assert": "1.46.0",
"@types/jest": "^25.2.1",
"@types/node": "10.17.5",
"jest": "^25.5.0",
"ts-jest": "^25.3.1",
"aws-cdk": "1.46.0",
"ts-node": "^8.1.0",
"typescript": "~3.7.2"
},
"dependencies": {
"@aws-cdk/core": "1.46.0",
"source-map-support": "^0.5.16"
}
}
パッケージをインストールしましょう。
$ npm install
TypeScript のコードをビルドして JavaScript のコードを生成します。
cdk synth してCDK のバージョンが正しく 1.46.0以上を指定していることを確認しておきましょう。
$ npm run build
$ cdk synth
Resources:
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Modules: aws-cdk=1.46.0,@aws-cdk/cdk-assets-schema=1.46.0,@aws-cdk/cloud-assembly-schema=1.46.0,@aws-cdk/core=1.46.0,@aws-cdk/cx-api=1.46.0,jsii-runtime=node.js/v14.4.0
...
Lambda のコードを書く
では Lambda のコードを書いていきます。
Lambda ディレクトリ を作り hello.js を作りましょう。(今回 Lambda のコードは JavaScript を利用します)
$ mkdir lambda $ touch lambda/hello.js
作成した JavaScript ファイルに以下を書き込んでいきます。
exports.handler = async function(event) {
console.log("request:", JSON.stringify(event, undefined, 2));
return {
statusCode: 200,
headers: { "Content-Type": "text/plain" },
body: `Hello, AWS Solutions Constructs! You've hit ${event.path}\n`
};
};
AWS CDK と AWS Solutions Constructs をインストール
必要なライブラリをインストールします。
最後にインストールする @aws-solutions-constructs/aws-apigateway-lambda@1.46.0 が今回の肝である Solutions Constructs ですね。
$ npm install -s @aws-cdk/aws-lambda@1.46.0 $ npm install -s @aws-cdk/aws-apigateway@1.46.0 $ npm install -s @aws-solutions-constructs/aws-apigateway-lambda@1.46.0
API Gateway / Lambda パターンをスタックに追加
lib/hello-constructs-stack.ts を以下のように編集します。
import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import * as api from '@aws-cdk/aws-apigateway';
import { ApiGatewayToLambda, ApiGatewayToLambdaProps } from '@aws-solutions-constructs/aws-apigateway-lambda';
export class HelloConstructsStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// The code that defines your stack goes here
const api_lambda_props: ApiGatewayToLambdaProps = {
deployLambda: true,
lambdaFunctionProps: {
code: lambda.Code.fromAsset('lambda'),
runtime: lambda.Runtime.NODEJS_12_X,
handler: 'hello.handler'
},
apiGatewayProps: {
defaultMethodOptions: {
authorizationType: api.AuthorizationType.NONE
}
}
};
new ApiGatewayToLambda(this, 'ApiGatewayToLambda', api_lambda_props);
}
}
API Gateway に渡すプロパティを作ったら、あとは ApiGatewayToLambda を new するだけで OK です。簡単すぎる。
何が出来上がるのか確認しておきましょう
$ npm run build $ cdk diff
長くなるので結果の引用は避けますが、
- API Gateway
- Lambda
- CloudWatch Logs のロググループ
- 各種権限セット
あたりが作成されることがわかります。
デプロイ
では実際にデプロイしてみましょう。
$ cdk deploy ... ✅ HelloConstructsStack Outputs: HelloConstructsStack.ApiGatewayToLambdaLambdaRestApiEndpointxxxxxxxx = https://xxxxxxx.execute-api.ap-northeast-1.amazonaws.com/prod/ Stack ARN: arn:aws:cloudformation:ap-northeast-1:xxxxxxxxxxxx:stack/HelloConstructsStack/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
最後に正しくAPIが動くか確認しましょう。
$ curl https://xxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/prod/ Hello, AWS Solutions Constructs! You've hit / $ curl https://xxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/prod/cdk-saikou Hello, AWS Solutions Constructs! You've hit /cdk-saikou
無事想定通り動いていることが確認できました。
最後に
今回は Solutions Constructs を利用してみました。
コードの行数を大幅に減らし、楽に、かつ良い構成が作れるというのは非常に魅力的ですね。
今回参考にした公式のハンズオンには続きがあるので、よければ挑戦してみてください。
Walkthrough - Part 2 - AWS Solutions Constructs
おまけ
現在、毎日 AWS というラジオ感覚放送を YouTube にて配信しています
その中で今回取り上げた Solutions Constructs について触れていますので、こちらもお聞きください!
毎日手軽にAWSのアップデート情報がキャッチアップできますよ