|
1 | 1 | # python-lambda-local-server
|
2 |
| -A thin wrapper around python-lambda-local package to run as a server instead cli. This package is designed to be run by Docker. |
| 2 | + |
| 3 | +A Docker image to help with development of a local AWS lambda Python functions. |
| 4 | +Based on the [lambci/lambda](https://hub.docker.com/r/lambci/lambda/) image. |
| 5 | +Created, because I haven't found an easy way to run lambda function locally. |
| 6 | + |
| 7 | +Features: |
| 8 | +* Automatically installs packages from requirements.txt during startup |
| 9 | +* Mirrored AWS Lambda environment thanks to lambci images |
| 10 | +* Code is reloaded for each submitted event |
| 11 | +* Available simple Web UI for better workflow |
| 12 | + |
| 13 | +# Usage |
| 14 | + |
| 15 | +Go to directory with your lambda Python code. Next, run this command: |
| 16 | + |
| 17 | +```bash |
| 18 | +docker run -it \ |
| 19 | + -p 8080:8080 \ |
| 20 | + -v lambda-packages-cache:/packages/ \ |
| 21 | + -v $PWD:/var/task/ \ |
| 22 | + valian/python-lambda-local-server |
| 23 | +``` |
| 24 | + |
| 25 | +It should automatically install packages from your requirements.txt and start web sever written in aiohttp. |
| 26 | +To see web UI, head to http://localhost:8080. You should see a simple web UI for testing your lambda function. |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +Assuming that you have your lambda entrypoint in `lambda.py` and handler is named `handler`, you should |
| 31 | +type it into desired inputs. Next, add JSON body that should be passed to the function as an event, and press 'Submit event'. |
| 32 | +Result and logs should be visible on the right panel after a moment. |
| 33 | + |
| 34 | + |
| 35 | +# Example |
| 36 | + |
| 37 | +First, clone this repository: |
| 38 | + |
| 39 | +```bash |
| 40 | +git clone https://github.com/Valian/python-lambda-local-server |
| 41 | +cd python-lambda-local-server |
| 42 | +``` |
| 43 | + |
| 44 | +Next, start server with a proper volume mounted: |
| 45 | +```bash |
| 46 | +docker run -it \ |
| 47 | + -p 8080:8080 \ |
| 48 | + -v $PWD/example:/var/task/ \ |
| 49 | + valian/python-lambda-local-server |
| 50 | +``` |
| 51 | + |
| 52 | +## Web UI usage |
| 53 | + |
| 54 | +Go to http://localhost:8080. You should see something like this: |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +Specify file, handler and event and click `Submit event`. |
| 59 | +It invokes handler and prints execution logs and return value. |
| 60 | + |
| 61 | + |
| 62 | +Api gateway tab automatically formats event to an AWS API Gateway format: |
| 63 | + |
| 64 | + |
| 65 | +## Console example |
| 66 | + |
| 67 | +You can of course use `curl` to submit events. Start sever and type: |
| 68 | + |
| 69 | +```bash |
| 70 | +curl -XPOST localhost:8080 -d '{"event": {"url": "https://example.com"}, "file": "handler.handler"}' |
| 71 | + |
| 72 | +{ |
| 73 | + "stdout": { |
| 74 | + "statusCode": 200, |
| 75 | + "url": "https://example.com/" |
| 76 | + }, |
| 77 | + "stderr": "START RequestId: b1891caf-c22b-4ce6-8639-e74862adae30 Version: $LATEST\nINFO:root:Starting request\nINFO:root:Request done, status code: 200\nEND RequestId: b1891caf-c22b-4ce6-8639-e74862adae30\nREPORT RequestId: b1891caf-c22b-4ce6-8639-e74862adae30 Duration: 803 ms Billed Duration: 900 ms Memory Size: 1536 MB Max Memory Used: 23 MB\n" |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +# TODO |
| 82 | +* [ ] Add support for `Serverless.yml` file |
| 83 | +* [ ] Cleanup code, better error codes |
| 84 | +* [ ] Support for chaining lambda calls either through AWS SDK or API endpoint |
| 85 | + |
| 86 | +# License |
| 87 | + |
| 88 | +MIT |
0 commit comments