Deploy Serverless Python Flask App to AWS Using Zappa

In this tutorial we will deploy a serverless Python Flask application to AWS using Zappa. Zappa allows us to focus on the app while it creates the necessary permissions and serverless resources on AWS. Specifically, Zappa will create the API Gateway, Lambda Function, and IAM Roles to run the Flask Website along with an S3 Bucket to bundle and deploy our Flask App and Python dependencies.

Create a Python Virtual Environment and Install Flask

First, create a new directory for the Flask Application, change into it, and create a new virtual environment. Install Flask after you activate the environment.

$ mkdir flask-zappa && cd $_
$ python -m venv env
$ source env/bin/activate
$ (env) pip install flask

Create Python Requirements.txt file to Specify Dependencies

After you install Flask, create a requirements.txt file to specify dependencies. Zappa will use this file to gather the dependencies and bundle them as part of your app when deploying it to AWS.

$ (env) pip freeze > requirements.txt

Create and Run the Python Flask Application

Using your favorite code editor, create a simple Flask Application and save it to the directory as


from flask import Flask, render_template

app = Flask(__name__)

@app.route('/', methods=['GET'])
@app.route('/index', methods=['GET'])
def index():
    return render_template(
        message='Welcome to Serverless Flask on AWS using Zappa!'

Let’s make the app mildly interesting by having it render a page template. Create a Flask template and save it to a templates directory as index.html.


<!DOCTYPE html>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">        
    <title>{{ title }}</title>
    <h1>{{ message }}</h1>

Now we have a Python Flask Application with a single route that displays an HTML Page based on the index.html Jinja 2 template.

Run the web application from the terminal and display it in the browser.

$ (env) export
$ (env) export FLASK_DEBUG=1
$ (env) flask run
 * Running on (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 254-980-658

Open your browser to to view the Flask Website. Press CTRL+C to quit the Flask Web Server when you are done viewing the website.

Deploy the Flask Application to AWS Using Zappa

We have the website running locally using Flask’s development server, but let’s install Zappa and get it running serverless using AWS. Install Zappa and run its INIT command.

$ (env) pip install zappa
$ (env) zappa init

Zappa will guide you through a series of questions to create a zappa_settings.json file which will be used for deployment. I accepted the defaults and it created the following JSON file.

    "dev": {
        "app_function": "",
        "aws_region": "us-east-1",
        "profile_name": "default",
        "project_name": "flask-zappa",
        "runtime": "python3.6",
        "s3_bucket": "zappa-g8bmqdovt"

Review the settings and make sure they are acceptable to you. When you are ready deploy the application.

$ (env) zappa deploy
Deploying API Gateway..
Deployment complete!:

This process is fairly involved as Zappa collects the necessary dependencies, bundles the code for deployment, and creates the API Gateway, Lambda, IAM roles, and S3 bucket to deploy your application. When complete, you will see an API Gateway endpoint to view the website.

Open your browser to view the serverless Flask Application!!!

Delete AWS Resources

Ideally Zappa would use AWS Cloud Formation to deploy applications, but this is not the case. You will need to manually delete all AWS resources used to create the Python Flask App.


Zappa makes it incredibly easy to deploy Python Flask Applications to AWS. Although I’ve only tried it with simple apps thus far, it has worked flawlessly for me with zero code changes.