How to Deploy Spring Boot Application on AWS EC2

How to Deploy Spring Boot Application on AWS EC2

How to Deploy Spring Boot Application on AWS EC2

Dear Reader, In one of my previous posts, you saw how to deploy Spring Boot application on AWS Elastic Beanstalk. 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.

What do we do if we talk about deploying Spring Boot apps to any server?

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

It’s that simple. 🙂

So are you ready to deploy Spring Boot application on AWS EC2 with me?

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

Don’t want to miss any posts from us? join us on our Facebook group, and follow us on Facebook, Twitter, LinkedIn, and Instagram. You can also subscribe to our newsletter below to not miss any updates from us.

Steps to Deploy Spring Boot Application on AWS EC2

  1. Create a Spring Boot project using Spring Initializr
  2. Import the project into your favourite 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 on AWS Ec2

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

After filling up the 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 favourite 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

After that, click Next

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

Deploy a Spring Boot Project on 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 to our spring boot project so that we can test it.

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

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 the build is successful, let’s run the project locally.

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

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

Step 5: Prepare the final jar file

Our application is working perfectly in the 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 the 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 a jar file.

Step 7: Copy the jar file to AWS EC2

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

copy jar

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

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

So, If you are a Linux user, continue the below steps

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

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

Navigate to the folder containing your jar and key pair.

cd /path/to/folder

Prepare the copy command as per the below syntax

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

After entering the details, the command result is like below.

scp -i ./DemoKeyPair.pem ./demo-0.0.1-SNAPSHOT.jar [email protected]:~

Please note that before you can run the 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 the 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 [email protected]:~
Home Directory has the jar file uploaded now

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

Note: Please notice the Tilda (~) sign at the end. It means the jar file is being copied to the 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 distributions 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 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 the above screenshot.

Connect to Your Ec2 Instance using Browser

Verify If the 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 a Linux system, you can use the below command to SSH into your instance.

ssh -i path/to/DemoKeyPair.pem [email protected]

In the case of Windows, 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 the current version of Java.

java -version
deploy spring boot application on aws ec2

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

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

 sudo yum install java-1.8.0

Step 9: Deploy Spring Boot application on AWS EC2 /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 the below command.

java -jar demo-0.0.1-SNAPSHOT.jar
How to Deploy Spring Boot Application on AWS EC2

As you can see, the Spring Boot application is running on an 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 cases, the 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 the instance security group.

deploy spring boot application on aws ec2

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

deploy spring boot application on aws ec2

Click on Add Rule.

deploy spring boot application on aws ec2

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

Click Save Rules

Step 11: Test Your spring boot endpoint deployed on EC2

As we have successfully deployed our Spring Boot application on AWS EC2 and also edited the security group configuration to allow inbound traffic.

Hit your application endpoint now.

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

deploy spring boot application on aws ec2

Congratulations !!!

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

Conclusion

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

Let’s summarize what we did-

  • Created and tested a Spring Boot application from scratch
  • Prepared the jar file and copied it to the 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 and were able to deploy spring boot application on aws ec2. Do let me know in the comments in case you face any issues.

Enjoyed the content?

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

Meanwhile, you can also –

Suggested Read:

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

  1. hello, thanks for your tutorial, but can you help on how to deploy pg4 database with rest api spring boot on AWS ec2

  2. 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

  3. 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

      1. A really useful tutorial – thank you!! I used it to deploy a Dropwizard app rather than Spring Boot but followed the same steps bar the Spring specific ones.

Leave a Reply

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