I’m going to demonstrate how to make a Flask app serverless without locking you into any tooling or cloud platforms. It doesn’t require any application code changes or serverless frameworks so you can continue to develop locally like you always have.

Ideally you should re-architect your application into small discrete functions to take full advantage of serverless. However, if you want to migrate a legacy application or just a quick way to get started with serverless then this is a good approach to take.

  1. Create Flask Application
  2. Make the Application Serverless
  3. Package the Application into Zip for Lambda
  4. Deploy with Terraform

1. Create Flask Application

We are going to create a Flask application for Python3.6+.

Setup

First create a virtual environment and install dependencies.

$ python3 -m venv env
$ source env/bin/activate
$ pip install flask flask-lambda-python36 http-prompt

Application Code

Add the code below which will create a Flask Application with impeccable music taste.

$ tree -L 4
.
├── app
│   ├── api
│   │   ├── __init__.py
│   │   └── resources.py
│   └── __init__.py
└── run.py
# app/api/resources.py
from flask import Blueprint
from flask import request, jsonify

api = Blueprint('api', __name__)

@api.route('/artists', methods=('GET', 'POST'))
def handle_artists():
    if request.method == 'POST':
        return 'ok'

    return jsonify([{'name': 'enya'}])
# app/api/__init__.py
from .resources import api
# app/__init__.py
def create_app(Flask):
    app = Flask(__name__)
    from .api import api as api_blueprint
    app.register_blueprint(api_blueprint)

return app
# run.py
from flask import Flask
from app import create_app

http_server = create_app(Flask)

Complete application code available at the example repository.

Run the App

Run the app in local development mode.

$ FLASK_DEBUG=1 FLASK_APP=run.py flask run

Test the App

In another terminal window manually test the API.

$ http-prompt localhost:5000

http://localhost:5000> get artists

HTTP/1.0 200 OK
Content-Length: 31
Content-Type: application/json
Date: Thu, 12 Apr 2018 19:15:56 GMT
Server: Werkzeug/0.14.1 Python/3.6.5

[
    {
        "name": "enya"
    }
]

2. Make the Application Serverless

Up to this point this is just another Flask application. Time to make it serverless. Add another file called run-lambda.py to the root of the project.

# run-lambda.py
from flask_lambda import FlaskLambda
from app import create_app

http_server = create_app(FlaskLambda)

That’s it! Your application is now ready be run on AWS Lambda. The trick here is using FlaskLambda instead of normal Flask. FlaskLambda converts the payload that AWS feeds your Lambda function into the format that Flask expects.

This adaptor pattern is the same one that zappa uses. Personally I prefer not to introduce any extra tooling dependencies and instead opt for Terraform or AWS Serverless Application Model (SAM) for deployment.

Be aware that FlaskLambda only supports Python <= 3.5. I’ve created a fork that adds support for Python 3.6+ which you installed earlier with pip install flask-lambda-python36.

3. Package Application into Zip for Lambda

In order to deploy your application you need to package your application into a zip. The following script which will bundle your application code and dependencies.

# bundlelambda.sh
#!/usr/bin/env bash

OUTPUT_ZIP="$PWD/terraform/flask-app.zip"
ARCHIVE_TMP="/tmp/lambda-bundle-tmp.zip"

addToZip() {
    local exclude_packages="setuptools pip easy_install"
    zip -r9 s"$ARCHIVE_TMP" \
        --exclude ./*.pyc \
        --exclude "$exclude_packages" \
        -- "${@}"
}
addDependencies() {
    local packages_dir=()
    packages_dir+=($(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"))
    env_packages=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(plat_specific=1))")
    if [[ "$env_packages" != "${packages_dir[0]}" ]]; then
        packages_dir+=($env_packages)
    fi
    for (( i=0; i<${#packages_dir[@]}; i++ )); do
        [[ -d "${packages_dir[$i]}" ]] && cd "${packages_dir[$i]}" && addToZip -- *
    done
}

rm "$ARCHIVE_TMP" "$OUTPUT_ZIP"
addDependencies
addToZip app ./*.py
mv "$ARCHIVE_TMP" "$OUTPUT_ZIP"

This is a simplified version of the script in the example repository.

Save and run the script to create a zip bundle at terraform/flask-app.zip. Ensure you run this on a Linux platform to ensure compatibility with the Lambda runtime environment.

$ chmod +x bundlelambda.sh
$ ./bundlelambda.sh

4. Deploy with Terraform

Terraform Config

We will use the lambda-api-gateway terraform module to handle deployment. Create the Terraform configuration file at terraform/main.tf and update the account_id with your AWS account ID.

# terraform/main.tf
module "lambda_api_gateway" {
    source               = "git@github.com:techjacker/terraform-aws-lambda-api-gateway"

    # tags
    project              = "todo-mvc"
    service              = "acme-corp"
    owner                = "Roadrunner"
    costcenter           = "acme-abc"

    # vpc
    vpc_cidr             = "10.0.0.0/16"
    public_subnets_cidr  = ["10.0.1.0/24", "10.0.2.0/24"]
    private_subnets_cidr = ["10.0.3.0/24", "10.0.4.0/24"]
    nat_cidr             = ["10.0.5.0/24", "10.0.6.0/24"]
    igw_cidr             = "10.0.8.0/24"
    azs                  = ["eu-west-1a", "eu-west-1b"]

    # lambda
    lambda_zip_path      = "flask-app.zip"
    lambda_handler       = "run_lambda.http_server"
    lambda_runtime       = "python3.6"
    lambda_function_name = "HttpWebserver"

    # API gateway
    region               = "eu-west-1"
    account_id           = "123456789"
}

The key value here is lambda_handler which instructs AWS Lambda to use the run_lambda.py file as the entrypoint to our app.

Deploy

$ cd terraform
$ terraform apply

When complete, Terraform should output the API URL which you can use to manually test the deplopment succeeded.

$ http-prompt $(terraform output api_url)
GET artists

HTTP/1.0 200 OK
Content-Length: 31
Content-Type: application/json
Date: Thu, 12 Apr 2018 19:15:56 GMT
Server: Werkzeug/0.14.1 Python/3.6.5

[
    {
        "name": "enya"
    }
]

Conclusion

We took an existing Flask application and turned it serverless with just three lines of code! By using the adaptor pattern we have given ourselves the freedom to easily swap serverless cloud providers in the future. We could even switch to a container based deployment without having to change any of the application code.

Benefits of this approach:

  • Develop locally like you always have (no need for emulators)
  • Complete visibility into and control over your infrastructure
  • Avoids cloud vendor lock-in
  • Avoids application code framework lock-in

Future posts will cover AWS Serverless Application Model (SAM), Azure Functions and Google Cloud Function deployments.

Full code for this tutorial available on github.