Today I took the time to go through the first steps using the Amazon Elastic Computing Cloud (= EC2) with a fairly simple dynamic web app. Nevertheless, I did not want to see what complexity I can put into a web application - this depends more on what you actually want to do - but see how the deployment procedure, infrastructure setup and everything related to it would work out.
And I must say, I am impressed with how easy it really is. Nearly no difference to using a local Tomcat installation from within Eclipse. But one step at a time, here's what I did:
Preconditions- a 1.6.* JDK from Sun
- a local Tomcat installation (I am using 6.0.20, the current latest stable)
- Eclipse JEE edition, Galileo (3.5+)
- an existing Amazon Web Services (AWS) account
The existing account with AWS is necessary because the initial sign-up process takes a while. I had to wait about 18 hours until my subscription was actually performed. I guess they have to propagate the credentials through their internal systems - and that is what makes it slow.
Steps in short- Install the AWS Toolkit for Eclipse using
their update site- Provide the account credentials in the Eclipse settings (account number, access key id, access secret)
- Create a dynamic web project using the Eclipse project wizard
- Create a servlet for the web project, also using the Eclipse wizard
- Edit the servlet (details below)
- Deploy to local Tomcat for testing, verify functionality
- Create EC2 Tomcat instance in Eclipse's "Server" view
- Deploy to EC2, test on remote host
Steps in detailInstallation of the AWS Toolkit..
..is self-explanatory if you have used the Eclipse Update Manager before. In case of questions, you can find a more detailed explanation
at the Toolkit site.
Providing the AWS account credentials..
..is relatively easy: Just navigate to the proper section within Window > Preferences > AWS and enter your account ID, the key ID and the secret access key. You can find all of these details on your
Security Credentials Page.
Create a dynamic web project..
..and a servlet within using the Eclipse wizards. This should not be that difficult since Eclipse does all the work for you like editing the deployment descriptor, provide a servlet mapping and the likes. I created a *very* simple servlet which takes to numeric parameters a and b and shows the addition result in the response. Here's the source code for the doGet() method, in case you are interested:
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = new PrintWriter(response.getOutputStream());
String a = request.getParameter("a");
String b = request.getParameter("b");
try {
int x = Integer.parseInt(a), y = Integer.parseInt(b);
out.println(a + " + " + b + " = " + (x + y));
} catch (NumberFormatException e) {
out.println("Invalid numbers (a=" + a + "b=" + b + ")!");
} finally {
out.close();
}
}
Deploying to the local Tomcat..
..assumes you already have your runtime configured properly. This means, Eclipse knows about where to find your Tomcat installation directory, knows which version of Tomcat it is etc. In case you have done all the configuration stuff, it should just be a matter of rightclicking your project in the Project Explorer (the Package Explorer or the Navigator should work, too) and choosing "Run As.." > "Run on server". You will be prompted to choose the server runtime you wish to use. After having chosen the local Tomcat installation, Eclipse should show the server startup in the "Servers" view (which can be opened with "Window > Show View > Other.. > Servers" in case you do not have it opened already) and automatically fire up your browser with the proper URL. In my case this was
http://localhost:8080/aws1/
which did not work out since I do not have any start pages, so my Tomcat presents me with a simple 404 in this case. Nevertheless I know where my Servlet is being mapped, so navigating to
http://localhost:8080/aws1/AdditionServlet
does the trick. I see the output telling me that a and b are invalid numbers (since they are null for that matter).
I just added the two parameters to the URL
http://localhost:8080/aws1/AdditionServlet?a=37&b=5
and get the nice result "37 + 5 = 42". Who would've thought - the answer to the Universe, Life and Everything[tm].
Create the EC2 Tomcat Instance..
..is as easy as setting up a local server instance. Just right-click the "Servers" view inside Eclipse, choose the EC2 Tomcat which is provided within the resulting dialog and fill in all the settings as described
in the original tutorial.
After having set up the EC2 instance, just use the same procedure as before in order to start the server and deploy the web app ("Run As.. > Run on server", remember?). And voila - the web app runs on the remote host.
That's it. Fairly simple, isn't it?
Btw, Amazon will charge you for every started hour a full hour's rate (which is, for the eu-west-1 region), about $0.15 - but see
the price listings for details. You have to provide valid credit card information in order to sign up for AWS and my test runs today amount up to about EUR 0.25.