It’s straightforward to run a Clojure web app on the AWS Beanstalk. Amazon will run a Java WAR file and there are existing Clojure tools to build a WAR file and push it out.

So let’s do that. We’ll

  • Make a simple web app
  • Push out a WAR file to beanstalk
  • See it live on the web

A Hello World web app

The source is on http://github.com/ctdean/beanstalk-hello

We’re assuming that you have lein and Java installed.

We’ll use http://webnoir.org/ to build the app. First the app itself.

(ns ctdean.hello
  (:use noir.core hiccup.core)
  (:require [noir.server :as server]))

;; The only page
(defpage "/" []
  "<html><body>Hello world</body></html>")

;; The handler for ring and the war file
(def handler (server/gen-handler {:mode :dev
                                  :ns 'ctdean.hello}))

And then the equally important project.clj

(defproject ctdean-beanstalk-hello "1.0.0"
  :description "hello world on the aws beanstalk"
  :license {:name "MIT License"
            :url "http://www.opensource.org/licenses/mit-license.html"}
  :plugins [[lein-ring "0.6.2"]
            [lein-beanstalk "0.2.2"]]
  :dependencies [[org.clojure/clojure "1.3.0"]
                 [noir "1.2.2"]]
  :ring {:handler ctdean.hello/handler})

The project name will be used to create the files on Amazon so you will need to choose a unique project name.

Let’s try it out:

% lein ring server &

You can look at it in your browser at http://localhost:3000, or on the command line:

% curl http://localhost:3000
<html><body>Hello world</body></html>

Push out a WAR file

Since we’re using https://github.com/weavejester/lein-beanstalk we can just use it to push out our code.

Make sure that your AWS account exists and you can access the AWS console at https://console.aws.amazon.com/elasticbeanstalk

Add a lein-beanstalk-credentials definition to your ~/.lein/init.clj file that contains your AWS credentials:

(def lein-beanstalk-credentials
  {:access-key "XXXXXXXXXXXXXXXXXX"
   :secret-key "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"})

and then push out the war file:

% lein beanstalk deploy development

If you get an error during this step make sure that your credentials are correct and that you have chosen a unique project name in your project.clj file.

View the app

Assuming the name of your app is USER-BEANSTALK-HELLO, the url will be http://USER-BEANSTALK-HELLO.elasticbeanstalk.com/

You can terminate the app from the console or from the command line using

% lein beanstalk terminate development

Remember to terminate the app or Amazon will keep charging for it.

Published

10 Apr 2012


Previous Archive Next