Help us understand the problem. What is going on with this article?

aws-sam-cliを使ってlocalでLambdaを動かしてみる

More than 1 year has passed since last update.

aws-sam-cliをインストールする

https://github.com/awslabs/aws-sam-cli#installation

Docker for MacとPython 2.7 or 3.6が必要。

Macのデフォルトでは2.7系だったので、一応最新版を入れる

pyenv install 3.6.5
pyenv global 3.6.5
pip install aws-sam-cli

※ドキュメントの方では—userをつけているが、これをつけると~/.localフォルダにsamがインストールされてしまうので注意!普段pip関連使わないからハマってしまったー。

sam init

sam init --runtime go

サンプルを作ってくれる

こんな感じのファイルが出来る。goじゃなくても別の言語してして作ってもOK

$ tree
.
└── sam-app
    ├── Makefile
    ├── README.md
    ├── hello-world
    │   ├── main.go
    │   └── main_test.go
    └── template.yaml

2 directories, 5 files

localで動かしてみる

build

go get github.com/aws/aws-lambda-go/events
go get github.com/aws/aws-lambda-go/lambda
make build

localでLambdaを動かす

# Lambdaに渡す用のイベントファイルの作成
sam local generate-event api > event_file.json

# Lambdaの実行
sam local invoke HelloWorldFunction --event event_file.json

localでAPI Gatewayを使って動かす

# 起動
sam local start-api

# 呼ぶ
curl http://127.0.0.1:3000/hello

所管

デプロイして確認すると、結構めんどくさいから、ローカルでいろいろと試せるのは便利!

参考

https://qiita.com/hayao_k/items/841026f9675d163b58d5

mapyo
プログラマー修行中の身です。
http://d.hatena.ne.jp/mapyo/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした