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.
- Install all dependencies with
make install. - 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)
- 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 preview a list of available Makefile commands:
make help - To create a Python virtual environment and install with dev dependencies:
make install - To update dependencies:
make update - To run unit tests:
make test - To lint the repo:
make lint
The Makefile includes several SAM commands as working examples for local Lambda testing. The included ping/pong endpoints are there to verify SAM is wired up correctly — adapt or replace them with function-specific events as the function evolves.
Ensure that AWS SAM CLI is installed: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html.
All following actions and commands should be performed from the root of the project (i.e. same directory as the Dockerfile).
1- Create a JSON file for SAM that has environment variables for the container
- copy
tests/sam/env.json.templatetotests/sam/env.json(which is git ignored) - fill in missing sensitive env vars
NOTE: AWS credentials are automatically passed from the terminal context that runs make sam-http-run or make sam-invoke; they do not need to be explicitly set as env vars.
2- Build Docker image:
make sam-buildUseful when the Lambda will sit behind an ALB, Function URL, or API Gateway. This starts a local HTTP server that accepts requests and returns responses similar to those environments.
1- Ensure any required AWS credentials are set in terminal, and any other env vars in tests/sam/env.json are up-to-date.
2- Run HTTP server:
make sam-http-runThis starts a server at http://localhost:3000. Requests must include a path, e.g. /myfunction, but are arbitrary insofar as the lambda does not utilize them in the request payload.
3- In another terminal, perform an HTTP request via another Makefile command:
make sam-http-pingResponse should have an HTTP status of 200 and respond with:
{
"response": "pong"
}Useful when the Lambda is invoked directly with an event payload (e.g. by a scheduled rule, another service, etc.) rather than via HTTP. You do not need to first start an HTTP server for this.
make sam-invokeThis sends a default event payload to the Lambda. To customize the event payload, pipe JSON directly to sam local invoke:
echo '{"action": "ping"}' | sam local invoke -e -Response:
{"statusCode": 200, "statusDescription": "200 OK", "headers": {"Content-Type": "application/json"}, "isBase64Encoded": false, "body": "{\"response\": \"pong\"}"}
Note: the lambda is still returning a dictionary that would work for an HTTP response, but when invoked directly it's just a dictionary with the relevant information.
When running a Lambda via SAM, it attempts to parse and setup AWS credentials just like a real Lambda would establish them. Depending on how you setup AWS credentials on your host machine, if they are stale or invalid, you may encounter this error when making your first requests of the Lambda.
Solution: Stop the SAM container, refresh AWS credentials, and restart it.
SENTRY_DSN=### If set to a valid Sentry DSN, enables Sentry exception monitoring. This is not needed for local development.
WORKSPACE=### Set to `dev` for local development, this will be set to `stage` and `prod` in those environments by Terraform.Delete this section if it isn't applicable to the PR.
<OPTIONAL_ENV>=### Description for optional environment variable