2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS SAM Local で関数をローカルからデプロイする

Posted at

AWS SAM Local とは?

AWS Lambda関数をローカルで実行しテストができるツールです。
S3, Kinesis, DynamoDB, Cloudwatch Scheduled Event, Cloudtrail,API Gatewayなどの関数からの呼び出しにも対応しています。

事前準備

Dokcerのインストール

Macの場合こちら:
Install Docker for Mac | Docker Documentation

AWS CLIのインストール

Macの場合こちら:
macOS で AWS Command Line Interface をインストールする - AWS Command Line Interface

SAM Localのインストール

Macの場合こちら:
$ brew install aws-sam-cli

SAM LocalでHello Worldしてデプロイ

用意するもの

実行したいコード

index.js
exports.handler = (event, context, callback) => {
    callback(null, "Hello " + event.body);
}

AWS SAM テンプレートファイル

template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS Lambda sample.
Resources:
  HelloWorld:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs8.10

sam local実行時に入力するイベントファイル

event.json
{"body": "World"}

構文チェック

$ sam validate
で行える。

/Users/hoge/sam-local-test/template.yaml is a valid SAM Template

など、~ is a valid SAM Templateが出ればOK.

実行

$ sam local invoke HelloWorld -e event.json

2019-01-04 13:09:41 Invoking index.handler (nodejs8.10)

Fetching lambci/lambda:nodejs8.10 Docker container image......
2019-01-04 13:09:45 Mounting /Users/hoge/sam-local-test as /var/task:ro inside runtime container
START RequestId: d020b83c-acb1-163c-ecdf-f1f05b72f463 Version: $LATEST
2019-01-04T04:09:46.490Z	d020b83c-acb1-163c-ecdf-f1f05b72f463	Loading function
END RequestId: d020b83c-acb1-163c-ecdf-f1f05b72f463
REPORT RequestId: d020b83c-acb1-163c-ecdf-f1f05b72f463	Duration: 8.69 ms	Billed Duration: 100 ms	Memory Size: 128 MB	Max Memory Used: 30 MB

"Hello World"

実際にHello Worldが出力されました。

デプロイ

templateを置くs3のバケットを作成する。 
注意: この際デプロイするLambdaと同一リージョンにs3のバケットを置くこと。また、s3のバケットは一意でなければならない。
$ aws s3 mb s3://[バケット名] --region [リージョン] --profile [プロファイル]

パッケージングする。
$ sam package --template-file template.yaml --s3-bucket [上で作ったバケット名] --output-template-file package.yaml --profile [プロファイル]

$ sam deploy --template-file package.yaml --stack-name sam-test-helloworld --capabilities CAPABILITY_IAM --profile personal --region [リージョン]

Waiting for changeset to be created..
Waiting for stack create/update to complete
Successfully created/updated stack - sam-test-helloworld

デプロイ成功。

確認

コンソールから確認します。
スクリーンショット 2019-01-04 13.52.29.png
できているようです。

右上の[テスト]からテストイベントを設定します。

スクリーンショット 2019-01-04 13.38.07.png

上のように設定して[保存]し、テストを実行します。

スクリーンショット 2019-01-04 13.57.00.png

上記のように"Hello world"が出力されていれば成功です。

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?