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 app.py.

[flask-zappa/app.py]

from flask import Flask, render_template


app = Flask(__name__)

@app.route('/', methods=['GET'])
@app.route('/index', methods=['GET'])
def index():
    return render_template(
        'index.html',
        title='Flask-Zappa',
        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.

[flask-zappa/templates/index.html]

<!DOCTYPE html>
<html>
<head>
    <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>
</head>
<body>
    <h1>{{ message }}</h1>
</body>
</html>

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 FLASK_APP=app.py
$ (env) export FLASK_DEBUG=1
$ (env) flask run
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 254-980-658

Open your browser to http://127.0.0.1:5000 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": "app.app",
        "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!: https://...amazonaws.com/dev

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.

Conclusion

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.

Contents