In this blog we ‘ll learn about spring boot application deployment to AWS EC2. EC2 stands for Elastic compute Cloud. It’s like renting a server from AWS and run your application like you are running it on your local system.
Let’s see what we ‘ll learn in this blog.
- Create a Spring boot application with PostgreSQL database.
- Create docker image of the spring boot application.
- Login to docker from local terminal and deploy this image to dockerhub.
- Create AWS EC2 instance and install docker in that instance.
- After Installation of docker on EC2 instance we run our spring boot project docker image on ec2.
So it’s coding time now let’s start and implement each step.
1- Create a Spring boot application with PostgreSQL database.
You can create a spring boot project from their official website https://start.spring.io/ or use any IDE. We need to add the below dependency in our project.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Database Dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Lombok dependency -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>Code language: HTML, XML (xml)
Add below properties in your application.properties file for database connectivity. Here we provide basic information about database like datasource URL, username and password.
spring.application.name=SpringbootAwsEc2
spring.datasource.url=${SPRING_DATASOURCE_URL}
spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}
# Hibernate Configuration
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImplCode language: PHP (php)
After this we create a simple controller to expose one endpoint. So that once our application is deployed to AWS ec2 we can validate it via calling this endpoint.
And along with this controller we create a JPA Entity class as well. So we can see that our spring boot application is connected to PostgreSQL on EC2 and able to do the JPA operations. Let’s create both.
EC2Controller .java
package com.javadream.SpringbootAwsEc2.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EC2Controller {
@GetMapping("/helloAwsEc2")
public String AwsEc2(){
return "Hello from Javadream you have deployed your spring boot application to AWS EC2";
}
}
Code language: CSS (css)
UserDetails .java
package com.javadream.SpringbootAwsEc2.entity;
import jakarta.persistence.*;
import lombok.Data;
@Data
@Entity
@Table(name = "userDetails")
public class UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String name;
private String emailId;
private String mobileNUmber;
private int isActive;
}Code language: CSS (css)
We have created both controller and Entity class. Now create a Dockerfile file.
Dockerfile
FROM eclipse-temurin:21-jdk-jammy
WORKDIR /javadream
COPY target/springboot-aws-ec2.jar springboot-aws-ec2.jar
ENTRYPOINT ["java", "-jar", "springboot-aws-ec2.jar"]Code language: JavaScript (javascript)
2- Create docker image of the spring boot application
It’s time for creating the docker image of our spring boot project. This is very simple as we have already created the Dockerfile in step 1. Now we just have to run docker build command to create the docker image. So go to your project root directory and run the below command.
docker build -t springboot-aws-ec2-dockerimage:v1 .Code language: CSS (css)
It ‘ll take some time to create the image just wait for some moment. Once it’s done you can verify the images has been created or not using below command.
docker images

3- Login to docker from local terminal and deploy this image to dockerhub.
Till step 2 we have created the docker image for our spring boot project. Now it’s time to deploy this image to dockerhub. Use below commands to complete this process. Make sure you have a Dockerhub account if not you can go to Dokcerhub and create the account.
Login to docker using below command
docker login
After running above command it ‘ll ask for your Dockerhub username and password. Just provide that details and you ‘ll be logged in. You ‘ll see something like below screenshot.

We have our docker image ready and we logged-in to dockerhub also. It’s time to push our spring boot docker image to Dockerhub portal. Follow below steps to complete this process.
First tag the image with your dockerhub username and dockerhub repository name.
docker tag springboot-aws-ec2-dockerimage:v1 vashurajput/springboot-ec2-image:v1

Push the image to Dockerhub
Last we just tag the image with our dockerhub username and repository. It’s time to push. To see which image we have tagged run docker images command and then just use docker push with image name along with tag.
docker push vashurajput/springboot-ec2-image:v1
It ‘ll take some time wait for some moment. Once done just go to your dockehub portal and see the image there.

