Deploy your Flask API to any Serverless Cloud Platform using this Simple Pattern

Andrew Griffiths
HackerNoon.com
4 min readApr 12, 2018

--

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

Application Code

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

Complete application code available at the example repository.

Run the App

Run the app in local development mode.

Test the App

In another terminal window manually test the API.

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.

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 the Application into a 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.

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.

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.

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

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

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 Functions deployments.

Full code for this tutorial available on github.

Originally published at andrewgriffithsonline.com. Andrew Griffiths is a freelance developer specialising in building and deploying applications in Golang, Python and JavaScript using Serverless platforms.

--

--

Andrew Griffiths
HackerNoon.com

Technology consultant helping companies build and scale using serverless technology https://andrewgriffithsonline.com/