Learn how to Deploy Spring Boot Software on AWS EC2
Expensive Reader, In certainly one of my earlier posts, you noticed easy methods to deploy Spring Boot software on AWS Elastic Beanstalk. On this put up, I’ll enable you to deploy a spring boot software on AWS EC2 in a step-by-step method.
EC2 or Elastic Compute Cloud is nothing however the digital server in AWS Cloud. So ideally we’re gonna run our spring boot software within the AWS cloud.
What will we do if we discuss deploying Spring Boot apps to any server?
We set up Java on the server, copy the jar file onto the server and easily run the jar file utilizing java -jar jarname.
It’s that straightforward. 🙂
So are you able to deploy Spring Boot software on AWS EC2 with me?
This put up goes to be a bit lengthy however fascinating. So please fasten your seat belts and be prepared for the enjoyment experience 🙂
Don’t need to miss any posts from us? be a part of us on our Fb group, and comply with us on Fb, Twitter, LinkedIn, and Instagram. You may as well subscribe to our publication under to not miss any updates from us.
Steps to Deploy Spring Boot Software on AWS EC2
- Create a Spring Boot mission utilizing Spring Initializr
- Import the mission into your favorite IDE
- Add a RestController to have the ability to take a look at
- Check the applying domestically
- Put together the ultimate jar file
- Launch an EC2 Occasion and preserve the important thing pair helpful
- Copy jar file to AWS EC2
- SSH into the EC2 occasion and Set up Java 1.8
- Run the Spring Boot Jar File on EC2
- Enable port 8080 in your Occasion safety group
- Check Your spring boot endpoint deployed on EC2
Step 1: Create a Spring Boot mission utilizing Spring Initializr
If you have already got a working Spring Boot software, you may go to Step 5 instantly. If not let’s create a brand new spring boot software collectively.
let’s go to dash initilizer web page and shortly bootstrap a easy spring boot internet software.
Open https://begin.spring.io/ and replenish particulars similar to Group, Artifact, Identify and dependency.

Observe: Please word that I’ve added a single dependency Spring Internet as you may see within the above screenshot.
After filling up the small print and choosing internet dependency, click on on Generate
This can obtain the zipped mission into your native system.
Step 2: Import the mission into your favorite IDE
Unzip the downloaded mission and import the mission into your IDE. I might be importing to Eclipse for this tutorial.
Click on on File -> Import -> Maven -> Present Maven Venture
After that, click on Subsequent
Click on on Browse, navigate to your unzipped mission folder and choose that. Choosing that ought to present the pom.xml file as proven within the under screenshot.

Click on on End to the mission into the IDE.
Step 3: Add a RestController to have the ability to take a look at
Now, we now have the mission imported into the IDE. Let’s add a Relaxation Controller to our spring boot mission in order that we are able to take a look at it.
Click on in your mission and create a controller class with the endpoint of /well being. We’ll use minimal code just like the under snippet.
bundle com.cloudkatha.demo.controller;
import org.springframework.internet.bind.annotation.GetMapping;
import org.springframework.internet.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/well being")
public String well being() {
return "Hiya & Welcome to CloudKatha !!!";
}
}
Step 4: Check the applying domestically
Earlier than we take a look at it, let’s construct the mission using-
mvn clear set up
As soon as the construct is profitable, let’s run the mission domestically.
Proper-click on the category annotated with @SpringBootApplication and Run as a Java Software.

Our software is operating high-quality domestically and you’ll entry it through localhost:8080/well being
Step 5: Put together the ultimate jar file
Our software is working completely within the native atmosphere. Let’s put together the ultimate jar file and preserve it helpful in a folder as we’ll want the jar file in the course of the deployment.
Run Maven construct to bundle the applying as a fats jar.
mvn clear set up
As soon as your construct is profitable, you will see that the jar file within the goal folder. Copy the jar file and preserve it in your system at a handy location.
Step 6: Launch an EC2 Occasion and preserve the important thing pair helpful
Earlier than we deploy our spring boot app, we have to launch an EC2 occasion. Yow will discover a step-by-step tutorial to launch an occasion under.
Launch an EC2 occasion in AWS EC2 Step by Step
Observe: Please preserve the keypair helpful in the identical folder as a jar file.
Step 7: Copy the jar file to AWS EC2
Let’s preserve the KeyPair and the Jar to be deployed into the identical folder as under.

