A template repository for creating Python lambda functions.
- Rename "my_function" to the desired initial function name across the repo. (May be helpful to do a project-wide find-and-replace).
- Update Python version if needed (note: AWS lambda cannot currently support versions higher than 3.9).
- Install all dependencies with
make install
to create initial Pipfile.lock with latest dependency versions. - Add initial function description to README and update initial required ENV variable documentation as needed.
- Update license if needed (check app-specific dependencies for licensing terms).
- Check Github repository settings:
- Confirm repo branch protection settings are correct (see dev docs for details)
- Confirm that all of the following are enabled in the repo's code security and analysis settings:
- Dependabot alerts
- Dependabot security updates
- Secret scanning
- Create a Sentry project for the app if needed (we want this for most apps):
- Send initial exceptions to Sentry project for dev, stage, and prod environments to create them.
- Create an alert for the prod environment only, with notifications sent to the appropriate team(s).
- If not using Sentry, delete Sentry configuration from my_function.py and test_my_function_.py, and remove sentry_sdk from project dependencies.
Description of the function/functions.
- To install with dev dependencies:
make install
- To update dependencies:
make update
- To run unit tests:
make test
- To lint the repo:
make lint
SENTRY_DSN
= If set to a valid Sentry DSN, enables Sentry exception monitoring. This is not needed for local development.WORKSPACE
= Set todev
for local development, this will be set tostage
andprod
in those environments by Terraform.
https://docs.aws.amazon.com/lambda/latest/dg/images-test.html
-
Build the container:
docker build -t my_function:latest .
-
Run the default handler for the container:
docker run -e WORKSPACE=dev -p 9000:8080 my_function:latest
-
Post to the container:
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
-
Observe output:
"You have successfully called this lambda!"
If this repo contains multiple lambda functions, you can call any handler you copy into the container (see Dockerfile) by name as part of the docker run
command:
docker run -p 9000:8080 my_function:latest lambdas.<a-different-module>.lambda_handler