July 13, 2018

Deploying a Flask app in serverless Python

This week was a special week at the office. First of all, it was not at the office, and second of all, it was a whole week of professional development.

Professional development opportunities—and budget—are always limited so our boss let us spend a week catching up with whichever technology, online course, whitepapers, etc. we needed to absorb.

My personal goals for the week were:

  • Complete the AWS Certified Cloud Practitioner course on Linux Academy
  • Build a simple Python/Flask weather app leveraging the Weather Underground API
  • Deploy the app on AWS, on Elastic Beanstalk or other

The first two were fairly straightforward. The app is on GitHub and does a very simple job: Showing the temperature and the weather conditions for Ithaca, NY, with a timestamp.

Screenshot of my simple weather app: weather condition, timestamp, and temperatures in F and C. I later deployed it in serverless Python.

A simple weather app. It’s Bootstrappy but the icon is cute.

Going serverless

When I was ready to look into a way to deploy the app on AWS, I bumped into Zappa: Serverless Python web services. I hadn’t even thought about the possibility of having an app deployed to a Lambda. I was going to explore EC2 instances but not having to maintain a server and only pay when an app is not used is appealing!

But there’s still a server, right?

To quote Zappa’s FAQ, yes, there’s still a server but “it only has a 40 millisecond life cycle!” Amazon API Gateway gives each request their own virtual http server. Apps get killed after the app is returned. Amazing.

How to run Zappa?

Zappa is pretty simple and as long as you have a Flask or Django app, it’s as easy as:

$ pip install zappa
$ zappa init
$ zappa deploy

You’ll have to make sure that the AWS CLI is installed, though, and configured. zappa init will run a wizard that gathers all the info needed.

Zappa takes care of everything—including in AWS: Creating the S3 bucket, zipping the app, creating the API Gateway routes, etc. When it’s done, it returns the deployment URL.

Let’s use serverless Python all the time?

I really loved dipping my toes into serverless Python but I still want to be able to deploy an app through Elastic Beanstalk. I appreciate that Zappa is doing all of the heavy lifting but I need to do some in order to learn.

Also, I don’t remember being asked anything about database setup when I ran zappa init. Most of the applications I’ve been developing to learn Flask rely on a database. I love using Heroku for this but I’ll teach myself how to do this in AWS, too.

And if your mind was blown and you want to show them some support, they have shirts!