Karmona Pragmatic Blog

Don't get overconfident… Tiny minds also think alike

Karmona Pragmatic Blog

Run “Hello World!!!1” Servlet on EC2 using AWS Toolkit for Eclipse

September 15th, 2010 by Moti Karmona | מוטי קרמונה · 12 Comments

It started like yetanother-weekend-experiment but once you start a weekend experiment you never know when or how it will end… ;)

I was very curios to take AWS for a quick test drive so I lost six hours of a precious beauty sleep and compiled this blog-post-capsule for future generations.

 

 

The Quest

* Run an “Hello World!!!1” Servlet on EC2 (less than $0.01 per hour)
Create a local development environment using EclipseAWS Toolkit for Eclipse (seems like a really interesting toolchain)

Preliminary Steps

Issues with AWS Toolkit defaults

The plan was to use this tutorial but surprisingly enough this did not work out-of-the-box (apparently due to Tomcat/JDK versions on the default AMI the plug-in is using but I didn’t waste time in making sure this is the issue) so I moved to plan B

 

Plan B – Create a custom EC2 AMI with Tomcat 6.something and JDK 1.6

* Launch an EC2 instance using Amazon’s ami-84db39ed AMI.  (basic Fedora 8 image)
* Use Putty connect to your instance

 

* Install Java on EC2 Instance

Download JDK (“Linux RPM in self-extracting JDK file”)

mkdir /usr/local/java
cd /usr/local/java
curl http://download.java.net/jdk6/6u23/promoted/b01/binaries/jdk-6u23-ea-bin-b01-linux-i586-30_aug_2010-rpm.bin > jdk1.6.0.23-rpm.bin
* Install the JDK
chmod 755 jdk1.6.0.23-rpm.bin # Change the permission of the file
./jdk1.6.0.23-rpm.bin #Install
updatedb; locate javac | grep bin  # this step merely serves to verify the installation
/usr/sbin/alternatives –install /usr/bin/java java /usr/java/jdk1.6.0_23/bin/java 100
/usr/sbin/alternatives –install /usr/bin/jar jar /usr/java/jdk1.6.0_23/bin/jar 100
/usr/sbin/alternatives –install /usr/bin/javac javac /usr/java/jdk1.6.0_23/bin/javac 100
/usr/sbin/alternatives –config java # Change the default JVM from gcj to Sun’s version (if needed)

* Install Tomcat on EC2 Instance

* Download Tomcat 6.0
mkdir /usr/tomcat
cd /usr/tomcat
curl http://apache.spd.co.il//tomcat/tomcat-6/v6.0.29/bin/apache-tomcat-6.0.29.tar.gz > apache-tomcat-6.0.29.tar.gz
tar zxvf apache-tomcat-6.0.29.tar.gz
* Start Tomcat and to verify the installation, load the root page from a web browser: http://your_instance_name:8080
cd apache-tomcat-6.0.29
bin/startup.sh  # Start Tomcat
* Configure Tomcat to launch automatically – Create a file “/etc/rc.d/init.d/tomcat” with the following content:
#!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.
JAVA_HOME=/usr/java/jdk1.6.0_23
CATALINA_HOME=/usr/tomcat/apache-tomcat-6.0.29
export JAVA_HOME CATALINA_HOME
exec $CATALINA_HOME/bin/catalina.sh $*
* Set the proper permissions for your init script and enable Tomcat for auto-launch:
chmod 755 /etc/rc.d/init.d/tomcat
chkconfig –level 2345 tomcat on
* Tomcat should now be automatically launched whenever your server restarts.

 

Are we there yet?

It could be but apparently the plug-in was poorly designed to use none-configurable command lines so I needed to add the following “tricks”:
* Set JAVA_HOME / PATH for your user – Login to your account and change your .bash_profile file
vi ~/.bash_profile
export JAVA_HOME=/usr/java/jdk1.6.0_23
export CATALINA_HOME=/usr/tomcat/apache-tomcat-6.0.29
* Create aliases to your Tomcat and JDK installation (these location are hard-coded in the plug-in)
ln -s /usr/java/jdk1.6.0_23/ /env/jdk
ln -s /usr/tomcat/apache-tomcat-6.0.29/ /env/tomcat
The EC2 instance is ready :)

 

