Peter Friese

Developer Advocate / Mobile Developer / Public Speaker

OSGi & Servlets: Deploying on Amazon EC2

| Comments

Last week, I showed you how to create a very simple OSGi-based servlet and run it locally on Jetty. Today, I will show you how to deploy this servlet to an Amazon EC2 instance.

We will first set up an Amazon EC2 instance and then export the servlet and install it in this instance.

Setting up an Amazon EC2 instance

Scott Lewis was kind enough to prepare an Amazon EC2 Machine Image (AMI) that already contains Jetty, Equinox and p2 – everything we need to get our servlet running. So let’s use this AMI to provision our instance:

  1. Sign in to the AWS web console

  2. Click on Launch instance to create a new instance

  3. In the Request Instances Wizard, jump over to the Community AMIs tab

  4. Type OSGi in the search field to search for ami-69d93600

  5. Select the AMI

  6. In the next step, you get to configure the instance. A small instance will do for now.

  7. Leave the Advanced Instance options as-is

  8. Next, you need to create a key pair (this will allow you to sign in to the instance using SSH). If you haven’t created an EC2 instance before, select Create a new Key Pair and follow the instructions. Otherwise, select an existing key pair

  9. To access the instance from outside using HTTP and SSH, you need to create and assign a security group. Make sure to add SSH and HTTP port configurations to this security group. Unfortunately, it is not possible to freely choose the port ranges, so we need to edit the security group after we’re finished with the wizard.

  10. . On the summary page, review the configuration and click on Launch to actually start your instance.

After a little while, the newly created instance will show up in the list of AMIs. You can test your instance by opening a terminal window and SSH’ing to your instance:

ssh -i path/to/your/privatekey.pem

You should be greeted with a prompt like this:

        __|  __|_  )  Fedora 8
        _|  (     /    32-bit

Welcome to an EC2 Public Image
                      :- )

Start the EclipseRT server as follows:

cd /env/osgi_eclipsert36_sb_p2_jetty_7_0_1_linux_x86/jetty-distribution-7.0.1.v20091125/

Your Eclipse RT Equinox server is now up and running. Time to export the servlet!

Exporting the servlet

We will export our simple servlet using a feature based update site, so it can be installed using p2.

First, create a new feature project, naming it simple.servlet.feature. Provide the following details to the wizard:

  1. ID: simple.servlet.feature

  2. Version: 1.0.0.qualifier

  3. Name: Simple Servlet Feature

  4. Provider: Your name

Open the Plug-ins page and add simple.servlet to the list of packages plug-ins.

Now, we can create the update site. It will only contain the feature we just created. Using the New Update Site wizard, create a new update site project, naming it simple.servlet.updatesite. Add the feature we just created to the list of features on the first page of the site.xml Update Site Map.

When you’re done with that, , you can build the update site by pressing the Build All button. This will build the servlet plug-in, the feature and finally the update site. After that, we’re ready to deploy.

Installing the servlet

Installing the servlet into our Amazon EC2 instance is quite easy, as we can leverage p2:

Using your favourite SFTP client, copy the entire update into the /tmp directory of your Amazon EC2 instance. When that’s done, hop over to your terminal window that’s connected to the OSGi console of your Eclipse RT server (remember, we started this server when setting up the Amazon EC2 instance). Issue the following commands:

provaddrepo file:/tmp/simple.servlet.updatesite
provinstall simple.servlet

Make sure you use the correct version identifier, it is likely to be different from the one I used here!

You can now list the installed bundles by invoking ss:

38      ACTIVE      org.eclipse.equinox.preferences_3.3.0.v20100208
39      ACTIVE      org.eclipse.equinox.registry_3.5.0.v20100301
40      ACTIVE      org.eclipse.equinox.security_1.0.200.v20100301
41      ACTIVE      org.eclipse.equinox.server.examples.hello_1.0.0.201003111015
42      ACTIVE      org.eclipse.equinox.simpleconfigurator.manipulator_2.0.0.v20100304
43      ACTIVE      org.eclipse.equinox.util_1.0.100.v20090520-1800
44      RESOLVED    org.eclipse.osgi.services_3.2.100.v20100108
45      RESOLVED    org.sat4j.core_2.2.0.v20100225
46      RESOLVED    org.sat4j.pb_2.2.0.v20100225
47      RESOLVED    simple.servlet_1.0.0.201004081743

Start the servlet by typing start 47 (again, please use the bundle ID issued by ss).

Open a webbrowser and navigate to (the DNS address is the same you used to SSH to your instance) and you should see the servlet output:

Hello from the cloud!


In this post, you saw how easy it is to create an Amazon EC2 instance and deploy an OSGi-based servlet on it. With this knowledge, you can now start sky-diving into the joys of cloud computing. Have fun!

Most of this information has been taken from the Eclipse Wiki. You are encouraged to add your own tips & tricks to this wiki page. All you need is an Eclipse Bugzilla account. Setting one up is easy, start here.