Deploy your Java Application with EC2

This article explains how to deploy your Java application with the Amazon EC2 service. Because Amazon gives 1 year free tier, you can freely deploy your application for one year. In this tutorial I would like to show you how to deploy Java application (.war) with tomcat server.

Step 1

First of all you must create an AWS account. After you create the account you will be able to try free tier for one year. (Please read the free tier conditions)

Step 2

step 2.1

Then login to your account and go to Services -> EC2. Then go to the INSTANCES -> Instances In the left menu bar.

Image for post
Image for post

step 2.2

Then go to launch Instance and choose “Ubuntu Server 16.04 LTS (HVM), SSD Volume Type — ami-80861296”. I am using this AMI because I am familiar with Ubuntu.

Image for post
Image for post
Ubuntu Server 16.04 LTS AMI

step 2.3

Use the free tier t2.micro machine for testing. Otherwise, you will be charged.

Image for post
Image for post
Free tier eligible machine

Step 2.4

You should create a new key pair and Download the Key Pair before the next step. I have created a key pair named “aTest” and when I download the Key Pair, It will download a “aTest.pem” file. This file is really important because without it, we will not be able to SSH to the EC2 machine.

Image for post
Image for post
New Key Pair Creation

Step 2.5

Launch the instance and if all the things are correctly done, you will see a new instance have been created.

Image for post
Image for post

Step 2.6

Wait until Status Checks finish its initialization. After some time status Checks will look like this.

Image for post
Image for post
Status Checks after few minutes

Now your EC2 Instance Lunching part is done. Let’s go to the next steps.

Step 3

Now you have to realize that you have a virtual instance and you can do whatever you want with it. First before you do anything with the instance you have login to the machine. For that you can use SSH. When you select your instance and click the connect button above the table you will see ab window like this.

Image for post
Image for post

Here you have to follow their guideline and SSH to the your instance. If you are using Linux then you can easily use SSH from the terminal. But if you are using windows then you have to use tools like “PuTTY”.

Image for post
Image for post
Following the SSH Connecting guideline

Step 4 — Installing Java

If the all the steps are successfully done, you should be able to login to the your ec2 instance right now.

Now we have to install Java for the EC2 instance. There are several ways to install Java for Ubuntu. But I am using the method described in the following link.

There are some changes you have to do, please read the instructions below the link, before you follow the link instructions.

This link provides how to install the Java on a local machine. Because we are installing it on EC2 instance, after the 4th step of that link ( 4 Download the Oracle Java JDK/JRE for Linux) we have to go through an additional step.

After Downloading the Oracle JDK you have to SCP it to the EC2 machine. Use following command to copy data from your local machine to ec2 instance.

scp -i aTest.pem jdk_tar.gz :/home/ubuntu

cp /home/ubuntu/jdk_tar.gz ~/Downloads/

Then follow the step 5 to 12 at the link.

Step 5

Now you should have installed Java to the machine successfully. Now we have to install tomcat to the machine. Find the latest stable tomcat version and download it’s tar.gz file. Same as above step, transfer the file using the SCP command.

scp -i aTest.pem apache-tomcat-9.0.0.M10.tar.gz :/home/ubuntu

Now extract this file to /opt/. To extract this file use following command.

tar xf apache-tomcat-9.0.0.M10.tar.gz -C /opt/

so now you have successfully completed the step 5.

Step 6

Now what you have to do is transfer your Java Application (.war) to the ec2 machine. To do that, use the SCP command again. The command will be like this.

scp -i aTest.pem testAPP-1.0-SNAPSHOT.war ubuntu@ec2– testAPP-1.0-SNAPSHOT.war

Now you have to copy your .war file to webapp folder. Use following command with suitable changes.

cp /home/ubuntu/testAPP-1.0-SNAPSHOT.war /opt/apache-tomcat-9.0.0.M10/webapps/

Now step 6 is completed.

Step 7

So now you only have to do is, starting the tomcat server. To do that, go to bin folder of the tomcat installation.

cd /opt/apache-tomcat-9.0.0.M10/bin

To start the server

./ start

To stop the server

./ stop

When you start the server, you will be able access your web application through your ec2 machine public DNS.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade
A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store