I am excited about Cloud Foundry. It promises to greatly simplify java application deployment to Amazon’s Elastic Compute Cloud (EC2). With a few clicks, you can deploy an J2EE application (e.g. Grails) to the container of your choice (Tomcat 5.5, tc Server, and more) on a fresh EC2 Linux instance backed by a MySQL database on Amazon’s Elastic Block Store (EBS). It doesn’t get much easier than this. The instance configuration automatically configures your container behind an Apache instance, which gives you some great options for hosting static content or configuring side-by-side application platforms.
I’ve been using Burt Beckwith‘s UI Performance plugin for Grails to bundle and compress my static content (images, js, css) during the build, so I can save on outbound bandwith and improve the response time of my application. In order to take advantage of this build-time optimization, we need to modify the Apache configuration to serve static content instead of proxying those requests to the container.
- $ grails install-plugin ui-performance
- Configure your app to use the plugin’s taglibs and to bundle resources if necessary. (See http://www.grails.org/plugin/ui-performance)
- Add a handler for the CreateWarEnd event in your scripts/Events.groovy file (make one if you don’t have one). This handler will package all the bundled and gzipped resources into a static zip file that you can upload to Cloud Foundry:
eventCreateWarEnd = {warName, stagingDir -> def directory = new File(warName).parent ant.zip(destfile:"${directory}/static.zip") { fileset(dir:"${stagingDir}") { include name: "images/**" include name: "css/**" include name: "js/**" } } } - Create a “container initialization script” for Cloud Foundry to execute when your instance starts up. This script will tell Apache to directly serve the following directories and to add the appropriate Content-Encoding header for content that is already gzipped. The name doesn’t really matter at this point, but I called mine proxyexcludes.sh.
cat > /etc/httpd/conf.d/_proxyexcludes.conf <<END ProxyPass /images ! ProxyPass /css ! ProxyPass /js ! SetEnvIf Request_URI "\\.gz\\.(css|js|jpg|gif|png|ico)\$" already_compressed=true Header set Content-Encoding gzip env=already_compressed END
I found that keeping this file up to date was easier if I just let grails rebuild it every time after the WAR is created. I modified my eventCreateWarEnd handler to take care of this for me:
eventCreateWarEnd = {warName, stagingDir -> def directory = new File(warName).parent ant.zip(destfile:"${directory}/static.zip") { fileset(dir:"${stagingDir}") { include name: "images/**" include name: "css/**" include name: "js/**" } } ant.echo "Creating Proxy exclusion list: ${directory}/proxyexcludes.sh" new File("${directory}/proxyexcludes.sh") << """cat > /etc/httpd/conf.d/_proxyexcludes.conf <<END ProxyPass /images ! ProxyPass /css ! ProxyPass /js ! SetEnvIf Request_URI "\\.gz\\.(css|js|jpg|gif|png|ico)\$" already_compressed=true Header set Content-Encoding gzip env=already_compressed END """ } - $ grails war
- Upload the static.zip file produced during the build as “static content” when you upload your app to Cloud Foundry:
- When creating a deployment, upload your “proxyexcludes.sh” container initialization script:
- Fire up your instance and check out the performance gains!

