Here's a basic breakdown of the folder structure for this repo:
.
├── README.md <-- This instructions file
├── podbooster <-- Source code for a lambda function
│ ├── index.js <-- Lambda function code
│ ├── package.json <-- NodeJS dependencies
│ └── tests <-- Unit tests
│ └── unit
│ └── test_handler.js
└── template.yaml <-- SAM template
- AWS CLI already configured with Administrator permission
- NodeJS 8.10+ installed
- Docker installed
AWS Lambda requires a flat folder with the application as well as its dependencies in a node_modules folder. When you make changes to your source code or dependency manifest, run the following command to build your project local testing and deployment:
sam build --use-container
By default, this command writes built artifacts to .aws-sam/build
folder.
Invoking function locally through local API Gateway
sam local start-api
If the previous command ran successfully you should now be able to hit the following local endpoint to invoke your function http://localhost:3000/podbooster
We use mocha
for testing our code and it is already added in package.json
under scripts
, so that we can simply run the following command to run our tests:
cd podbooster
npm install
npm run test
Because differences in machine architecture, the files to be uploaded to lambda (like node_modules) must all be built in an Amazon Linux 2 Docker container so that the Lame bindings work in lambda.
- build the docker image locally:
docker build -f Dockerfile . -t scprdev/lambda-podbooster
- run the image:
docker run -v path/to/this/repo/podbooster:/working -it scprdev/lambda-podbooster:latest
- note in the previous step that the path is to the
podbooster
directory in this current repo, not the repo root. We're mapping that to the '/working` directory in the docker container. - the previous
docker run
command should have created a newlambda.zip
file in the repo root (check the timestamp to verify) - run
./publish.sh
to publish thatlambda.zip
to AWS lambda