Java HelloWorld @ the Cloud with Amazon EC2

This post is about how to run a simple java application installed in one of your Amazon AMI’s

Thanks to the GridGain guys who inspired me when I read this page about running GridGain in Amazon EC2

I am going to assume that you know how to create an Amazon AMI. For information on how to create one, please look here it.

In my scenario I took one of the Ubuntu 8.04 images, and I created a user helloworld as I do not want to run the process as root.

Once you have logged in your running instance follow the steps.

Step 1

I assume you have created your helloworld user.

su - helloworld

Step 2
Create your HelloWorld.java file

vi HelloWorld.java

Then type your java favorite source code ever.


public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("[Data passed to the AMI instance:] "+System.getProperty("userData"));
    }

}

You can see in the source code that I am reading a system property userData. This is because we are going to send that data to the running EC2 instance at boot time.

Then compile the code

javac HelloWorld.java

And test it. You should get a null as output. This is because we have not defined the property -DuserData

java HelloWorld

Step 3
Create your personalized AMI

If you want to run a java process at boot time. Before creating the AMI, opne the /etc/rc.local and add at the end the command that executes your java process

echo "Running java process " > /tmp/rc.local.log
su - helloworld /home/helloworld/runHelloWorld.sh

The runHelloWorld.sh might look like this


#!/bin/bash
clear
export JAVA_HOME=${HOME}/software/jdk1.6.0_14
export PATH=${JAVA_HOME}/bin:${PATH}
export USER_DATA=`GET http://169.254.169.254/2007-03-01/user-data`
echo ">>> [USER_DATA] >>> "${USER_DATA}
echo
java ${USER_DATA} HelloWorld >> ${LOG_FILE} 2>&1

As you see, I am calling an Amazon web service to get the data I have passed to the AMI. That data is an string with the JMV args. So what I am going to pass is the string “-DuserData=Amazon_says_Hello_World”
passing the user data to the JVM as arguments.

Step 4
Once you have your AMI ready and registered, you only have to run it.

ec2-run-instances ${MY_AMI} -n ${NODES} -K ${EC2_PRIVATE_KEY} -C ${EC2_CERT} -g ${MY_SECURITY_GROUP} -z ${ZONE}  -t ${INSTANCE_TYPE} -d "-DuserData=\"Amazon_says_Hello_World\""

This line runs the instance in the Amazon Cloud. If you log in into the running instance and changes the user from root to helloworld. You can check the log file to see that the HelloWorld was executed. But you can also run it manually just by executing the runHelloWorld.sh script which you created before.

Even you can query the amazon web service. You only have to type

GET http://169.254.169.254/2007-03-01/user-data

You should see at the prompt “-DuserData=Amazon_says_Hello_World”
If you run the helloWorld script you will see “[Data passed to the AMI instance:] Amazon_says_Hello_World”

Amazon allow you also to pass files at the ec2-run-instances so you could implement many ways of passing data, or configuration arguments to your processes.

Even if you have installed subversion you could check out for the latest configuration file of your process. You could ftp a server, etc… It is up to you the way you do it.

With this approach of passing an String, you can face some problems. For instance if you pass several JVM arguments such
“-Xms4g\${IFS}-Xmx7g\${IFS}-XX:MaxPermSize=512M\${IFS}-Xnoclassgc”
you cannot put a white space between the different arguments because the ec2-run-instances command thinks that those are also parameters for him. The work around for this, only if you want to pass the data as an String, is to use the ${IFS} variable, which by default is the white space. Then in the script that runs the HelloWorld class, aster calling the web service for the data you have to add an extra line.

USER_DATA=`eval "echo ${USER_DATA}"`

This makes the $IFS to be evaluated, but this approach is a bit tricky and weird ;)

4 Comments on “Java HelloWorld @ the Cloud with Amazon EC2”

  1. #1 Rudy Gunawan
    on Apr 19th, 2010 at 7:50 pm

    Hi,

    This is great ! However, would you happen to know how to do it in C since I am planning to run MCSim (written and compiled in C) in Amazon EC2 cloud ? Thank you very much !

    Regards,

    Rudy

  2. #2 Alfonso Olias
    on Apr 20th, 2010 at 1:36 pm

    You can use everything as it is in the scripts. The only part is the C you have to run.

    The getenv() function at stdlib.h returns you the env variables. So you would be able to read any environment variable you set up before running your C code.

    check the stdlib.h

  3. #3 Francesco
    on Aug 25th, 2010 at 2:17 pm

    Hi Alfonso,
    nice piece of work. I look around and did not found any articol for instantiating and de-instantiating an AMI so i build my own example in java.
    This is the link to the article:
    http://instrumentelem.sourceforge.net/wiki/index.php/Amazon_ec2

    Hope you will find it useful

    Talk soon,
    Francesco

  4. #4 Rabindra Singh
    on Jun 11th, 2011 at 2:21 pm

    Hi,
    Very much helpful material you have provided. Really, Thanks for this.

    I would like to provide a link that gives an example of complete implementation of web application on stax or amazon cloud.

    http://jksnu.blogspot.com/2011/02/java-on-cloud-stax-networks.html

    followings are the links that gives information about other cloud.

    http://jksnu.blogspot.com/2011/01/java-on-cloud-computing.html
    http://jksnu.blogspot.com/2011/01/java-on-cloud-google-app-engine.html

Leave a Comment