Friday, August 12, 2011

Creating a server instance and deploying a Java app on Amazon's EC2 Cloud

My previous blog post detailed the process by which I chose to host my web application on the Amazons EC2 Cloud using the Bitnami Tomcat Stack AMI.  This post will describe some of the steps I followed when creating the server instance, connecting to it and deploying a Java .war.

Connecting to your Amazon EC2 server instance

There's no need for me to write step-by-step directions for creating the instance since Bitnami has already done a great job in providing instructions.  It's quite simple and I didn't run into any issues creating the instance.

Don't forget to open the SSH and HTTP ports for your server instance using the Security Groups function of the AWS Management Console.  Once you open port 80 you will be able to access the default Tomcat web page in your browser via your instance's public DNS name (ie. http://ec2-107-20-221-246.compute-1.amazonaws.com).

Connecting to the new server instance is a little trickier, particularly for someone who isn't exactly a Unix/Linux guru.  However, it's still relatively simple and again Bitnami has provided detailed instructions.

They key to connecting to your instance is the key.  :) Well, key pairs to be exact.  It's not just the key pair, you'll also need an SSH client like PuTTy.  You will be prompted to create and download the key when you create your first EC2 server instance.  Keep this file secure and do not lose it.

Here's the part that tripped me up: the key you download from AWS is not compatible with the PuTTy SSH client.  AWS provides a key in .pem format and you'll need to convert it to the .ppk format that PuTTy can use.  This excellent blog post provides details and instructions for converting the key and connecting with PuTTy.

Configuring MySQL

My next step was to access MySQL and create my database schema. I installed Navicat Lite on my desktop and configured it as described by Bitnami here.  This worked like a charm and it wasn't long before I had remotely logged into MySQL running on my server instance on EC2.  I  ran SQL commands to create a database schema, user and grant privileges.  I then imported a .sql file with the DDL I had exported from my local database using MySQL's "mysqldump" tool.

Deploying a .war file to Tomcat

I installed the WinSCP FTP client on my development machine and configured it to connect to my server instance.  Then I FTP'd my .war file to the "/opt/bitami/apache-tomcat/webapps" directory and voila!  My web application was now running on Amazon's EC2.

9 comments:

  1. Thanks for the nice article on our stack, just tweeted about it.

    ReplyDelete
    Replies
    1. Hey Daniel thanks for making the stack!

      Delete
  2. Justin: I had a question for you on your StackOverflow post regarding "How to return a partial JSON response using Java?". What would be the best way for me to ask this question - clearly it doesn't belong in this comment thread.

    Thanks so much - and sorry about the intrusion!
    RB

    ReplyDelete
  3. Richard, if it's related to the SO post you can add a comment there. Otherwise, you can email me direct justinhrobbins at gmail.

    ReplyDelete
  4. Hi,
    First of all thanks lot for posting this information. It saved me a lot of headache.

    I was able to install the bitnami tomcat stack onto amazon ec2 and the instance is running.
    But I am having trouble connecting to the instance from my windows machine using putty.

    I followed all the steps provided by the bitnami guide but I keep getting the following error when I try to open a connection with the instance.

    server has refused our key along with a popup saying Disconnected:No supported authentication methods available(Server sent: publickey)

    Could you please help me with this. It would be greatly appreciated.
    Thanks

    ReplyDelete
  5. well described, succinct and well referred.

    ReplyDelete
  6. Please let me know how to get hold of the below password?
    " password: "

    ReplyDelete
  7. Hi,

    I have deployed web app on AWS EC2 server (ubuntu) after successful deployment,i got the database connection.I am using MySQL database which is created by RDS where i am define database host name ,I am able to get connection when run that application from eclipse but after deployment i got some error:

    1. com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:
    2.org.hibernate.exception.JDBCConnectionException: Cannot open connection

    Please give me any solutions !!!!

    Thanks
    Vaibhav

    ReplyDelete
  8. Great blog... I found complete information on how to create AWS EC2 instance and for backup AWS backup EC2 instance is very helpful.

    ReplyDelete