serverless を試す
API Gateway + Lambda を使ってHTTP APIを作るには、何を使うのが一番良いのだろうと まずは Apex – Serverless Infrastructure を試してみたものの、API Gatewayとlambdaを繋げるところまでがサポートされていなく そこをterraformでやるのは厳しいかなと思って別なものを探していたら、Serverless というそのままの名前のツールがちょうど良さそうだった。
Serverless
AWS Lambda を使ったAPI サーバーの設定などを管理できるビルドツール。
install
npm install -g serverless
Create credentials
まずパッケージで必要なcredentialsを設定する。
これをすると ~/.aws/credntial
にファイルを作成してくれる
$ serverless config credentials -p aws -k <aws-key> -s <aws-secret>
テンプレートの作成
$ serverless create -t aws-nodejs
いくつかテンプレートが用意されているので、それを使うと、
handler.js
と serverless.yml
が作成される。
handler.js
module.exports.hello = (event, context, callback) => { const response = { statusCode: 200, body: JSON.stringify({ message: 'Go Serverless v1.0! Your function executed successfully!', input: event, }), }; callback(null, response); // Use this code if you don't use the http event with the LAMBDA-PROXY integration // callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event }); };
serverless.yml
service: aws-nodejs # NOTE: update this with your service name provider: name: aws runtime: nodejs4.3 functions: hello: handler: handler.hello
serverlessにawsの設定などを記載していくことになる。
とりあえずエンドポイントを実行できるようにしてみる
functionsにイベントを記載する
functions: hello: handler: handler.hello events: - http: path: / method: get
http イベントを設定すると、API Gateway と紐づけてくれる。 イベントは他にもいくつかある Serverless - AWS Lambda - Events 。
あとはデプロイをするだけ。
$ serverless deploy -r ap-northeast-1 -s dev
deploy
devにデプロイしてみる
$ serverless deploy -r ap-northeast-1 -s dev Serverless: Creating Stack... Serverless: Checking Stack create progress... ..... Serverless: Stack create finished... Serverless: Packaging service... Serverless: Uploading CloudFormation file to S3... Serverless: Uploading service .zip file to S3 (583 B)... Serverless: Updating Stack... Serverless: Checking Stack update progress... ............................ Serverless: Stack update finished... Service Information service: aws-nodejs stage: dev region: ap-northeast-1 api keys: None endpoints: GET - https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/ functions: aws-nodejs-dev-hello: arn:aws:lambda:ap-northeast-1:59XXXXXXXXXX:function:aws-nodejs-dev-hell
色々なものを一気に作ってくれる
role も作ってくれる。 arn:aws:iam::59XXXXXXXXXX:role/aws-nodejs-dev-ap-northeast-1-lambdaRole
アクセスできるようになる
% curl https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/ {"message":"Go Serverless v1.0! Your function executed successfully!","input":{"resource":"/","path":"/","httpMethod":"GET","headers":{"Accept":"*/*","CloudFront-Forwarded-Proto":"https","CloudFront-Is-Desktop-Viewer":"true","CloudFront-Is-Mobile-Viewer":"false","CloudFront-Is-SmartTV-Viewer":"false","CloudFront-Is-Tablet-Viewer":"false","CloudFront-Viewer-Country":"JP","Host":"xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com","User-Agent":"curl/7.49.1","Via":"1.1 25e2bd4c76e5ce50a0e42ea7d68cb8be.cloudfront.net (CloudFront)","X-Amz-Cf-Id":"vNC5HOdFJ9wDxnmXVq9MwGrMwsFGtFHwKCohNx3PGyWbw6mJiXeT3Q==","X-Forwarded-For":"xxx.xxx.xx.xx, xx.xxx.xxx.xx","X-Forwarded-Port":"443","X-Forwarded-Proto":"https"},"queryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"accountId":"592382460000","resourceId":"7wt5tp3333","stage":"dev","requestId":"e2bef286-bf5c-11e6-8ed0-8b69baa26de7","identity":{"cognitoIdentityPoolId":null,"accountId":null,"cognitoIdentityId":null,"caller":null,"apiKey":null,"sourceIp":"xxx.xxx.xx.xx","accessKey":null,"cognitoAuthenticationType":null,"cognitoAuthenticationProvider":null,"userArn":null,"userAgent":"curl/7.49.1","user":null},"resourcePath":"/","httpMethod":"GET","apiId":"qeg4d8alu6"},"body":null,"isBase64Encoded":false}}
確認
--region
は必須
$ serverless deploy list -r ap-northeast-1 Serverless: Listing deployments: Serverless: ------------- Serverless: Timestamp: 1481428344900 Serverless: Datetime: 2016-12-11T03:52:24.900Z Serverless: Files: Serverless: - aws-nodejs.zip Serverless: - compiled-cloudformation-template.json
削除
remove でできる。
$ serverless remove -r ap-northeast-1 Serverless: Getting all objects in S3 bucket... Serverless: Removing objects in S3 bucket... Serverless: Removing Stack... Serverless: Checking Stack removal progress... ................. Serverless: Stack removal finished.
roleも削除してくれる
すごい楽
アプリケーションを作る立場からするとすごい楽。だけど、構成管理としては 使っているサービスは terraform に寄せたいけれど、現実的に厳しそう(そもそもterraformでlambdaでデプロイするのに、毎回アプリケーションをまとめたファイルを用意しなきゃいけない)で、なんかもやもやしてる。