|
| 1 | +# python-lambda-template |
| 2 | + |
| 3 | +A template repository for creating Python lambda functions. |
| 4 | + |
| 5 | +## Repo setup (delete this section and above after initial function setup) |
| 6 | + |
| 7 | +1. Rename "my_function" to the desired initial function name across the repo. (May be helpful to do a project-wide find-and-replace). |
| 8 | +2. Update Python version if needed (note: AWS lambda cannot currently support versions higher than 3.9). |
| 9 | +3. Install all dependencies with `make install` to create initial Pipfile.lock with latest dependency versions. |
| 10 | +4. Add initial function description to README and update initial required ENV variable documentation as needed. |
| 11 | +5. Update license if needed (check app-specific dependencies for licensing terms). |
| 12 | +6. Check Github repository settings: |
| 13 | + - Confirm repo branch protection settings are correct (see [dev docs](https://mitlibraries.github.io/guides/basics/github.html) for details) |
| 14 | + - Confirm that all of the following are enabled in the repo's code security and analysis settings: |
| 15 | + - Dependabot alerts |
| 16 | + - Dependabot security updates |
| 17 | + - Secret scanning |
| 18 | + |
| 19 | +# my_function |
| 20 | + |
| 21 | +Description of the function/functions. |
| 22 | + |
| 23 | +## Development |
| 24 | + |
| 25 | +- To install with dev dependencies: `make install` |
| 26 | +- To update dependencies: `make update` |
| 27 | +- To run unit tests: `make test` |
| 28 | +- To lint the repo: `make lint` |
| 29 | + |
| 30 | +## Required ENV |
| 31 | + |
| 32 | +- `WORKSPACE` = Set to `dev` for local development, this will be set to `stage` and `prod` in those environments by Terraform. |
| 33 | + |
| 34 | +## Running locally |
| 35 | + |
| 36 | +<https://docs.aws.amazon.com/lambda/latest/dg/images-test.html> |
| 37 | + |
| 38 | +- Build the container: |
| 39 | + |
| 40 | + ```bash |
| 41 | + docker build -t my_function:latest . |
| 42 | + ``` |
| 43 | + |
| 44 | +- Run the default handler for the container: |
| 45 | + |
| 46 | + ```bash |
| 47 | + docker run -p 9000:8080 my_function:latest |
| 48 | + ``` |
| 49 | + |
| 50 | +- Post to the container: |
| 51 | + |
| 52 | + ```bash |
| 53 | + curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}' |
| 54 | + ``` |
| 55 | + |
| 56 | +- Observe output: |
| 57 | + |
| 58 | + ``` |
| 59 | + "You have successfully called this lambda!" |
| 60 | + ``` |
| 61 | + |
| 62 | +## Running a different handler in the container |
| 63 | + |
| 64 | +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: |
| 65 | + |
| 66 | +```bash |
| 67 | +docker run -p 9000:8080 my_function:latest lambdas.<a-different-module>.lambda_handler |
| 68 | +``` |
0 commit comments