|
| 1 | +python-λ |
| 2 | +======== |
| 3 | + |
| 4 | +Python-lambda is a toolset for developing and deploying *serverless* Python code in AWS Lambda. |
| 5 | + |
| 6 | +Description |
| 7 | +=========== |
| 8 | + |
| 9 | +AWS Lambda is a service that allows you to write Python, Java, or Node.js code that gets executed in response to events like http requests or files uploaded to S3. |
| 10 | + |
| 11 | +Working with Lambda is relatively easy, but the process of bundling and deploying your code is not as simple as it could be. |
| 12 | + |
| 13 | +The *Python-Lambda* library takes away the guess work of developing your Python-Lambda services by providing you a toolset to streamline the annoying parts. |
| 14 | + |
| 15 | +Requirements |
| 16 | +============ |
| 17 | + |
| 18 | +* Python 3.6 |
| 19 | +* Pip (~8.1.1) |
| 20 | +* Virtualenv (~15.0.0) |
| 21 | +* Virtualenvwrapper (~4.7.1) |
| 22 | + |
| 23 | +Getting Started |
| 24 | +=============== |
| 25 | + |
| 26 | +Begin by creating a new virtualenv and project folder. |
| 27 | + |
| 28 | +```bash |
| 29 | + $ virtualenv -p python3 pylambda |
| 30 | + $ source pylambda/bin/activate |
| 31 | +``` |
| 32 | + |
| 33 | +Next, download *Python-Lambda* using pip via Github. |
| 34 | + |
| 35 | +```bash |
| 36 | + (pylambda) $ pip install git+https://github.com/jgillard/python-lambda |
| 37 | +``` |
| 38 | +From your ``pylambda`` directory, run the following to bootstrap your project. |
| 39 | + |
| 40 | +```bash |
| 41 | + (pylambda) $ lambda init |
| 42 | +``` |
| 43 | + |
| 44 | +This will create the following files: ``event.json``, ``__init__.py``, ``service.py``, and ``config.yaml``. |
| 45 | + |
| 46 | +Next let's open ``service.py``, in here you'll find the following function: |
| 47 | + |
| 48 | +```python |
| 49 | + def handler(event, context): |
| 50 | + # Your code goes here! |
| 51 | + e = event.get('e') |
| 52 | + pi = event.get('pi') |
| 53 | + return e + pi |
| 54 | +``` |
| 55 | + |
| 56 | +This is the handler function; this is the function AWS Lambda will invoke in response to an event. You will notice that in the sample code ``e`` and ``pi`` are values in a ``dict``. AWS Lambda uses the ``event`` parameter to pass in event data to the handler. |
| 57 | + |
| 58 | +So if, for example, your function is responding to an http request, ``event`` will be the ``POST`` JSON data and if your function returns something, the contents will be in your http response payload. |
| 59 | + |
| 60 | +Next let's open the ``event.json`` file: |
| 61 | + |
| 62 | +```json |
| 63 | + { |
| 64 | + "pi": 3.14, |
| 65 | + "e": 2.718 |
| 66 | + } |
| 67 | +``` |
| 68 | + |
| 69 | +Here you'll find the values of ``e`` and ``pi`` that are being referenced in the sample code. |
| 70 | + |
| 71 | +If you now try and run: |
| 72 | + |
| 73 | +```bash |
| 74 | + (pylambda) $ lambda invoke -v |
| 75 | +``` |
| 76 | + |
| 77 | +You will get: |
| 78 | + |
| 79 | +```bash |
| 80 | + # 5.858 |
| 81 | + |
| 82 | + # execution time: 0.00000310s |
| 83 | + # function execution timeout: 15s |
| 84 | +``` |
| 85 | + |
| 86 | +As you probably put together, the ``lambda invoke`` command grabs the values stored in the ``event.json`` file and passes them to your function. |
| 87 | + |
| 88 | +The ``event.json`` file should help you develop your Lambda service locally. You can specify an alternate ``event.json`` file by passing the ``--event-file=<filename>.json`` argument to ``lambda invoke``. |
| 89 | + |
| 90 | +Testing |
| 91 | +=============== |
| 92 | + |
| 93 | +```bash |
| 94 | + (pylambda) $ python -m pytest tests/ |
| 95 | +``` |
| 96 | + |
| 97 | +TODO: `lambda test` to wrap pytest |
| 98 | + |
| 99 | +Deploying |
| 100 | +=============== |
| 101 | + |
| 102 | +Copy the template Terraform from the Usage section here: |
| 103 | + |
| 104 | +https://github.com/depop/depop-infrastructure/tree/master/modules/tf_community_modules/tf_aws_lambda#usage |
| 105 | + |
| 106 | +There is also a scheduled Lambda function available: |
| 107 | + |
| 108 | +https://github.com/depop/depop-infrastructure/tree/master/modules/tf_community_modules/tf_aws_lambda_scheduled#usage |
0 commit comments