Running Clojure on Azure Web Sites

The recent release of Java on Microsoft Azure Web Sites was a big one. Not only because there are a load of Java developers out there that can now get easy access to a great public cloud (and yes, Web Sites have sticky sessions out of the box) but the announcement is much more than just Java, it’s the Java Virtual Machine.

The JVM is host to a number of interesting languages, so this announcement unlocks the potential to run thing likes Scala, Clojure, Groovy, JRuby, Jython and Kotlin on Azure Web Sites.

I thought it would be fun to start with Clojure. It’s what Uncle Bob would do.

This tutorial takes you from scratch to having a clojure ‘hello world’ running. I did this all from Windows 8, but I assume you could do this just as easily from any desktop operating system.

Prerequisites

Install Java

Install Java SE Development Kit 7

Install Clojure 1.6

Install Leiningen

Install Git

(yes there’s a lot of installing)

Create a Hello World Application

We’ll use compojure to create a simple hello world app, and then create a war file.

From the command prompt type this:

> lein new compojure hello-world

You can test your new web application by typing in:

> cd hello-world
> lein ring server

… which will start the web server on port 3000.

Now create a ‘war’ file, which we will use to deploy the application to Azure:

> lein ring uberwar

This should create something called target/hello-world-0.1.0-SNAPSHOT-standalone.war

Creating the Azure Web Site from the Jetty Template

The easiest way to get Java running is to take the existing Jetty template.

In the Azure Portal go to ‘New’ and select Compute -> Web Site -> From Gallery.

Select the ‘Jetty’ template (it’s in the ‘Templates’ sub section).

Type in a unique url, and select a region, and click the ‘tick’ button to create the site.

This will create a template website with the Jetty stuff already configured.

Deploy to Azure

To deploy our Clojure app, we need to put the war file in the right place in the Jetty template, and then push it up to Azure.

In the portal navigate to the newly created website and click ‘Set up deployment from source control’ and select ‘Local Git repository’.

This will take a few seconds, then give you a git url.

Copy this url, and then from the command prompt, navigate out of the clojure app directory, and clone the website to a new directory (make sure the USERNAME and WEBSITENAME bit are correct for your site).

> cd ..
> git clone https://USERNAME@WEBSITENAME.scm.azurewebsites.net:443/WEBSITENAME.git
> cd WEBSITENAME

Now take the ‘target/hello-world-0.1.0-SNAPSHOT-standalone.war’ file, and save it in the ‘bin/jetty-distribution-9.1.2.v20140210/webapps’ directory as ‘ROOT.war’. This should replace the existing file.

> git commit -am "added clojure war"
> git push origin master

And you’re done.  Navigate to the URL for your web site, and you should see ‘Hello World’.

Conclusion

It’s exciting to see Microsoft providing support for the JVM in Azure Web Sites. The JVM support a rich ecosystem of new languages and web frameworks, and deployment to Azure websites is a simple git push away.

Oh, and Clojure is fun.

Advertisements