Build, Test, and Deploy with Zappa

Learn how to build, test, and deploy a Python web service using Zappa in this article by Abdulwahid Abdulhaque Barguzar, a software architect and an active contributor to the open source community. He has developed a deep understanding of architecting software product development through Python web frameworks and JavaScript frameworks.

What is Zappa?

Zappa is an open source tool that was developed and designed by Rich Jones, founder/CTO of Gun.io (https://www.gun.io/). Zappa is mainly designed to build and deploy serverless Python applications on AWS Lambda and API Gateway.

Zappa is great for deploying serverless Python microservices with frameworks such as Flask and Bottle for hosting large web applications and CMSes with Django. You can also deploy any Python WSGI application as well.

This article will show you how to create a simple hello world program as a microservice using Python’s Bottle framework. You can find the relevant code files for this article at https://github.com/PacktPublishing/Building-Serverless-Python-Web-Services-with-Zappa/tree/master/Chapter02.

Time to get started!

Installing and configuring Zappa

Installing Zappa is a straightforward task, but you need to configure the prerequisites. Make sure that you have Python 2.7 or Python 3.6 and a valid AWS account. Now, you need to configure the AWS credentials on your machine with help awscli :

Configure the AWS credentials using the aws configure command, as shown in the following screenshot:

The configuration for AWS credentials requires that you have AWS Access Key ID , AWS Secret Access Key , Default region name , and Default output format . You can get AWS credential information from your My Security Credentials  page:

Once you’ve configured your AWS credentials, you can begin the Zappa installation. Zappa must be installed in a virtual environment. It’s strongly recommended that you create a virtual environment and activate it before installing Zappa. You can use the virtualenv  tool:

You have successfully created a virtual environment named env using python3.6, where -p  indicates the Python version. Now, activate the virtual environment as follows:

Now, install Zappa using pip :

You are now ready to launch Zappa.

Building, testing, and deploying a Python web service using Zappa

You can follow some basic steps to configure a small project using the Bottle framework:

  1. First, create a new project directory named lambda_bottle_poc :

  1. Now, create a virtual environment inside the lambda_bottle_pocdirectory :

  1. Here is the basic hello world program using the Bottle framework:

Now it’s time to deploy the program as serverless on AWS Lambda and expose the /hello  API via API Gateway. With the help of Zappa, all manual processes of the AWS console and AWS CLI are automated and provide a rapid process to deploy and maintain your application in a serverless environment.

Building the deployment package

Initialize Zappa using the zappa init  command. This command helps you to create and deploy Python applications. This command runs in a user interactive mode with some basic questions required so that you can set up the deployment process.

By the end of the questionnaire, Zappa creates a JSON file named zappa_settings.json . This file is nothing but the backbone of Zappa as it maintains the mapping information that’s used by Zappa internally. Take a look at the following screenshot, which describes the flow of the zappa init  command:

As you can see, zappa init  starts up user interactive mode with some questions. Now, here’s some information about each question.

What do you call this environment? (default dev)

Amazon API Gateway provides a mechanism to maintain different environment stages of your hosted API. For example, you can create environment stages for development, staging, and production.

With the help of Zappa, you can manage environment stages easily. You can define your own environment stage name or leave it empty to consider the default stage name as dev.

What do you want to call your bucket? (default zappa-2o2zd8dg4)

Zappa deployments need to be uploaded to a private Amazon S3 bucket. AWS Lambda requires two types of code entries, such as inline code and uploading the ZIP. If the ZIP file size exceeds 10 MB, then consider uploading the ZIP onto Amazon S3. This is why Zappa, by default, creates a bucket that will be used to upload the deployment ZIP file and refer to AWS Lambda.

You can provide your own existing bucket name or choose the default, as suggested by Zappa. If the bucket doesn’t exist, Zappa will automatically create one for you.

What’s the modular path to your app function? (default dev)

The AWS Lambda function requires an attribute, such as lambda_handler, which points to a function as an entry point for Lambda execution. Hence, you need to provide information about the function name with a modular path such as <filename>.<function_name/app_name> to Zappa.

In this case, you have a file named hello.py  and an app object that was created using the Bottle  class of Python’s Bottle framework. Hence, the answer to this question is hello.app.

Would you like to deploy the application globally? (default n)

AWS provides a feature to extend the Lambda services to all available regions. That’s what you should do if you want to make your service available globally with much less latency. Zappa supports this feature, which will enable you to extend the Lambda service in all regions without any manual effort.

Finally, you would get a zappa_settings.json file, which will have all configurations related to your deployments. Now, take a look at the zappa_settings.json  file in the next section.

The zappa_settings.json file

Once you’ve finished the questionnaire session, Zappa creates a basic zappa_settings.json  file based on your input. It plays an important role in configuring Zappa with your project. If you initialize Zappa in your existing project ( Django/Flask/Pyramid/Bottle ), then Zappa automatically detects the type of project and creates the zappa_settings.json  file accordingly.

The following is the content of the newly created zappa_settings.json  file for the hello world  program:

For a Django project, it uses django_settings instead of app_function. django_settings needs to be initialized with the path to your Django settings:

The preceding configuration is enough to deploy a basic Python web application. Now, deploy hello world as a serverless application.

Deploying and testing hello world

Zappa deployment is super easy as you only need to run a single command in order to start the deployment:

That’s it! You are done with deployment. The following screenshot describes the deployment process:

Once the deployment completes, you’ll get the API URL endpoint. Test the hello world application by hitting the API URL with the /hello  endpoint:

After running the preceding command, you’ll see the following output:

It’s really amazing to be able to configure the service and deploy it in a few seconds.

If you found this article interesting, you can explore Building Serverless Python Web Services with Zappa to master serverless architectures in Python and their implementation, with Zappa on three different frameworks. Building Serverless Python Web Services with Zappa will help you build serverless applications in a quick and efficient way.

Abdulwahid Abdulhaque Barguzar is a software architect and an active contributor in the open source community. He has developed a deep understanding of architecting software product development through Python web frameworks and JavaScript frameworks. He is passionate about implementing and mastering new technologies. In his free time, he researches innovative approaches to rapidly developing and designing a software product and automation testing, aiming to become a master software architect.

Tagged with: , , , ,