Java development 2.0: You can borrow EC2 too

Flexibility and low cost combine to create an efficient, scalable borrowed infrastructure

In this Java development 2.0 installment, Andrew Glover give you a hands-on introduction to developing for and deploying on the Amazon Elastic Compute Cloud (EC2). Learn how EC2 differs from Google App Engine, and leverage an Eclipse plug-in and the concise Groovy language to get a simple Web application up and running quickly on EC2.


Andrew Glover, Author and developer

Andrew GloverAndrew Glover is a developer, author, speaker, and entrepreneur with a passion for behavior-driven development, Continuous Integration, and Agile software development. You can keep up with him at his blog.

29 September 2009

Also available in Chinese Russian Japanese Portuguese

You've probably heard of the Standish Chaos Report, a yearly examination of the successes and failures of IT projects in the aggregate (see Resources). The report often contains a lot of bad news. (It wouldn't be called the Chaos Report if it were all sunshine, right?) The 2009 report claims that 44 percent of all IT projects were in bad shape for various reasons. One of the reasons cited is that individual projects were over budget. It's easy to imagine a few reasons why a project would overrun its costs. For example, programmers aren't cheap. Another cost that comes to mind is infrastructure.

As you know from the first article in this series ("Hello Google App Engine") a key aspect of Java™ development 2.0 is that software producers now have an alternative to maintaining high-cost, in-house infrastructures. Several years ago, a few smart companies took advantage of the commoditization of hardware (and related software) by building systems made up of a lot of cheap machines, knowing that the entire infrastructure would continue to work even if individual machines broke at some point. Those smart companies — like Google and Amazon (to name a few) — have enormous infrastructures that they can literally rent out to people like you and me (and keep their core business running smoothly at the same time). Consequently, the notion of cloud computing is a reality these days. If you find yourself budgeting for an IT project, you owe it yourself (and your company's wallet) to see if renting infrastructure from Google or Amazon is cheaper than acquiring the hardware yourself.

About this series

The Java development landscape has changed radically since Java technology first emerged. Thanks to mature open source frameworks and reliable for-rent deployment infrastructures, it's now possible to assemble, test, run, and maintain Java applications quickly and inexpensively. In this series, Andrew Glover explores the spectrum of technologies and tools that make this new Java development paradigm possible.

The EC2 difference

Google's and Amazon's borrowed infrastructures differ distinctly. As you saw in the previous article, Google App Engine is more of a platform for developing Java Web applications. You can't use everything available to the Java universe, such as Hibernate. Instead, you are constrained somewhat to using Google's Big Table, for instance. However, in return you get a free hosted solution that scales to your heart's desire. (Keep in mind that once your Google App Engine Web site reaches a bandwidth or space threshold, Google will charge you money.)

Amazon's EC2 offering is less of a development platform per se and more of a generic infrastructure service that hosts virtual machines (which can be Linux®, OpenSolaris, or Windows® based) on which you can run anything you'd like. EC2 isn't free, but it's a lot more flexible than Google App Engine. You can run any Java application (including one that uses Hibernate, for example) provided you can create or borrow a virtual machine. (Amazon and its community have entire catalogs of preconfigured virtual machines, called images.) As with Google App Engine, scaling an application to a global audience is quite efficient, because where the application is deployed and how it scales is largely under Amazon's control. (Amazon also lets you select a few geographical areas to deploy to.)

Amazon provides Eclipse plug-ins that facilitate creation of applications that can take advantage of the platform. In contrast to working with the Google App Engine plug-in, EC2's flexibility can make things fairly complicated quickly. For example, Amazon's EC2 Eclipse plug-in makes the use case of deploying an Apache Tomcat Web application (without a database) to a configurable cluster leveraging EC2 quite simple. But moving beyond this use case (say, to using a deployed instance of MySQL on EC2 or using Amazon's SimpleDB) requires a bit more jostling.

In this article, I'll show you how quickly and easily you can create a simple application that uses Groovlets on Amazon's infrastructure via Eclipse. You don't need any hardware other than your development machine, nor do you have to pay licensing fees for any of the software you'll use. (You do have to pay a nominal fee for the hosting of your code on Amazon's infrastructure.)

Getting started with EC2

EC2 is cheap

Use of a "small instance" — an image for running simple applications that don't use a lot of space or memory — costs 10 cents an hour. Bandwidth costs amount to roughly 30 cents per gigabyte of back-and-forth traffic. And you can "rent" an IP address, which costs 1 cent an hour (but you reserve it for almost two days). Deploying and testing this article's application cost me about 60 cents. To put this into perspective, I spent about $3.50 on a coffee today.

To use EC2, you must create an Amazon Web Services (AWS) account. You'll need to provide a credit card number if you are not already a registered Amazon customer — EC2 isn't free (but signing up is). As soon as you sign up, you'll receive an Access Key ID and Secret Access Key, which are required for working securely with EC2.

To use the AWS plug-in for Eclipse, Amazon recommends you use Eclipse 3.5; otherwise, you'll need to install some prerequisites that are defaults in this latest Eclipse version. Because you're going to create and deploy a Web application, I recommend you use the Eclipse IDE for Java EE Developers, which has a nifty Java EE perspective that supports building Web applications.

Installing the AWS plug-in suite

To install Amazon's AWS plug-in (like any other Eclipse plug-in), you must first configure Eclipse to pull down the latest version of the plug-in via Help > Install New Software. Once the Install dialog opens, enter in the Work with text box. A list of available plug-ins will appear in the section below the text box, as demonstrated in Figure 1. Select the entire AWS Toolkit for Eclipse option and then click OK. After you agree to the license, Eclipse installs the plug-in. You'll most likely need to restart Eclipse too.

Figure 1. Downloading the AWS plug-in
downloading the AWS plug-in

The Eclipse plug-in allows you to associate target runtimes for Web applications. For this article, you'll use Tomcat 6, so you need to download and install Tomcat 6 before moving forward. Even though you'll deploy your Web application to EC2, which will run a cluster of Tomcat servers, you still want to test and run things locally, where it's free.

Creating a Groovy Web application

Before I start demonstrating how to configure EC2, I'm going to create a simple Web application with my favorite language for rapid development: Groovy. As I demonstrated in "Hello Google App Engine," Groovy is essentially the Java language without a lot of the syntax (see Resources). For example, a typical Hello World servlet is roughly 6 to 10 lines of Java code (depending on whether you decide to count imports and so on). Regardless of how you decide to count lines of code, I can guarantee you that the same Hello World servlet written in Groovy — a Groovlet — is shorter; in fact, the simplest servlet you can write in Groovy consists of one line of code.

Using the Eclipse Java EE perspective, right-click in the project explorer and select New > Dynamic Web Project. If you haven't ever created one of these before, don't fret — Eclipse chooses to differentiate between static Web projects that use HTML pages and dynamic projects that have server-side resources such as servlets.

After you select the Dynamic Web Project option, you're presented with a dialog to configure the project's various facets, such as the target runtime and Servlet version. Make sure you select Tomcat 6. (You might have to tell Eclipse where you've installed Tomcat 6 too.) For the configuration option, leave that as the default.

You can select where source files live. I usually leave that as the src directory. Lastly, you have to tell Eclipse the context root — that is, the name that'll be used in the URI of your Web application. Make sure you pick a simple name here. And you need to pick a name for the Web content folder where static files and your Groovlets will live.

Click Finish, and you are good to go.

Put Groovy dependencies in it

In order to use Groovy, you must make two changes. First, add the groovy-all JAR (at the time of this writing, it is 1.6.4) into your project's classpath. And you need to update the web.xml file found in the WEB-INF directory. This file essentially maps Web requests to Groovlets — you did this if you followed along in the previous article too. You can see what the web.xml file should look like in Listing 1:

Listing 1. A web.xml file that supports Groovlets

Now you're ready to write something with Groovy.

Write something already!

The simplest Groovlet is a one-liner that literally prints Hello World using a println. You'll add a bit of 1999-vintage pizzazz and spice up the prosaic Hello World with some HTML. Groovy supports the notion of Builders, which are logical mechanisms for defining hierarchical data structures (see Resources). Groovy's Builders let you effortlessly mimic markup languages such as XML and HTML, and even GUIs with frameworks like Swing. You can quickly create HTML markup without having to deal with HTML itself (no brackets!). The simplest HTMLized Groovlet you can author leverages Groovy's MarkupBuilder, which in the case of a Groovlet is already present in the form of the html variable. For instance, Listing 2 shows a simple Hello World Groovlet that uses HTML:

Listing 2. A super-slick Groovlet
html.html {
  head {
      title("Hello from EC2")
  body {
    p("Hello from EC2!")

That's it. These few lines of code yield well-formatted HTML that has a <head> element followed by a <title> and then a <body> element with a single paragraph — utterly simple. Create a file called EC2Groovlet.groovy and put the code in Listing 2 in it. Save this file in your Web content directory too.

Want to create XML? It's just as easy. Don't want to mix your view with your controller? No problem, use a GroovyServer Page (GSP) or a JavaServer Page (JSP) if you'd like. Regardless of your desired direction, I hope you see that Groovy can make authoring servlets a breeze. Now watch how EC2 makes using servlets a breeze too.

As a final step, create a simple index.html file in your project's context root. This file will be handy later on because when you fire up EC2 in Eclipse, the URL Eclipse brings up is the context root; if no index.html file is found, you'll get an error page from Tomcat. In index.html, create a relative link to your Groovlet.

For instance, my index.html looks the one in Listing 3:

Listing 3. Simple index.html that points to my Groovlet
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Insert any old title here!</title>
  <p><a href="EC2Groovlet.groovy">Groovy is slick!</a></p>

The easy work is done. Now it's time to get rolling with EC2.

Configure EC2

EC2 is fairly straightforward to configure via the Eclipse plug-in. First, open the Amazon EC2 perspective and enter your credentials. Next, open Window > View > Servers. You need to define at least one server, but you'll define two: one locally for free testing and another on Amazon's infrastructure. Accordingly, right-click and select New > Server.

Type local in the Select the server type text box. You'll see a few choices (as shown in Figure 2). Select the Tomcat v6.0 Server option (provided you've already downloaded Tomcat and properly configured your runtime configuration in Eclipse, following the instructions under Getting started with EC2). Click Next, and you'll have the option of adding available resources (that is, projects) to this instance. Select your project and click the Add button. Click Finish, and you're good to go. You can now automatically publish your Web project to a local instance of Tomcat.

Figure 2. Defining a local server
Defining a local server

In the Servers console, select the local Tomcat instance, right-click, choose Start. Once Tomcat has started, Eclipse will deploy your code (that is, your snazzy Groovlet). Fire up your favorite browser to localhost:8080/your context name/EC2Groovlet.groovy and you should see the splendor of a Groovy-Builder-HTMLized Hello World. If you do, you're ready to try it out on Amazon's infrastructure.

Deploy + test with AWS

By now, I hope you're biting at the bit to get this show deployed on EC2. The good news is that it's about as easy as deploying locally. First, go back to the Servers console, right-click, and select New. From here, type ec2, and you'll see the Amazon EC2 Tomcat v6.0 Cluster option shown in Figure 3. Select it and click Next.

Figure 3. Defining an EC2 server
Defining an EC2 server

In the next dialog, shown in Figure 4, you can select the number of instances you'd like your cluster to leverage. For now, stick with 1 — you can always scale up later. You can pick a custom Amazon Machine Image (AMI) or take the default. For now, use the default; suffice it to say, selecting another AMI provides a wealth of opportunities to add additional packages such as a database. For instance type, select Small — you can get bigger if you'd like (just remember, you'll have to pay for it).

Figure 4. EC2 preferences
Defining EC2 preferences

Amazon has indicated that it is serious about security, and the next two options (also shown in Figure 4) relate to securing your Tomcat cluster. For now, just create default aspects by right-clicking in the Key Pair area and selecting New Key Pair. You'll have to define a name and pick the key pair you should have already installed in the process of signing up for EC2.

Next, in the Security Group box, you can create groups. (You just create them here; in another view, you can actually do something with them.) Right-click and add a new one, calling it whatever you like. The default settings allow anyone to hit port 8080, which is just fine for now.

Lastly, Amazon allows you to configure IP addresses. This will become handy if you end up using EC2, because the default URI for your Web application will be quite long and hard to remember. For now just stick with the address EC2 provides. But don't worry — you can always configure your custom domain name to map to an IP address that Amazon provides.

Click the Next button and associate your project (with the Groovlet) with your EC2 server. You're ready to deploy!

Just as before, right-click on your newly defined Amazon EC2 server and start it, as shown in Figure 5:

Figure 5. Starting and stopping servers
starting servers

Starting might take a few extra moments this time because things are happening remotely. Once your server is running, select your Web project and the Run as > Run on Server option. Next, select your already started Amazon Tomcat cluster and click Finish. Doing so will fire up an embedded browser in Eclipse that defaults to your context root — where you've defined that index.html file with a link to your Groovlet.

Note the URL in the browser location window. You can see mine in Figure 6. You can always map the URL to something more logical. Click the link to your Groovlet and witness the wonder of Groovy all over again — this time from some server about whose location you haven't the slightest clue!

Figure 6. EC2 is Groovy!
EC2 is Groovy

Once you've clicked the link a few times and satisfied yourself that running on Amazon's infrastructure was about as easy as running on your local machine, be sure to stop your server — if you don't, you'll be billed. Stopping, logically, is the same as starting. Just right-click on your instance in the Server console and select Stop.

EC2 is Java development 2.0

Java development 2.0 is largely influenced by two key factors:

  • Fully capitalizing on open source tools and frameworks for building applications top to bottom
  • Renting (or borrowing) the application infrastructures required at all levels for managing the software life cycle, including running the applications themselves

As you can see from this article, using EC2 brings both aspects of Java development 2.0 into play. You can fully leverage any software package you'd like (remember, Amazon's virtual machines can run anything you can run) on a borrowed infrastructure that costs far less than acquiring the hardware resources yourself. What's more, EC2 is quite efficient to set up and can scale far beyond what you can do on your own for the same cost.

The next Java development 2.0 installment will take a deeper dive into EC2 by provisioning a virtual machine (or AMI) that supports running Tomcat as well as MySQL. And you'll learn how to deploy a Java Web application that supports both Spring and Hibernate — arguably the two most popular Java open source frameworks out there today — and once again you'll see the fundamental facets of Java development 2.0 in action.



Get products and technologies



developerWorks: Sign in

Required fields are indicated with an asterisk (*).

Need an IBM ID?
Forgot your IBM ID?

Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.


All information submitted is secure.

Dig deeper into Java technology on developerWorks

Zone=Java technology, Cloud computing
ArticleTitle=Java development 2.0: You can borrow EC2 too