Restful webhook using Lambda + API Gateway— PART 1
By: Christopher Lagali
Tags: AWS, AWS Lambda, Api Gateway, Flask Restful

Lambda

Lambda essentially is a way to run your code on demand without paying or setting up any servers. So all the prerequisites that your code needs (apart from what is inbuilt) needs to be packaged with your code.

The packages we will be using are flask and flask-lambda.

Its a good idea to develop your code in a virtual environment and have all its packages installed in there. Makes your code cleaner and lighter.

To start with create a virtual environment:

> virtualenv — python=/usr/bin/python3.6 python36

After you have created a virtual environment; in this case named python36 you need to activate it.

> source python36/bin/activate

Notice the name of your virtual environment in the left most corner; which means you are now inside your environment.

Let the installation begin!

We start by installing flask-lambda using pip.

With flask-lambda installed you will not need to install Flask explicitly.

Lets now create the actual lambda code:

The code above is to not just say hello but is to send out a json object with some Transaction related information.

Now run this code to test it in your environment as flask-lambda will let you do so as if you were executing it on Lambda itself.

Once done; come out of the environment.

> deactivate

Lets start zipping your code and your dependencies. For this, head over to the location where flask-lambda package is installed. This would essentially be the python3.6 directory in your virtual environment base location.

In this case it will be:

cd python36/lib/python3.6/site-packages/

use the following to zip the contents of the folder:

zip -r /home/ec2-user/lambda/my-deployment-package.zip .

Now, move this zip folder to the folder where your lambda_function python code exists and add your lambda_function.py file to the zip.

The results of your zip should look something similar:

Zipping all dependency packages together with your code make its easier for the lambda function to load and execute them when referenced in your py code.

This is my resultant folder structure:

We were repackaging a missing dependency in the deployment package when this screenshot was taken.

Armed with the zipped contents; head over to the Lambda function in the AWS console and upload the code as a zip:

The zipped package might take a few minutes to upload based on its size.

Modifying Runtime Settings:

The Handler settings gives Lambda an idea as to which method it needs to execute once it is invoked. In our case lambda_function.app is suitable as this refers to our Flask application name.

Local Testing

Once done go ahead and test it by configuring a test event.

API Gateway test template will give you a skeleton of the test JSON object that will emulate a request if sent from an external API client.

Go ahead and add the url resource name you added in the Flak app. In this case it is /hello and httpMethod will be GET.

Save this Test Template and execute it.

The response Body seen here is the JSON object that was built and returned by your Flask app when it gets triggered.

Well!! With your business Logic in place its time to spruce up the front end.

Errors and Resolution:

Since we are packaging code; chances of missing a few dependencies is highly likely. This is evident by errors of this nature:

In such a situation try installing that package in the local folder and re add it to the deployment package manually.

pip install markupsafe -t ./ zip -r my-deployment-package.zip markupsafe-1.1.dist-info/

After you have re packaged the code you will need to redeploy the zip folder and perform a test.