What next?

* Create EBS Image AMI from your instance (it does takes couple of minutes to complete)
* Open your eclipse and start a new AWS project as described in the original link
* Define a new EC2 Server in Eclipse using your new AMI (reminder: the default didn’t work)
* Create your “Hello World!!!1” Servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println(“Hello World!!!1”);
}
* Click Run… this will automatically deploy your Servlet and run it on the remote EC2 instance… Wow :)

 

That’s it – I hope this will help, it does take approx. 1 hour so if you know some other way to make it work, please don’t be shy and comment.

Additional references I used to make it this far:

* Amazon EC2 – Getting Started Guide

 

Free VI Tip for Dummies

80% of knowing VI is just: ESC ESC ESC, i, Type-Your-Stuff, ESC ESC ESC, :wq!

 

 

Tags: Cloud · Development · Simplicity · Tools

12 responses so far ↓

  • 1  Tzvika // Sep 15, 2010 at 6:57 pm

    Dude you need to switch to app engine pronto

  • 2  Moti Karmona | מוטי קרמונה // Sep 15, 2010 at 8:18 pm

    Why?

  • 3  Tzvika // Sep 15, 2010 at 8:28 pm

    Because it’s cheaper at scale

  • 4  Moti Karmona | מוטי קרמונה // Sep 15, 2010 at 8:37 pm

    * Cool – Where exactly can I see updated price list for Google App Engine?
    * Are there still limits on response time as I remember it had before?

  • 5  Tzvika // Sep 15, 2010 at 11:52 pm

    http://code.google.com/appengine/business/

    also for quotas see: http://code.google.com/appengine/docs/quotas.html

  • 6  Ed // Sep 16, 2010 at 9:19 pm

    The example for the Tomcat URL is not correct

    You say: http://your_instance_name:8080

    Should be: http://:8080

    The instance name and public DNS are two different things.

  • 7  Moti Karmona | מוטי קרמונה // Sep 16, 2010 at 9:39 pm

    @Ed, Thank for the fix! – You are more than right.
    It should be: http://your_public_dns:8080

    — Moti

  • 8  Yan Virin // Sep 17, 2010 at 12:22 pm

    Well, you see that putting something in the “cloud” is not just yet a simple thing to do… how about running complex calculations on Terra’s of data? Not simple… Needs a strong infrastructure and a lot of nerves to restart, re-run, looking at logs, swearing and sweating when it does not work, scripting and more scripting and then debugging.

    But, it is the future :)

  • 9  Todd // Sep 10, 2011 at 3:03 pm

    This all worked perfectly until:
    ln -s /usr/java/jdk1.6.0_23/ /env/jdk

    which threw the error ln: creating symbolic link `/env/jdk’: No such file or directory

    I’m using the exact ami on ec2, and followed the steps exactly.

    Can you help?

  • 10  Moti Karmona | מוטי קרמונה // Sep 10, 2011 at 8:21 pm

    Hi Todd,

    I can try :)

    Please make sure the destination folder already exist (in this case: /env/jdk) – Create it if needed using mkdir

    Please update me if you need any further help.

  • 11  Matthew Cornell // Dec 6, 2011 at 5:31 pm

    Thanks for this! I’ve been resisting Eclipse because I love IntelliJ IDEA so much, but all those plugins are hard to beat…

  • 12  Noor Kaloon // Dec 9, 2011 at 6:00 am

    I have to thank you for such excellent piece of important work..

    I specifically needed the part for starting Tomcat automatically when the instance starts (in my case I started jboss AS)

    An inquiry:

    I’ve enabled AccessLogValves in my jboss server to info about requests. When I ssh to the instance that started my jboss automatically, I can’t find the log file in /home/ec2-user/jboss/server/default/log directory. Even the server logs are not there.

    Where the log file has gone? any pointers? I tested my instance without automatic start of jboss and I found the log file in that directory

    The JAVA_HOME & JBOSS_HOME I used are set to /home/ec2-user/jdk.. /home/ec2-user/jboss..

    Look forward for some pointers to access the log files

Leave a Comment

Allowed tags <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

intersoluble-mortarless