|
| 1 | +<div align="center"> |
| 2 | + <p> |
| 3 | + <img src="https://s3.amazonaws.com/nf-assets/python-lambda.svg" width="350" height="328" alt="python-lambda logo" /> |
| 4 | + </p> |
| 5 | + <p align="center"> |
| 6 | + <img src="https://img.shields.io/pypi/v/python-lambda.svg" alt="pypi" /> |
| 7 | + <a href="https://pypi.org/project/python-lambda/"><img src="https://img.shields.io/pypi/dm/python-lambda.svg" alt="pypi"></a> |
| 8 | + <a href="https://pypi.python.org/pypi/python-lambda/"><img src="https://img.shields.io/pypi/pyversions/python-lambda.svg" /></a> |
| 9 | + </p> |
| 10 | +</div> |
| 11 | + |
| 12 | +Python-lambda is a toolset for developing and deploying *serverless* Python code in AWS Lambda. |
| 13 | + |
| 14 | +# A call for contributors |
| 15 | +With python-lambda and pytube both continuing to gain momentum, I'm calling for |
| 16 | +contributors to help build out new features, review pull requests, fix bugs, |
| 17 | +and maintain overall code quality. If you're interested, please email me at |
| 18 | +nficano[at]gmail.com. |
| 19 | + |
| 20 | +# Description |
| 21 | + |
| 22 | +AWS Lambda is a service that allows you to write Python, Java, or Node.js code |
| 23 | +that gets executed in response to events like http requests or files uploaded |
| 24 | +to S3. |
| 25 | + |
| 26 | +Working with Lambda is relatively easy, but the process of bundling and |
| 27 | +deploying your code is not as simple as it could be. |
| 28 | + |
| 29 | +The *Python-Lambda* library takes away the guess work of developing your |
| 30 | +Python-Lambda services by providing you a toolset to streamline the annoying |
| 31 | +parts. |
| 32 | + |
| 33 | +# Requirements |
| 34 | + |
| 35 | +* Python 2.7 & 3.6 (At the time of writing this, AWS Lambda only supports Python 2.7/3.6). |
| 36 | +* Pip (~8.1.1) |
| 37 | +* Virtualenv (~15.0.0) |
| 38 | +* Virtualenvwrapper (~4.7.1) |
| 39 | + |
| 40 | +# Getting Started |
| 41 | + |
| 42 | +First, you must create an IAM Role on your AWS account called |
| 43 | +``lambda_basic_execution`` with the ``LambdaBasicExecution`` policy attached. |
| 44 | + |
| 45 | +On your computer, create a new virtualenv and project folder. |
| 46 | + |
| 47 | +```bash |
| 48 | +$ mkvirtualenv pylambda |
| 49 | +(pylambda) $ mkdir pylambda |
| 50 | +``` |
| 51 | + |
| 52 | +Next, download *Python-Lambda* using pip via pypi. |
| 53 | + |
| 54 | +```bash |
| 55 | +(pylambda) $ pip install python-lambda |
| 56 | +``` |
| 57 | + |
| 58 | +From your ``pylambda`` directory, run the following to bootstrap your project. |
| 59 | + |
| 60 | +```bash |
| 61 | +(pylambda) $ lambda init |
| 62 | +``` |
| 63 | + |
| 64 | +This will create the following files: ``event.json``, ``__init__.py``, |
| 65 | +``service.py``, and ``config.yaml``. |
| 66 | + |
| 67 | +Let's begin by opening ``config.yaml`` in the text editor of your choice. For |
| 68 | +the purpose of this tutorial, the only required information is |
| 69 | +``aws_access_key_id`` and ``aws_secret_access_key``. You can find these by |
| 70 | +logging into the AWS management console. |
| 71 | + |
| 72 | +Next let's open ``service.py``, in here you'll find the following function: |
| 73 | + |
| 74 | +```python |
| 75 | +def handler(event, context): |
| 76 | + # Your code goes here! |
| 77 | + e = event.get('e') |
| 78 | + pi = event.get('pi') |
| 79 | + return e + pi |
| 80 | +``` |
| 81 | + |
| 82 | +This is the handler function; this is the function AWS Lambda will invoke in |
| 83 | +response to an event. You will notice that in the sample code ``e`` and ``pi`` |
| 84 | +are values in a ``dict``. AWS Lambda uses the ``event`` parameter to pass in |
| 85 | +event data to the handler. |
| 86 | + |
| 87 | +So if, for example, your function is responding to an http request, ``event`` |
| 88 | +will be the ``POST`` JSON data and if your function returns something, the |
| 89 | +contents will be in your http response payload. |
| 90 | + |
| 91 | +Next let's open the ``event.json`` file: |
| 92 | + |
| 93 | +```json |
| 94 | +{ |
| 95 | + "pi": 3.14, |
| 96 | + "e": 2.718 |
| 97 | +} |
| 98 | +``` |
| 99 | +Here you'll find the values of ``e`` and ``pi`` that are being referenced in |
| 100 | +the sample code. |
| 101 | + |
| 102 | +If you now try and run: |
| 103 | + |
| 104 | +```bash |
| 105 | +(pylambda) $ lambda invoke -v |
| 106 | +``` |
| 107 | + |
| 108 | +You will get: |
| 109 | +```bash |
| 110 | +# 5.858 |
| 111 | +# execution time: 0.00000310s |
| 112 | +# function execution timeout: 15s |
| 113 | +``` |
| 114 | + |
| 115 | +As you probably put together, the ``lambda invoke`` command grabs the values |
| 116 | +stored in the ``event.json`` file and passes them to your function. |
| 117 | + |
| 118 | +The ``event.json`` file should help you develop your Lambda service locally. |
| 119 | +You can specify an alternate ``event.json`` file by passing the |
| 120 | +``--event-file=<filename>.json`` argument to ``lambda invoke``. |
| 121 | + |
| 122 | +When you're ready to deploy your code to Lambda simply run: |
| 123 | + |
| 124 | +```bash |
| 125 | +(pylambda) $ lambda deploy |
| 126 | +``` |
| 127 | + |
| 128 | +The deploy script will evaluate your virtualenv and identify your project |
| 129 | +dependencies. It will package these up along with your handler function to a |
| 130 | +zip file that it then uploads to AWS Lambda. |
| 131 | + |
| 132 | +You can now log into the |
| 133 | +[AWS Lambda management console](https://console.aws.amazon.com/lambda/) to |
| 134 | +verify the code deployed successfully. |
| 135 | + |
| 136 | +### Wiring to an API endpoint |
| 137 | + |
| 138 | +If you're looking to develop a simple microservice you can easily wire your |
| 139 | +function up to an http endpoint. |
| 140 | + |
| 141 | +Begin by navigating to your [AWS Lambda management console](https://console.aws.amazon.com/lambda/) and |
| 142 | +clicking on your function. Click the API Endpoints tab and click "Add API endpoint". |
| 143 | + |
| 144 | +Under API endpoint type select "API Gateway". |
| 145 | + |
| 146 | +Next change Method to ``POST`` and Security to "Open" and click submit (NOTE: |
| 147 | +you should secure this for use in production, open security is used for demo |
| 148 | +purposes). |
| 149 | + |
| 150 | +At last you need to change the return value of the function to comply with the |
| 151 | +standard defined for the API Gateway endpoint, the function should now look |
| 152 | +like this: |
| 153 | + |
| 154 | +``` |
| 155 | +def handler(event, context): |
| 156 | + # Your code goes here! |
| 157 | + e = event.get('e') |
| 158 | + pi = event.get('pi') |
| 159 | + return { |
| 160 | + "statusCode": 200, |
| 161 | + "headers": { "Content-Type": "application/json"}, |
| 162 | + "body": e + pi |
| 163 | + } |
| 164 | +``` |
| 165 | + |
| 166 | +Now try and run: |
| 167 | + |
| 168 | +```bash |
| 169 | +$ curl --header "Content-Type:application/json" \ |
| 170 | + --request POST \ |
| 171 | + --data '{"pi": 3.14, "e": 2.718}' \ |
| 172 | + https://<API endpoint URL> |
| 173 | +# 5.8580000000000005 |
| 174 | +``` |
| 175 | + |
| 176 | +### Environment Variables |
| 177 | +Lambda functions support environment variables. In order to set environment |
| 178 | +variables for your deployed code to use, you can configure them in |
| 179 | +``config.yaml``. To load the value for the environment variable at the time of |
| 180 | +deployment (instead of hard coding them in your configuration file), you can |
| 181 | +use local environment values (see 'env3' in example code below). |
| 182 | + |
| 183 | +```yaml |
| 184 | +environment_variables: |
| 185 | + env1: foo |
| 186 | + env2: baz |
| 187 | + env3: ${LOCAL_ENVIRONMENT_VARIABLE_NAME} |
| 188 | +``` |
| 189 | +
|
| 190 | +This would create environment variables in the lambda instance upon deploy. If |
| 191 | +your functions don't need environment variables, simply leave this section out |
| 192 | +of your config. |
| 193 | +
|
| 194 | +### Uploading to S3 You may find that you do not need the toolkit to fully |
| 195 | +deploy your Lambda or that your code bundle is too large to upload via the API. |
| 196 | +You can use the ``upload`` command to send the bundle to an S3 bucket of your |
| 197 | +choosing. Before doing this, you will need to set the following variables in |
| 198 | +``config.yaml``: |
| 199 | +
|
| 200 | +```yaml |
| 201 | +role: basic_s3_upload |
| 202 | +bucket_name: 'example-bucket' |
| 203 | +s3_key_prefix: 'path/to/file/' |
| 204 | +``` |
| 205 | +Your role must have ``s3:PutObject`` permission on the bucket/key that you |
| 206 | +specify for the upload to work properly. Once you have that set, you can |
| 207 | +execute ``lambda upload`` to initiate the transfer. |
| 208 | +
|
| 209 | +### Deploying via S3 |
| 210 | +You can also choose to use S3 as your source for Lambda deployments. This can |
| 211 | +be done by issuing ``lambda deploy_s3`` with the same variables/AWS permissions |
| 212 | +you'd set for executing the ``upload`` command. |
| 213 | +
|
| 214 | +### Development |
| 215 | +Development of "python-lambda" is facilitated exclusively on GitHub. |
| 216 | +Contributions in the form of patches, tests and feature creation and/or |
| 217 | +requests are very welcome and highly encouraged. Please open an issue if this |
| 218 | +tool does not function as you'd expect. |
| 219 | +
|
| 220 | +
|
| 221 | +### How to release updates If this is the first time you're releasing to pypi, |
| 222 | +you'll need to run: ``pip install -r tests/dev_requirements.txt``. |
| 223 | +
|
| 224 | +Once complete, execute the following commands: |
| 225 | +
|
| 226 | +```bash |
| 227 | +git checkout master |
| 228 | + |
| 229 | +# Increment the version number and tag the release. |
| 230 | +bumpversion [major|minor|patch] |
| 231 | + |
| 232 | +# Upload the distribution to PyPi |
| 233 | +python setup.py sdist bdist_wheel upload |
| 234 | + |
| 235 | +# Since master often contains work-in-progress changes, increment the version |
| 236 | +# to a patch release to prevent inaccurate attribution. |
| 237 | +bumpversion --no-tag patch |
| 238 | + |
| 239 | +git push origin master --tags |
| 240 | +``` |
0 commit comments