Share on facebook
Share on twitter
Share on linkedin
Share on email

Solution for Serverless Python Web Hosting on AWS

Serverless python web hosting AWS

A common challenge developers face when working on a project is application hosting.  I’ve found serverless solutions to be appealing for many reasons (scalability, no server maintenance, pay-as-you-go, etc.), but haven’t found a great migration solution—until now.

Zappa is a very easy-to-use Python package that helps you host your project on AWS Lambda and API Gateway. It’s as easy as set up, install, initialize, and deploy! In this article, I’ll go through how to host a simple flask app, and lastly, how to use a secure custom domain.

Set Up

The first thing we need to do is set up your AWS account on your laptop. For this step, you’ll need an IAM user set up for your AWS account.

In a terminal, create a file ~/.aws/credentials and enter the credentials.

If you have multiple accounts or IAM users that you would like to use, you can add them here. Zappa will be able to distinguish between multiple accounts.

aws_access_key_id = [ACCESS_KEY_FROM_AWS]
aws_secret_access_key = [SECRET_KEY_FROM_AWS]

aws_access_key_id = [ACCESS_KEY_FROM_AWS]
aws_secret_access_key = [SECRET_KEY_FROM_AWS]

Create another file ~/.aws/config and enter the preferred region.

output = json
region = us-east-2

[profile org2]
output = json
region = us-west-1

Now create a project folder and a virtual environment with python3 if you choose. I’ll call my project zappa-demo and my virtualenv Zappa-Demo.

mkdir zappa-demo
cd zappa-demo


Now we can begin with our project. Let’s start by installing dependencies and getting our flask application running.

Create a file called requirements.txt with flask and Zappa.

We’ll use pip to install the Python requirements.
pip install -r requirements.txt

Create your flask application in a file called

# ~/zappa-demo/

from flask import Flask

app = Flask(__name__)

def hello_world():
    return "Hello World!"

if __name__ == '__main__':

Run your flask app and test by visiting and view the “Hello World” message.

                                                                                  (env: Zappa-Demo)
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on (Press CTRL+C to quit)

Now that flask is running okay, let’s work on setting up Zappa and migrating it to our AWS serverless architecture. Stop the web server with CTRL+C and continue on.


Zappa is controlled by some CLI commands. You can view a full list of them using the help command.

zappa --help

For us to get started, we will use the initialize command.

zappa init

After the welcome screen appears, follow the series of on-screen prompts to create the initialization file. Leave the section blank to accept the default option.

First, enter an environment name. This is helpful when setting up multiple environments to separate production from others used for development and staging.

Welcome to Zappa!

Zappa is a system for running server-less Python web applications on AWS Lambda and AWS API Gateway.
This `init` command will help you create and configure your new Zappa deployment.
Let's get started!

Your Zappa configuration can support multiple production stages, like 'dev', 'staging', and 'production'.
What do you want to call this environment (default 'dev'):

Second, Zappa will recognize the profiles created in our ~/.aws/credentials file. Notice they ask if we want to use default or org2 that we set up at the beginning.

AWS Lambda and API Gateway are only available in certain regions. Let's check to make sure you have a profile set up in one that will work.
We found the following profiles: default, and org2. Which would you like us to use? (default 'default'):

During deployment, Zappa zips and moves your project to an s3 bucket. In step 3, enter the name of the bucket you would like to use. I prefer a naming convention like [PROJECT]_[ENVIRONMENT]_ZAPPA to keep things visually organized.

Your Zappa deployments will need to be uploaded to a private S3 bucket.
If you don't have a bucket yet, we'll create one for you too.
What do you want to call your bucket? (default 'zappa-f374x10rx'):

Next, Lambda needs to know what file is the executable file. Zappa recognized that our flask application was in a file called and the initialization was called app also. They recommend and we should accept this value.

It looks like this is a Flask application.
What's the modular path to your app's function?
This will likely be something like ''.
We discovered:
Where is your app's function? (default ''):

The last step asks if we want global service. We will choose no for this.

