Docker-only local lambda development environment with docker lambda and docker aws-lambda-api-gateway-local
- Docker && Docker Compose
- Environment Variables AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY (Only need when deploy)
You should set up the aws credentials environment variables.
To set these variables on Linux or macOS, use the export command:
export AWS_ACCESS_KEY_ID=your_access_key_id
export AWS_SECRET_ACCESS_KEY=your_secret_access_key
To set these variables on Windows, use the set command:
set AWS_ACCESS_KEY_ID=your_access_key_id
set AWS_SECRET_ACCESS_KEY=your_secret_access_key
docker run -it -v ${PWD}:/var/task lambci/lambda:build-nodejs12.x sh -c "sam init"
Create a docker-compose.yaml in your inited folder:
Instead your runtime and build image, for me, it's
lambci/lambda:nodejs12.x
andlambci/lambda:build-nodejs12.x
, and define the runtime function entry for yours:hello-world/app.lambdaHandler
version: '3'
services:
runtime:
image: lambci/lambda:nodejs12.x
ports:
- "9001:9001"
volumes:
- ".:/var/task:ro,delegated"
environment:
DOCKER_LAMBDA_WATCH: 1
DOCKER_LAMBDA_STAY_OPEN: 1
command:
- hello-world/app.lambdaHandler
api-gateway:
image: owenyoung/aws-lambda-api-gateway-local
ports:
- "3000:3000"
volumes:
- ".:/var/task"
environment:
LAMBDA_ENDPOINT: "http://runtime:9001"
build:
image: lambci/lambda:build-nodejs12.x
volumes:
- ".:/var/task:delegated"
environment:
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
command:
- sh
- -c
- sam build
docker-compose up runtime api-gateway
Now, you can visit your api at: http://127.0.0.1:3000/hello (For official sam-app example, you can instead your real path), anything what you changed will trigger the auto reloading.
docker-compose run build
First time for deploy:
docker-compose run build sh -c "sam build && sam deploy --guided"
docker-compose run build && docker-compose run build sh -c "sam build && sam deploy"
For example:
.PHONY: start build deploy init-deploy api-sh runtime-sh
start:
docker-compose up api-gateway runtime
build:
docker-compose run build
init-deploy:
docker-compose run build sh -c "sam build && sam deploy --guided"
deploy:
docker-compose run build sh -c "sam build && sam deploy"
api-sh:
docker-compose exec api-gateway /bin/sh
runtime-sh:
docker-compose exec runtime /bin/sh