インストール
npmでインストールすることができます
$ npm install -g aws-sam-local
プロジェクト作成
適当なディレクトリを作成しSAMの公式プロジェクトのサンプルを参考に
index.jsとtemplate.yml を作成していきます
template.yml
AWSTemplateFormatVersion : '2017-09-20'
Description: A hello world application.
Resources:
HelloWorld:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs6.10
index.js
'use strict';
console.log('Loading function');
exports.handler = (event, context, callback) => {
callback(null, 'Hello World!');
};
実行
index.jsとtemplate.ymlが作成できたら実行してみます
※事前にDockerを起動しておく必要があります
$ echo '{}' | sam local invoke HelloWorld
2017/09/20 18:39:12 Successfully parsed template.yaml
2017/09/20 18:39:12 Connected to Docker 1.30
2017/09/20 18:39:12 Fetching lambci/lambda:nodejs6.10 image for nodejs6.10 runtime...
nodejs6.10: Pulling from lambci/lambda
Digest: sha256:b9a57b98dcfe226cac3fb0b8329594eefb62ba7089fa27cf8ac968e5736cc04a
Status: Image is up to date for lambci/lambda:nodejs6.10
2017/09/20 18:39:15 Reading invoke payload from stdin (you can also pass it from file with --event)
2017/09/20 18:39:15 Invoking index.handler (nodejs6.10)
START RequestId: feb31e2a-3748-1152-e1c1-85725b4021a7 Version: $LATEST
2017-09-20T09:39:16.722Z feb31e2a-3748-1152-e1c1-85725b4021a7 Loading function
END RequestId: feb31e2a-3748-1152-e1c1-85725b4021a7
REPORT RequestId: feb31e2a-3748-1152-e1c1-85725b4021a7 Duration: 9.66 ms Billed Duration: 0 ms Memory Size: 0 MB Max Memory Used: 29 MB
"Hello World!"
1回目の実行のときは Docker イメージの DLを行ってから実行されます