You can optionally deploy to all available regions in order to provide fast global service.
If you are using Zappa for the first time, you probably don't want to do this!
Would you like to deploy this application globally?
(default 'n') [y/n/(p)rimary]:

Now that the init is complete, we have a zappa_settings.json file that has been generated for us.

Accept the values to continue.

Okay, here's your zappa_settings.json:

    "dev": {
        "app_function": "",
        "aws_region": "us-east-2",
        "profile_name": "default",
        "project_name": "zappa-demo",
        "runtime": "python3.7",
        "s3_bucket": "zappa-f374x10rx"

Does this look okay? (default 'y') [y/n]:

*As you are more comfortable with Zappa, you can create this file manually and skip the init step next time.


Now that Zappa is configured, we simply run the deploy command.

zappa deploy

You will see your app being packaged and pushed to AWS services. At the bottom of the output you will see “Deployment Complete” with a URL. Visit the URL to access your site.

Calling deploy for stage dev..
Creating zappa-demo-dev-ZappaLambdaExecutionRole IAM Role..
Creating zappa-permissions policy on zappa-demo-dev-ZappaLambdaExecutionRole IAM Role.
Downloading and installing dependencies..
 - markupsafe==1.1.1: Using locally cached manylinux wheel
 - sqlite==python3: Using precompiled lambda package
Packaging project as zip.
Uploading (5.4MiB)..
100%|█████████████| 5.71M/5.71M [00:37<00:00, 152kB/s]
Scheduled zappa-demo-dev-zappa-keep-warm-handler.keep_warm_callback with expression rate(4 minutes)!
Uploading zappa-demo-dev-template-1581976197.json (1.6KiB)..
100%|█████████████| 1.62k/1.62k [00:00<00:00, 22.5kB/s]
Waiting for stack zappa-demo-dev to create (this can take a bit)..
100%|████████████████| 4/4 [00:12<00:00,  3.15s/res]
Deploying API Gateway..
Deployment complete!:

Rename and Secure

Now that we have our site created, we want a custom domain and to secure it with an SSL certificate. Create an AWS Route53 record with a domain that you own. Create a CNAME record you want to attach and enter the domain as the value.

serverless pythone web hosting on aws

Need to secure it with an SSL certificate? Use AWS Certificate Manager to create a certificate for your domain. (Check AWS Certificate Documentation for help.)

Once you have the certificate, update the zappa-settings.json file with certificate_arn and the domain you want to use.

    "dev": {
        "app_function": "",
        "aws_region": "us-east-2",
        "profile_name": "default",
        "project_name": "zappa-demo",
        "runtime": "python3.7",
        "s3_bucket": "zappa-f374x10rx"
        "certificate_arn": "arn:aws:us-east-2:514021523585...",
        "domain": ""

Now execute the Zappa certify command to set the certificate to your domain.

zappa certify

Now we can access our site at!


What if I update my code and need to push the change?

Simply run the update command.

zappa update

What if I have environment variables I need to add to my application?

Visit the AWS Lambda, choose your region and function, click environment variables, and add as many as you need. This is what I use to connect to AWS RDS database service.

Serverless python AWS

What if I want to delete everything?

There’s an undeploy command for that. It will delete the API Gateway and Lambda function.

zappa undeploy


Feel free to reach out to the product engineering team at InfoTrust today with questions about serverless Python web hosting on AWS, or to simply learn more about the unique technology projects taking place at InfoTrust.

Engineer? We're Hiring!

With fully-paid health insurance, a flexible work schedule, open PTO policy, and more, InfoTrust has quickly become a destination employer for Python engineers.
Share on facebook
Share on twitter
Share on linkedin
Share on email

Other Articles You Will Enjoy

Talk To Us

Receive Book Updates

Fill out this form to receive email announcements about Crawl, Walk, Run: Advancing Analytics Maturity with Google Marketing Platform. This includes pre-sale dates, official publishing dates, and more.

Our website uses cookies and may collect user information to provide a good experience. Read our Privacy Policy here.

Leave Us A Review

Leave a review and let us know how we’re doing. Only actual clients, please.