Monday 9 July 2012

Running the Jersey web service on AWS with Elastic Beanstalk

In a previous blog entry, I created a web-service using Jersey that calculates the area of various shapes. In this entry, I deploy the web-service to AWS using Elastic Beanstalk.

Running the Jersey web service on Elastic Beanstalk was fairly straight forward.  I followed the instructions for creating a new application.  However, I did come across an issue with the health check, that required some slight tweaking.

Health Check

Beanstalk will send a health check to your application.  By default, the application health check URL is the application root (e.g., http://myapp.elasticbeanstalk.com/) and it expects a response of 200 OK for the application to be considered healthy.

With my application, the root will return a 405 response.  However, Beanstalk will let you configure the health check url.  So, I decided to implement a health check method for Beanstalk to call.  I could have setup the health check url to call one of the existing methods (i.e. call the square method), but I figured a health check method would be better.  I added the following method to the "CalculateAreaResource" class

 @Path("/healthcheck")
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String healthCheck() {
        return Response.Status.OK.name();
    }
I then set the url health check in Elastic Beanstalk to "/calculate/area/healthcheck". However, my beanstalk environment failed to build with the error:
Your health check URL may be misconfigured. If your application does not respond to requests at http://calculate.elasticbeanstalk.com:80/calculate/area/healthcheck, modify the health check URL to a valid path.

Fixing the Health Check URL

I knew from running locally that http://localhost:8080/calculate/area/healthcheck worked, so I ssh'd into the ec2 instance.  Checking the tomcat log files and web-app directory, I noticed that my war was deployed as the ROOT web-app and not into the "calculate" context as I had been using locally.  (Note: the tomcat directory is in /opt/tomcat7)

I modified the url health check in the Elastic Beanstalk settings from "/calculate/area/healthcheck" to
"/area/healthcheck" and restarted the environment. It loaded properly and I could access my web service!

For details on how to ssh into your instance, please see the reference links below.

References:



3 comments:

  1. I cloned your project from github and imported into Eclipse. Tried:
    1 using the aws plugin to run in on the elastic beanstalk server;
    2 Maven install and upload the war to elastic beanstalk server.

    Both failed and get 404 error page not found.

    If I remove the Jersey servlet from web.xml, I can access index.jsp (the hello world page). So I think it is the Jersey servlet issue.

    Could you please help me out here?

    ReplyDelete
    Replies
    1. What URL are you using to access the app at? Do you get the 404 error when you run the app locally?

      Delete
  2. Thanks a lot your reference links are very useful top us keep updatingAWS Online Training Bangalore

    ReplyDelete