Package: python-lambda2
Python-lambda is a toolset for developing and deployingserverlessPython code in AWS Lambda.DescriptionAWS 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.Working with Lambda is relatively easy, but the process of bundling and deploying your code is not as simple as it could be.ThePython-Lambda2library takes away the guess work of developing your Python-Lambda services by providing you a toolset to streamline the annoying parts.RequirementsPython 2.7 & 3.6 (At the time of writing this, AWS Lambda only supports Python 2.7/3.6).Pip (~8.1.1)Virtualenv (~15.0.0)Virtualenvwrapper (~4.7.1)Getting StartedFirst, you must create an IAM Role on your AWS account calledlambda_basic_executionwith theLambdaBasicExecutionpolicy attached.On your computer, create a new virtualenv and project folder.$mkvirtualenvpylambda(pylambda)$mkdirpylambdaNext, downloadPython-Lambdausing pip via pypi.(pylambda)$pipinstallpython-lambda2From yourpylambdadirectory, run the following to bootstrap your project.(pylambda)$lambdainitThis will create the following files:event.json,__init__.py,service.py, andconfig.yaml.Let’s begin by openingconfig.yamlin the text editor of your choice. For the purpose of this tutorial, the only required information isaws_access_key_idandaws_secret_access_key. You can find these by logging into the AWS management console.Next let’s openservice.py, in here you’ll find the following function:defhandler(event,context):# Your code goes here!e=event.get('e')pi=event.get('pi')returne+piThis is the handler function; this is the function AWS Lambda will invoke in response to an event. You will notice that in the sample codeeandpiare values in adict. AWS Lambda uses theeventparameter to pass in event data to the handler.So if, for example, your function is responding to an http request,eventwill be thePOSTJSON data and if your function returns something, the contents will be in your http response payload.Next let’s open theevent.jsonfile:{"pi":3.14,"e":2.718}Here you’ll find the values ofeandpithat are being referenced in the sample code.If you now try and run:(pylambda)$lambdainvoke-vYou will get:# 5.858# execution time: 0.00000310s
# function execution timeout: 15sAs you probably put together, thelambda invokecommand grabs the values stored in theevent.jsonfile and passes them to your function.Theevent.jsonfile should help you develop your Lambda service locally. You can specify an alternateevent.jsonfile by passing the--event-file=<filename>.jsonargument tolambda invoke.When you’re ready to deploy your code to Lambda simply run:(pylambda)$lambdadeploy[--preserve-vpc][--preserve-variables]The deploy script will evaluate your virtualenv and identify your project dependencies. It will package these up along with your handler function to a zip file that it then uploads to AWS Lambda.You can now log into theAWS Lambda management consoleto verify the code deployed successfully.Wiring to an API endpointIf you’re looking to develop a simple microservice you can easily wire your function up to an http endpoint.Begin by navigating to yourAWS Lambda management consoleand clicking on your function. Click the API Endpoints tab and click “Add API endpoint”.Under API endpoint type select “API Gateway”.Next change Method toPOSTand Security to “Open” and click submit (NOTE: you should secure this for use in production, open security is used for demo purposes).At last you need to change the return value of the function to comply with the standard defined for the API Gateway endpoint, the function should now look like this:defhandler(event,context):# Your code goes here!e=event.get('e')pi=event.get('pi')return{"statusCode":200,"headers":{"Content-Type":"application/json"},"body":e+pi}Now try and run:$curl--header"Content-Type:application/json"\--requestPOST\--data'{"pi": 3.14, "e": 2.718}'\https://<APIendpointURL># 5.8580000000000005Environment VariablesLambda functions support environment variables. In order to set environment variables for your deployed code to use, you can configure them inconfig.yaml.  To load the
value for the environment variable at the time of deployment (instead of hard coding them in your configuration file), you can use local environment values (see ‘env3’ in example code below).environment_variables:env1:fooenv2:bazenv3:${LOCAL_ENVIRONMENT_VARIABLE_NAME}This would create environment variables in the lambda instance upon deploy. If your functions don’t need environment variables, simply leave this section out of your config.Uploading to S3You may find that you do not need the toolkit to fully deploy your Lambda or that your code bundle is too large to upload via the API.  You can use theuploadcommand to send the bundle to an S3 bucket of your choosing.
Before doing this, you will need to set the following variables inconfig.yaml:role:basic_s3_uploadbucket_name:'example-bucket's3_key_prefix:'path/to/file/'Your role must haves3:PutObjectpermission on the bucket/key that you specify for the upload to work properly. Once you have that set, you can executelambda uploadto initiate the transfer.Deploying via S3You can also choose to use S3 as your source for Lambda deployments.  This can be done by issuinglambda deploy_s3with the same variables/AWS permissions you’d set for executing theuploadcommand.DevelopmentDevelopment of “python-lambda2” is facilitated exclusively on GitHub. Contributions in the form of patches, tests and feature creation and/or requests are very welcome and highly encouraged. Please open an issue if this tool does not function as you’d expect.How to release updatesIf this is the first time you’re releasing to pypi, you’ll need to run:pip install-rtests/dev_requirements.txt.Once complete, execute the following commands:gitcheckoutmaster# Increment the version number and tag the release.bumpversion[major|minor|patch]# Upload the distribution to PyPipythonsetup.pysdistbdist_wheelupload# Since master often contains work-in-progress changes, increment the version
# to a patch release to prevent inaccurate attribution.bumpversion--no-tagpatchgitpushoriginmaster--tags