Please word that In case you are utilizing a Linux machine you should utilize the scp command out of the field. However in case you are utilizing Home windows, It doesn’t include a SCP consumer and you have to set up one earlier than you may copy information to EC2.
See: Learn how to Copy a File into EC2 Occasion from Home windows Machine
So, In case you are a Linux consumer, proceed the under steps
Open the terminal and navigate to the listing the place you have got stored the KeyPair and Jar File.
I usually use Cloud9 IDE with Linux occasion for growth so I’ll use the under command to repeat my information utilizing SCP.
Navigate to the folder containing your jar and key pair.
cd /path/to/folder
Put together the copy command as per the under syntax
scp -i ./DemoKeyPair.pem . @:/pathwhere/you/needto/copy
After getting into the small print, the command result’s like under.
scp -i ./DemoKeyPair.pem ./demo-0.0.1-SNAPSHOT.jar [email protected]:~
Please word that earlier than you may run the above command you have to set the permissions of your personal key file in order that solely you may learn it. Should you don’t set the permission then you can’t connect with your occasion utilizing keypair.
chmod 400 DemoKeyPair.pem
Sort the above command in your terminal and hit enter.
Now permission in your personal key’s set and you’ll run your copy command.
scp -i ./DemoKeyPair.pem ./demo-0.0.1-SNAPSHOT.jar [email protected]:~

As confirmed by the ls command, you may see that the jar is copied to the house listing of my EC2 occasion.
Observe: Please discover the Tilda (~) signal on the finish. It means the jar file is being copied to the house listing of the occasion.
Step 8: SSH into the EC2 occasion and Set up Java 1.8
There are a number of methods to SSH however I want browser-based SSH connection utilizing EC2 Occasion Join function. On this method, I don’t depend upon my native system. However please word that on the time of writing solely under Linux distributions are supported.
- Amazon Linux 2 (any model)
- Ubuntu 16.04 or later
So, In case your occasion kind is likely one of the above, be happy to make use of EC2 Occasion Join.
Since I launched an Amazon Linux 2 I’ll go forward and join utilizing EC2 Occasion Join.
SSH into Occasion utilizing Occasion Join.
1. SSH into your Occasion utilizing Occasion Join.
Choose your EC2 Occasion and click on on Join as proven within the above screenshot.

Confirm If the username is appropriate in your kind of occasion and click on join.

In a matter of seconds, you might be related to your Occasion.

2. SSH into your occasion out of your Native machine
You probably have a Linux system, you should utilize the under command to SSH into your occasion.
ssh -i path/to/DemoKeyPair.pem [email protected]
Within the case of Home windows, you should utilize putty to SSH into your occasion.
Right here is how: SSH into your linux occasion from home windows utilizing putty
Set up Java 1.8
We’re related to our EC2 occasion so Let’s begin with checking the present model of Java.
java -version

As you may see my AMI already got here with Java 1.8 put in.
If it’s not put in, you may set up it utilizing the under command.
sudo yum set up java-1.8.0
Step 9: Deploy Spring Boot software on AWS EC2 /Run the Spring Boot Jar File on EC2
Java is put in within the EC2 occasion and our jar file is current within the residence listing of the occasion. So let’s go forward and run the jar file utilizing the under command.
java -jar demo-0.0.1-SNAPSHOT.jar

As you may see, the Spring Boot software is operating on an EC2 occasion on Port 8080.
As of now, Should you attempt to entry your software on-
https://ec2-34-240-45-168.eu-west-1.compute.amazonaws.com:8080/well being
You’ll not get any response and your request will day out ultimately. Usually, the safety group is the perpetrator.
Step 10: Enable Port 8080 on Your Occasion Safety Group
By default, an EC2 occasion safety group enable solely port 22 for SSH entry. Let’s go forward and permit port 8080.
Click on on Your Occasion.
Go to Safety -> Click on on the occasion safety group.

Verify inbound guidelines and Edit the Inbound guidelines as proven within the under screenshot.

Click on on Add Rule.

Add a rule for kind Customized TCP and the port vary might be 8080. We’ll enable it from in all places as you may see within the above screenshot.
Click on Save Guidelines
Step 11: Check Your spring boot endpoint deployed on EC2
As we now have efficiently deployed our Spring Boot software on AWS EC2 and in addition edited the safety group configuration to permit inbound site visitors.
Hit your software endpoint now.
http://ec2-34-240-45-168.eu-west-1.compute.amazonaws.com:8080/well being

Congratulations !!!
You could have efficiently deployed your spring boot software to AWS EC2.
Conclusion
On this in-depth tutorial, You realized to deploy a spring boot software on AWS EC2.
Let’s summarize what we did-
- Created and examined a Spring Boot software from scratch
- Ready the jar file and copied it to the EC2 occasion.
- Related to EC2 occasion utilizing SSH and checked Java model
- Deployed the Spring Boot software to EC2 and examined the endpoint
I hope you loved the tutorial and have been capable of deploy spring boot software on aws ec2. Do let me know within the feedback in case you face any points.
Loved the content material?
Subscribe to our publication under to get superior AWS studying supplies delivered straight to your inbox.
In the meantime, you may also –
- Observe Us On
- Share this put up with your mates
Steered Learn: