Deploy Springboot 3 based Microservice on AWS EC2

Hemant
2 min readSep 10, 2023

--

In this article we will explore a simple way to deploy springboot 3 based microservice to AWS EC2 Instance.

If you don’t have an account already in AWS or haven’t created any instance yet then follow steps mentioned here

Steps in this tutorial

  1. Create microservice using springboot 3
  2. Connect to EC2 instance via SSH.
  3. Install Java, Git & maven on EC2 instance
  4. Clone application git repo
  5. Start application using maven
  6. Test Application URL
  7. Stop/Terminate instance

1. Create microservice using springboot 3

For this article we are going to use the userbook service which is already created using springboot 3 and explained in this article.

2. Connect to EC2 instance via SSH

Run below command to connect EC2 instance

ssh -i <key-pair>.pem ec2-user@<Public IPv4 DNS of EC2>

Replace these fields in above command

<key-pair>.pem — File name of key-pair file generate from AWS while creating EC2.

<Public IPv4 DNS of EC2> — Can be obtained from EC2 instance summary Networking tab.

3. Install Java, Git & maven on EC2 instance

Run these commands in EC2 instance via ssh session.

  1. Install Java using command

sudo yum install java-17-amazon-corretto.x86_64

2. Install Git using command

sudo yum install git-core.x86_64

3. Install Maven using command

sudo yum install maven.noarch

4. Clone application git repo

Run command to clone userbook application repo from github

git clone https://github.com/hk-springboot-projects/userbook.git

5. Start application using maven

Run below commands to start application

cd userbook

mvn spring-boot:run

Application will be started at port 8080.

6. Test Application URL

Open any browser and type below url

http://<Public IPv4 DNS of EC2>:8080/users

you should see a list of users in response. userbook application uses embedded H2 database and default list of users are initialized on application bootstrap.

Play around with other APIs (POST/PUT/DELETE) using postman or any other api testing tool of your choice.

7. Stop/Terminate instance

Don’t forget to Stop or Terminate your instance once you are done with above steps.

Happy Learning.

--

--

Hemant

Lead Software Engineer @JPMorgan & Chase. Writes about Java, AWS and System Design