SERVERLESS FRAMEWORKが最高すぎる
最近SERVERLESS FRAMEWORKってのがリリースされました(v1) serverless.com
これなに
プログラムを書いてデプロイすると AWS LambdaとAPIGateway環境にデプロイされる。 ルーティングもYAMLで記述ができる。 基本的にはこれだけ。
動作イメージ
module.exports.piyo = (event, context, callback) => { const response = { statusCode: 200, body: JSON.stringify({ message: 'おっけー!' }), }; callback(null, response); }
service: servicename provider: name: aws runtime: nodejs4.3 region: ap-northeast-1 functions: piyo: handler: handler.piyo events: - http: path: piyo method: get
これでデプロイすると、
$ serverless deploy Serverless: Packaging service… Serverless: Merged stage variables into ApiGateway Deployment Serverless: Removing old service versions… Serverless: Uploading CloudFormation file to S3… Serverless: Uploading service .zip file to S3… Serverless: Updating Stack… Serverless: Checking Stack update progress… .............. Serverless: Stack update finished… Service Information service: servicename stage: dev region: ap-northeast-1 api keys: None endpoints: POST - https://xxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/piyo functions: servicename-dev-piyo: arn:aws:lambda:ap-northeast-1:000000:function:ms-servicename-dev-piyo
https://xxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/piyo できた!(さんぷるなので動いてないです)
実用する
SERVERLESS FRAMEWORK本体の責務としては「デプロイするまで」というイメージです。 なので単体だと開発環境は全く用意されておらず、 開発環境を用意するには別途プラグインを入れていく必要があります。 中でも必須っぽいプラグインがいくつかあるので紹介すると、
serverless-run-function-plugin
書いたfunctionをローカルで実行する為のプラグイン
$ serverless run -f piyo
と叩けば実行される。
serverless-offline
Lambda環境をエミュレートしてくれて、ローカルでサーバを起動するプラグイン
$ serverless offline --port 3000
で立ち上がる。あとはhttpリクエスト送るだけ。
serverless-plugin-stage-variables
ステージ変数を定義出来る
(中略) stageVariables: google_sheet_id: === aws_access_key_id: === aws_secret_access_key: ===
使ってみて
- サーバレスのシステムがフレームワークに乗っかる形で書けるのでチームに共有しやすそう
- ローカル環境で動作確認できるのがすごく良い
- dev/prod環境わけられたり、とても実用的
結構本格的に使っていきそう。