Heroku-ification of a Clojure App

Here at Relevance, Inc we often use Heroku for our Rails apps. Unfortunately, there hasn't been a similarly easy deployment story for our Clojure web apps. Recently, Heroku has changed that with their Cedar stack.

My colleague, Craig Andera, has been working on a web port of a board game called Artifact. I noticed he added a GitHub Issue "Deploy app to Heroku" This seemed like the perfect time to show off just how easy it is to deploy a Clojure web app to Heroku.

I grabbed a recent commit that was configured to run a local Jetty server. Here is the simple process I followed to get his app deployed to Heroku. As a prerequisite, you should have a Heroku account as well as the heroku gem installed. That process is described on their site.

Start with cloning the repo

git clone git@github.com:candera/artifact.git
cd artifact

The next step is to create a new heroku app on cedar called artifact. When this command is run from inside your app, the remote heroku repository is automatically added for you.

heroku create --stack cedar artifact

Then modify the -main function to work with how Heroku manages ports. The change we are making here is to read the port from the environment variable when it is available.

In core.clj, change the following block of code from

(defn -main [& args]
  (run-jetty app {:port 8080}))


(defn -main [& args]
  (let [port (Integer/parseInt (get (System/getenv) "PORT" "8080"))]
    (run-jetty app {:port port})))

Next, create a Procfile in the root dir of the app to tell Heroku how to start our app. This tells the web dyno to use leiningen to launch the web server.

web: lein run -m artifact.core

Commit the changes and push to Heroku.

git add .
git commit -m "Heroku-ify"
git push heroku master

And finally view the page. heroku open

You can see the app at http://artifact.herokuapp.com. It is still in early development, so expect to run into rough edges. If you'd like to help, I'm sure Craig would love your contributions.

The process of taking a working Clojure web app and moving it to Heroku took me less than ten minutes, a small code change, and an additional line of config. This is a major development in the life of Clojure web development. With Heroku, we can build an app with the power of Clojure and the ease of deployment that Heroku provides to other frameworks, such as Rails. This helps us continue to expand our business and offer choice in deployment strategies to all our customers.

For more detail than just this simple intro, be sure to read "Building a Database-Backed Clojure Web Application".

Get In Touch