Book Image

Building Serverless Python Web Services with Zappa

By : Abdulwahid Abdulhaque Barguzar
Book Image

Building Serverless Python Web Services with Zappa

By: Abdulwahid Abdulhaque Barguzar

Overview of this book

Serverless applications are becoming very popular these days, not just because they save developers the trouble of managing the servers, but also because they provide several other benefits such as cutting heavy costs and improving the overall performance of the application. This book will help you build serverless applications in a quick and efficient way. We begin with an introduction to AWS and the API gateway, the environment for serverless development, and Zappa. We then look at building, testing, and deploying apps in AWS with three different frameworks--Flask, Django, and Pyramid. Setting up a custom domain along with SSL certificates and configuring them with Zappa is also covered. A few advanced Zappa settings are also covered along with securing Zappa with AWS VPC. By the end of the book you will have mastered using three frameworks to build robust and cost-efficient serverless apps in Python.
Table of Contents (20 chapters)
Title Page
Dedication
Packt Upsell
Contributors
Preface
Index

Executing the Lambda function


AWS Lambda supports several methods of execution. Let's start with the basic execution from its own web console interface. AWS Lambda provides the capability to test the function manually, where you can define the test event context. If you want to test against some other Amazon services, then there are built-in event templates available.

The following screenshot demonstrates the test event creation:

As shown in the preceding screenshot, a single Lambda function can have a maximum of 10 test events and the test events are persisted, so you can reuse them whenever you want to test your Lambda function.

I created the test event with the event name as HelloWorld and now I am going to execute the HelloWorld function, when converting the Lambda function as a Python microservice, as shown in the following code:

from __future__ import print_function 
import json 
 
print('Loading function') 
 
def lambda_handler(event, context): 
    print("Received event: " + json.dumps(event, indent=2)) 
    print("value1 = " + event['key1']) 
    print("value2 = " + event['key2']) 
    print("value3 = " + event['key3']) 
    return "Hello World" 

Here, we are printing the event data and then returning back to the Hello World string:

Lambda manages some information on every request execution, such as a request ID and billing information. The Lambda price model is based on the time consumption on request processing, whereas the request ID is the unique identification of every request.

In the Log output, you can see all the print statements output. Now, let's raise an error and see how Lambda responds and returns the logs.

We are going to replace the current code with the following snippet:

from __future__ import print_function 
import json 
 
print('Loading function') 
 
 
def lambda_handler(event, context): 
    print("Received event: " + json.dumps(event, indent=2)) 
    raise Exception('Exception raised manually.') 

The following screenshot is the log snippet of the execution result:

Here, Lambda responded with the complete stack trace information and logged it as well. You can check the CloudWatch logs, as CloudWatch is preconfigured with the AWS Lambda execution.

We learned about the Lambda function execution from the Lambda console, and now it's time to execute the Lambda function from a schedule trigger. In our project, we often need to have a cron job schedule to execute some functionality at a particular time period.

Lambda triggers will help us to set up the triggers based on events. Let's move ahead to introduce the trigger to our hello world function.