Elastic Beanstalk and Flask

Deployment Gotcha’s

Petar Bojinov
3 min readApr 14, 2014

This weekend I decided to deploy my Flask app to Amazon’s Elastic Beanstalk with the allure of better performance, scalability, and easy management. EBS brings together traditional AWS components such as EC2, S3, and CloudWatch in an easy to use PaaS.

Python deployment to EBS is a breeze once you work out a few gotcha’s which I’m going to cover here.

Make sure your flask app is named application.py.

WSGI looks for an application.py file in your project root. This is the default when creating an EBS app but can be changed if you want to use another name by modifying the WSGIPath in the root of your app folder under .elasticbeanstalk/optionsettings.myflaskapp-env

WSGIPath in .elasticbeanstalk/optionsettings.myapp-env

I learned this the hard way when trying to deploy my app as run.py and kept getting a 500 server error from EBS.

Have an application object in application.py

This one was a little trickier to figure out. WSGI looks for an application object in the application.py file.

Incorrect example without application object

Notice how I assigned my Flask app to an api object, but not to an application object. If you don’t have the application object in there, you’ll get the dreaded 500 again even though your app health will be green and status ready when running

eb status
Correct example using application object

Now notice how I simply assigned my Flask app to application. This will get things working!

Managing and Installing Dependencies (the hard way)

I made the mistake and installed all my pip packages globally so when I went to create a dependencies file for using

pip freeze --local > requirements.txt

I got a list back of every globally installed package. This isn’t going to fly when we want to install only certain dependencies on our EBS instance.

Since I didn’t have that many external libraries in my project I manually added them to the requirements.txt file which EBS then uses to install these dependencies on the server.

Managing Dependencies Efficiently

A time saving alternative I came across after doing the latter was to use virtualenv to create a local isolated python environment.

Make sure you use -not-site-packages so virtualenv doesn’t symlink to your system packages when you install a new package in the future. Now when you run pip freeze again you’ll get all local dependencies in the requirements.txt

virtualenv —no-site-packages myflaskapp

A more in-depth tutorial that I used to setup my virtualenv can be found here.

Some downfalls of using EBS —

  1. Limited documentation
  2. The community is quite small. It isn’t the Node.js community oasis that I am used to.

Good Resources

http://stackoverflow.com/search?q=elastic+beanstalk

Original Flask deployment guide from Amazon — http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Python_flask.html

More gotcha’s — http://jtushman.github.io/blog/2013/06/12/first-impressions-with-flask-and-elastic-beanstalk/#4

--

--

Petar Bojinov

Passionate about developing products that millions of people use on a daily basis.