なぜやる
Lambda の自動テストのため、Background に実行できるAPI Gatewayが必要
どうやる
AWS SAM の Docker Image 作れば、Background で sam local start-api
のコマンドでAPI Gateway 起動できる
どうやった
Dockerfile
FROM python:3.7-alpine
RUN apk add --no-cache --virtual build-deps build-base gcc && \
pip install aws-sam-cli && \
apk del build-deps
WORKDIR /var/opt
EXPOSE 3000
ENTRYPOINT ["/usr/local/bin/sam"]
$ docker build -t aws-sam-cli .
Successfully built 281181c0ac11
Successfully tagged aws-sam-cli:latest
どう使う
shell
$ git clone https://github.com/wwalpha/docker-aws-sam-cli.git
$ cd docker-aws-sam-cli
$ docker run -itd --rm \
-v /var/run/docker.sock:/var/run/docker.sock \
-v "$(PWD)/example":/var/opt \
-p "3000:3000" \
wwalpha/aws-sam-cli local start-api --docker-volume-basedir "$(PWD)/example" --host 0.0.0.0
データベースなど他のネットワークに接続したい場合、最後に --docker-network xxxxx
が設定できます。
使ってみる
Shell_OR_PowerShell
$ curl http://localhost:3000
Hello, World!
参考資料
- GitHub (Examples)
- Docker Hub (Image)