Heroku + Flask: Simplicity

After playing around with Amazon’s free tier EC2/EBS instances, I found myself becoming frustrated with the amount of work it took to get something simple up and running. Take this with a grain of salt; I am by no means a command line master or an experienced dev ops guy. Shortly after mentioning this to a co-worker, I was directed to Heroku.

It was a dream. Heroku takes care of almost all of the dev ops stuff that can make life a nightmare. Following their getting started with Python documentation was incredibly straightforward. While their pricing seems like it can be a bit high, they have certainly provided a a well refined product to a market of developers who either do not have the interest in configuring environments in the cloud or don’t have the time to dedicate to its maintenance. Well done, Heroku.

Heroku and Flask Setup in Ubuntu 13.04:

This is a brief walk through of setting up and launching a simple Heroku environment and Flask server. It follows closely with the official Heroku Python docs with a couple of my personal notes. Enjoy!

  1. Sign up for Heroku.
  2. Download the the Heroku toolbet:
  3. $ sudo su
    $ wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh

  4. Login to Heroku: If prompted to generate a key enter yes.
  5. $ heroku login

  6. Make a directory to hold your app, create a virtual environment, and activate it.
  7. $ mkdir /path/to/your/app
    $ cd /path/to/your/app
    $ virtualenv venv –distribute
    $ source venv/bin/activate

  8. Install Flask, our framework, and gunicorn, our web server.
  9. $ pip install Flask gunicorn

  10. Now, create a very simple hello world application.
  11. $ vi hello.py

    import os
    from flask import Flask

    app = Flask(__name__)
    @app.route(‘/’)

    def hello():
    return ‘Hello World!’

  12. Next, we need to create a Procfile which will to state which command should be called when the web dyno starts; Gunicorn in our case.
  13. $ vi Procfile

    web: gunicorn hellp:app

  14. Finally, start the process using Foreman from the command line and verify it’s up and running.
  15. $ foreman start
    $ python
    >>> import requests
    >>> response = requests.get(‘http://0.0.0.0:5000′)
    >>> response.text
    u’Hello World!’
    >>> exit()

  16. Heroku requires a requirements.txt file in the root of your repository to identify it as a Python application. Fortunately,  pip does this for us. From the root directory:
  17. $ pip freeze > requirements.txt
    $ cat requirements.txt
    Flask==0.9
    Jinja2==2.7
    MarkupSafe==0.18
    Werkzeug==0.8.3
    argparse==1.2.1
    distribute==0.6.34
    gunicorn==0.17.4
    wsgiref==0.1.2

  18. Create and setup your .gitignore file to disregard files we don’t want being uploaded to our repository.
  19. $ vi .gitignore

    1 venv
    2 *.pyc

  20. Next, initiliaze the git repo, add your app to it, and commit it.
  21. $ git init
    $ git add .
    $ git commit -m “Initial commit of my Heroku app.”

  22. Push your repo to Heroku and launch your app.
  23. $ heroku create
    $ git push heroku master
    *Note: If you see something like: ‘Push rejected, no Cedar-supported app detected’, verify that your requirements.txt exists, is complete, and it is in the root of your application directory structure.*
    $ heroku ps:scale web=1

  24. Visit your app!
  25. $ heroku open

And that’s basically it. Please visit the official Heroku Python docs for more information.

A few useful commands:

  • heroku logout — logs you out
  • heroku ps:scale worker=0 — stops your dyno
  • heroku run python — launches an interactive Python shell

-H.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: