How to Deploy Spring Boot Application on AWS EC2

How to Deploy Spring Boot Application on AWS EC2
Sharing is Caring:

Dear Reader, In this post, I will help you deploy a spring boot application on AWS EC2 in a step by step manner. EC2 or Elastic Compute Cloud is nothing but the virtual server in AWS Cloud. So ideally we are gonna run our spring boot application in the AWS cloud.

If we talk about deployment of Spring Boot apps to any server, What do we do?

We install Java on the server, copy the jar file on to sever and simply run the jar file using java -jar jarname.

It’s that simple. 🙂

This post is going to be a bit long but interesting. So please fasten your seat belts and be ready for the joy ride 🙂

Steps to Deploy Spring Boot Application on AWS EC2

  1. Create a Spring Boot project using Spring Initializr
  2. Import the project into your favorite IDE
  3. Add a RestController to be able to test
  4. Test the application locally
  5. Prepare the final jar file
  6. Launch an EC2 Instance and keep the key pair handy
  7. Copy jar file to AWS EC2
  8. SSH into the EC2 instance and Install Java 1.8
  9. Run the Spring Boot Jar File on EC2
  10. Allow port 8080 on your Instance security group
  11. Test Your spring boot endpoint deployed on EC2

Step 1: Create a Spring Boot project using Spring Initializr

If you already have a working Spring Boot application, you can go to Step 5 directly. If not let’s create a new spring boot application together.

let’s go to sprint initilizer page and quickly bootstrap a simple spring boot web application.

Open https://start.spring.io/ and fill up details such as Group, Artifact, Name and dependency.

Deploy a Spring Boot Application to AWS Ec2

Note: Please not that I have added a single dependency Spring Web as you can see in above screenshot.

After filling up details and selecting web dependency , click on Generate

This will download the zipped project into your local system.

Step 2: Import the project into your favorite IDE

Unzip the downloaded project and import the project into your IDE. I will be importing to eclipse for this tutorial.

Click on File -> Import -> Maven -> Existing Maven Project

Click Next

Click on Browse and navigate to your unzipped project folder and select that. Selecting that should show the pom.xml file as shown in below screenshot.

Deploy a Spring Boot Project to AWS EC2 1

Click on Finish to the project into the IDE.

Step 3: Add a RestController to be able to test

Now, we have the project imported into the IDE. Let’s add a Rest Controller into our spring boot project so that we can test it.

Click on your project and create a controller class with endpoint of /health. We will use minimal code like below.

package com.cloudkatha.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

@GetMapping("/health")
public String health() {
  return "Hello & Welcome to CloudKatha !!!";
}
}

Step 4: Test the application locally

Before we test it, let’s build the project using-

mvn clean install

Once build is successful, let’s run the project locally.

Right click on the class annotated with @SpringBootApplication and Run as Java Application.

Our application is running fine locally and you can access it like localhost:8080/health

Step 5 : Prepare the final jar file

Our application is working perfectly in local environment. Let’s prepare the final jar file and keep it handy in a folder as we will need the jar file during the deployment.

Run maven build to package the application as a fat jar.

mvn clean install

Once your build is successful, you will find the jar file in target folder. Copy the jar file and keep it in your system at a convenient location.

Step 6 : Launch an EC2 Instance and keep the key pair handy

Before we deploy our spring boot app, we need to launch an EC2 instance. You can find a step by step tutorial to launch an instance below.

Launch an EC2 instance in AWS EC2 Step by Step

Note: Please keep the keypair handy in the same folder as jar file.

Step 7: Copy jar file to AWS EC2

Let’s keep the KeyPair and the Jar to be deployed into the same folder like below.

copy jar

Please note that If you are using a linux machine you can use scp command out of the box. But if you are using windows, It doesn’t come with scp client and you must install one before you can copy file to EC2.

See: How to Copy a File into EC2 Instance from Windows Machine

So, If you are a linux user, continue below steps

Open terminal and navigate to directory where you have kept KeyPair and Jar File.

I generally use Cloud9 IDE with Linux instance for development so I will use below command to copy my files using SCP.

Navigate to folder containing your jar and keypair

cd /path/to/folder

Prepare the copy command as per below syntax

scp -i ./DemoKeyPair.pem . <username>@<public-ip or DNS>:/pathwhere/you/needto/copy

After putting the details, command result like below.

scp -i ./DemoKeyPair.pem ./demo-0.0.1-SNAPSHOT.jar ec2-user@ec2-34-240-45-168.eu-west-1.compute.amazonaws.com:~

Please note that before you can run above command you must set the permissions of your private key file so that only you can read it. If you don’t set the permission then you can not connect to your instance using keypair.

chmod 400 DemoKeyPair.pem

Type above command in your terminal and hit enter.

Now permission on your private key is set and you can run your copy command.

scp -i ./DemoKeyPair.pem ./demo-0.0.1-SNAPSHOT.jar ec2-user@ec2-34-240-45-168.eu-west-1.compute.amazonaws.com:~
Home Directory has the jar file uploaded now

As confirmed by ls command, you can see that jar is copied to the home directory of my EC2 instance.

Note: Please notice the tilda(~) sign at the end. It means jar file is being copied to home directory of the instance.

Step 8: SSH into the EC2 instance and Install Java 1.8

There are multiple ways to SSH but I prefer browser based SSH connection using EC2 Instance Connect feature. In this way I don’t depend on my local system. But please note that at the time of writing only below linux distribution are supported.

  • Amazon Linux 2 (any version)
  • Ubuntu 16.04 or later

So, If your instance type is one of the above, feel free to use EC2 Instance Connect.

Since, I launched an Amazon Linux 2 so I will go ahead and connect using EC2 Instance Connect.

SSH into Instance using Instance Connect.

1.SSH into your Instance using Instance Connect.

Select your EC2 Instance and click on Connect as shown in above screenshot.

Connect to Your Ec2 Instance using Browser

Verify If username is correct for your type of instance and click connect.

Usernme verification

In a matter of seconds, you are connected to your Instance.

Connected to Ec2 Insatnce

2.SSH into your instance from your Local machine

If you have linux system, you can use below command to SSH into your instance.

ssh -i path/to/DemoKeyPair.pem ec2-user@ec2-12-34-567-890.compute-1.amazonaws.com

In case of window, you can use putty to SSH into your instance.

Here is how: SSH into your linux instance from windows using putty

Install Java 1.8

We are connected to our EC2 instance so Let’s start with checking current version of Java.

java -version
Java is already installed

As you can see my AMI already came with java 1.8 is installed.

If it’s not installed, you can install it using below command.

 sudo yum install java-1.8.0

Step 9 : Run the Spring Boot Jar File on EC2

Java is installed in the EC2 instance and our jar file is present in the home directory of the instance. So let’s go ahead and run the jar file using below command.

java -jar demo-0.0.1-SNAPSHOT.jar
Spring Boot Application Rnning on port 8080

As you can see, Spring Boot application is running on EC2 instance on Port 8080.

As of now, If you try to access your application on-

https://ec2-34-240-45-168.eu-west-1.compute.amazonaws.com:8080/health

You will not get any response and your request will time out in the end. In most of the cases security group is the culprit.

Step 10: Allow Port 8080 on Your Instance Security Group

By default an EC2 instance security group allow only port 22 for SSH access. Let’s go ahead and allow port 8080.

Click on Your Instance.

Go to Security -> Click on instance security group.

Security group for instance

Check inbound rules and Edit the Inbound rules as shown in below screenshot.

Edlit inbound rule

Click on Add Rule.

Security group of instance

Add a rule for type Custom TCP and port range will be 8080. We will allow it from everywhere as you can see on above screenshot.

Click Save Rules

Step 11: Test Your spring boot endpoint deployed on EC2

Hit your application endpoint now.

http://ec2-34-240-45-168.eu-west-1.compute.amazonaws.com:8080/health

Congratulations !!!

You have successfully deployed your spring boot application on to AWS EC2.

Conclusion

In this in-depth tutorial, You learnt to deploy spring boot application in AWS EC2.

Let’s summarize what we did-

  • Created and tested an Spring Boot application from scratch
  • Prepared the jar file and copied to EC2 instance.
  • Connected to EC2 instance using SSH and checked Java version
  • Deployed the Spring Boot application to EC2 and tested the endpoint

I hope you enjoyed the tutorial. Do let me know in comment in case you face any issue.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

I hope you enjoyed this tutorial. If you have a question, feel free to drop in comment. I will be more then happy to answer your query.

Meanwhile you can also –

Suggested Read:

Sharing is Caring:

5 thoughts on “How to Deploy Spring Boot Application on AWS EC2

  1. Thank you for the tutorial, but as soon as I close the tab where I was connected to the instance with, the application becomes available.

    Same with when I SSH on a terminal. How do i keep the app running?
    Thank you

    1. Sorry for the delay in response. You can try ‘nohup java -jar my-application.jar &’ to keep it running in the background. If i get time I will update the article to include this as well

  2. This is what we do at start up. But these days it’s advanced technologies being used coming with docker, kubernetes container based deployment.

    Correction –
    To create package you mentioned wrong command. It should be

    mvn clean compile package

    1. Hi Tejas, Thank you for adding your comment

      Regarding your first point. I agree , But there are still people looking for a tutorial to deploy a spring boot an on EC2 so it was for them.

      Regarding your second point, It’s up to you -you can use mvn clean package or mvn clean install. mvn clean install does everything athat package does and at the same time adds the jar to your local maven repo so that other projects can use it.

      Feel free to check on the same here: https://stackoverflow.com/questions/16602017/how-are-mvn-clean-package-and-mvn-clean-install-different

Leave a Reply

Your email address will not be published. Required fields are marked *