4- Create AWS EC2 instance and install docker in that instance.
Now we ‘ll create the AWS EC2 instance. After creating it we ‘ll get a certificate, using this certificate we can ssh into our aws ec2 instance. After ssh into ec2 instance we ‘ll install the docker in that instance and run out spring boot docker image.
Let’s see this process step by step.
Go to AWS console and search for EC2.
Select EC2 and click on Launch instance.
On Launch an instance page fill the name. Select OS image as Amazon Linux or whatever OS you are comfortable with. Select instance type as t2.micro. On Key pair click on create new key pair and fill the details it ‘ll give you a .pem file that helps you to ssh into your aws ec2 instance. After this keep remaining field default and click on launch instance.





So we have successfully created the EC2 ( Elastic Compute Cloud ). In next section let’s see how to install docker and deploy our spring boot image on this ec2.
5- Install Docker on EC2 instance and run spring boot project docker image
First SSH into your ec2 instance. Keep that .pem file ready that we get while creating the ec2 instance. Open your terminal and run the below command.
ssh -i javadreamEC2RSA.pem ec2-user@3.86.154.102Code language: CSS (css)

We have successfully ssh into our instance. This is a fresh server so we don’t have docker here. Let’s install the docker first. Run the below command to install docker on your amazon-linux ec2 instance.
sudo dnf update -y
sudo dnf install docker -y
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker ec2-user
newgrp docker
Now after running all above commands on your ec2 console. Just verify if docker installed or not by running docker -v command.

We have docker now on our ec2. First pull the postgreSQL image. Run below command to pull postgreSQL image.
docker run -d --name my_postgres -e POSTGRES_USER=myuser -e POSTGRES_PASSWORD=mypassword -e POSTGRES_DB=mydb -p 5433:5432 postgres:17
It ‘ll take some time to pull the images. You can see we are passing some parameter with pull command. This is the username, password and DB that helps us to connect to our DB via spring boot application or by any plugin like Dbeaver.
Just verify which postgres version has been installed. Go inside postgres container using docker exec and run the select version() command.
docker exec -it f68927df7958 bash

Pull your spring boot image on ec2 and run
Run the below command to pull the docker image. Just change this command as per your docker username and repository. It ‘ll take some time just wait and get this image pulled. After completed run docker images command to verify.
docker pull vashurajput/springboot-ec2-image:v1

Now we have both the image ready. If you remember in our application.properties file we are passing datasource url, username and password as variable. So we need to pass that variable at run time while running docker image. Use below command to do that.
docker run -p 8080:8080 -e SPRING_DATASOURCE_URL=jdbc:postgresql://ip-172-31-80-154.ec2.internal:5432/mydb -e SPRING_DATASOURCE_USERNAME=myuser -e SPRING_DATASOURCE_PASSWORD=mypassword vashurajput/springboot-ec2-image:v1Code language: JavaScript (javascript)
in datasource url just replace the ip with your instance private ip. Follow below screenshot for reference.

Final Step
You can see that our spring boot application is up and running. It’s time to validate that are we able to access our endpoint that we created and also validate is our userdetail table created or not while running this spring boot docker image.
Before checking this as we know that our application is running on 8080 port and our postgreSQL is running on 5433 port. So just edit your inbound rule for this EC2 instance so traffic come on this port. Follow below step to do this rule changes.
- Go to your instance
- Go to security tab.
- Click on Security groups
- Click on Edit inbound rule. And add new rule if it’s not there.



Validation
Now we are done congrats you have completed all the necessary steps. It’s time to validate our endpoint and connect postgreSQL to any software like Dbeaver to validate database thing.
For validating endpoint just use your EC2 public IP address and call that endpoint you ‘ll get the response.

For validating userdetail table creation let’s connect it to Dbeaver and see.


Hope you enjoy this blog. If you like please share this with others and help us to grow. Please provide your feedback on comment section.
If you need blog on any topic please mention in comment section we ‘ll try to cover in our upcoming blog. Thanks
Other blogs you may like.