この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
モバイルアプリサービス部の五十嵐です。
久しぶりに Serverless を使おうとしたところ、バージョンが1.0系になっていました。どうやら0.x系から大きく変わっているようなので、チュートリアルがてら前回の記事(Slash CommandsとAWS Lambdaで作るChatOpsの基本機能 | Developers.IO)で作ったLambdaをserverless-v1.0.0-rc.1を使ってデプロイしてみました。
長くなってしまったので、実際に作成したserverlessのプロジェクトについては次の記事に書きます。
環境
- serverless 1.0.0-rc.1
- node.js 4.3.2
- npm 2.14.12
インストール
まずはserverlessのインストールです。
# npm install serverless -g
# serverless -v
1.0.0-rc.1
# serverless
Commands
* Serverless documentation: http://docs.serverless.com
* You can run commands with "serverless" or the shortcut "sls"
* Pass "--help" after any <command> for contextual help
create ................... Create new Serverless Service.
deploy ................... Deploy Service.
deploy function .......... Deploys a single function from the service
info ..................... Displays information about the service.
invoke ................... Invokes a deployed function.
logs ..................... Outputs the logs of a deployed function.
remove ................... Remove resources.
tracking ................. Enable or disable usage tracking.
Plugins
AwsCompileApigEvents, AwsCompileFunctions, AwsCompileS3Events, AwsCompileSNSEvents, AwsCompileScheduledEvents, AwsDeploy, AwsDeployFunction, AwsInfo, AwsInvoke, AwsLogs, AwsRemove, Create, Deploy, Info, Invoke, Logs, Package, Remove, Tracking
インストールすると serverless
コマンドとそのエイリアスの sls
が使えるようになります。以降はエイリアス sls
を使用します。
プロジェクトの作成
create
コマンドでプロジェクトを作成します。今回はnode.jsで作りたいので --template aws-nodejs
を指定します。ちなみに対応しているテンプレート(言語環境)は "aws-nodejs", "aws-python", "aws-java-maven" and "aws-java-gradle" の4つです。 --path
オプションを付けると指定したディレクトリの下にプロジェクトを作ってくれます。
# sls create --template aws-nodejs --path sample
Serverless: Creating new Serverless service...
Serverless: Creating the service in "/Users/bisque33/Programs/serverless/sample"
_______ __
| _ .-----.----.--.--.-----.----| .-----.-----.-----.
| |___| -__| _| | | -__| _| | -__|__ --|__ --|
|____ |_____|__| \___/|_____|__| |__|_____|_____|_____|
| | | The Serverless Application Framework
| | serverless.com, v1.0.0-rc.1
-------'
Serverless: Successfully created service with template: "aws-nodejs"
# ll sample
total 24
-rw-r--r-- 1 bisque33 staff 63B 9 10 17:31 event.json
-rw-r--r-- 1 bisque33 staff 259B 9 10 17:31 handler.js
-rw-r--r-- 1 bisque33 staff 1.7K 9 10 17:31 serverless.yml
デプロイ
デプロイにはAWSのCredentialsが必要です。まだ設定していなければ serverless/01-setup.md at master · serverless/serverless を参照して設定します。
プロジェクトを作成したい状態で deploy
コマンドを実行してみます。AWSの設定がうまくできていれば以下のように成功するはずです。CloudFormationを使ってデプロイしていることが分かりますね。 -v
オプションを付けるとCloudFormationのログも出力されます。
sls deploy
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...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
............
Serverless: Stack update finished...
Service Information
service: sample
stage: dev
region: us-east-1
endpoints:
None
functions:
sample-dev-hello: arn:aws:lambda:us-east-1:XXX:function:sample-dev-hello
実行
デプロイされたLambdaを実行するには invoke
コマンドを使います。デフォルトのFunction名はhelloです。eventパラメータにはプロジェクト作成時に作成されたevent.jsonが使われます。
# sls invoke --function hello
{
"message": "Go Serverless v1.0! Your function executed successfully!",
"event": {}
}
ログを見る
CloudWatchLogsにあるLambdaのログを見るには、 logs
コマンドを使います。コマンドも短いし aws-cli
に頭を切り替えなくて良いのは便利ですね。
# sls logs --function hello
START RequestId: f1b39691-7731-11e6-98d8-c9dbb6237eb0 Version: $LATEST
END RequestId: f1b39691-7731-11e6-98d8-c9dbb6237eb0
REPORT RequestId: f1b39691-7731-11e6-98d8-c9dbb6237eb0 Duration: 2.65 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 34 MB
まとめ
serverless 1.0系の所感としては、
- Lambdaの開発に必要な設定やコマンドが全てserverlessに集約されているので便利です。
- serverless.yamlという設定ファイルがCloudFormationのテンプレートに近い感覚で書けるのも良いです。(次の記事で軽く触れます)
- Lambdaやその他リソースが全て設定ファイルに書いたとおりにCloudFormationになるので、透明性が高く、もしserverlessが無くなってもCloudFormationが残るので全く困らないということです(JAWSの頃はこの辺りが不安でした)。
まだまだ変更が激しい時期ですが、これなら業務でも使いたいなと思いました。