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!
- Sign up for Heroku.
- Download the the Heroku toolbet:
- Login to Heroku: If prompted to generate a key enter yes.
- Make a directory to hold your app, create a virtual environment, and activate it.
- Install Flask, our framework, and gunicorn, our web server.
- Now, create a very simple hello world application.
- 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.
- Finally, start the process using Foreman from the command line and verify it’s up and running.
- 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:
- Create and setup your .gitignore file to disregard files we don’t want being uploaded to our repository.
- Next, initiliaze the git repo, add your app to it, and commit it.
- Push your repo to Heroku and launch your app.
- Visit your app!
$ sudo su
$ wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh
$ heroku login
$ mkdir /path/to/your/app
$ cd /path/to/your/app
$ virtualenv venv –distribute
$ source venv/bin/activate
$ pip install Flask gunicorn
$ vi hello.py
from flask import Flask
app = Flask(__name__)
return ‘Hello World!’
$ vi Procfile
web: gunicorn hellp:app
$ foreman start
>>> import requests
>>> response = requests.get(‘http://0.0.0.0:5000′)
$ pip freeze > requirements.txt
$ cat requirements.txt
$ vi .gitignore
$ git init
$ git add .
$ git commit -m “Initial commit of my Heroku app.”
$ 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
$ 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