Deploy Spring Boot Application on AWS EC2 (Step-by-Step)

Deploying a Spring Boot application on AWS EC2 is one of the easiest ways to host your Java backend in the cloud. In this guide, you will build a JAR file, copy it to an EC2 instance, install Java, run the app, expose port 8080, and keep the application running in the background using nohup.

Overview of the Deployment Flow

  • A Spring Boot REST API runs locally on port 8080
  • An EC2 instance (Amazon Linux, t2.micro) runs in AWS
  • You build a JAR from the Spring Boot project
  • You SSH into EC2 and copy the JAR
  • You install Java 17 and run the JAR file
  • You open port 8080 in the security group

After deployment, anyone can access your API using the instance's public IP or DNS.


Step 1: Create & Test a Simple Spring Boot App

Start with Spring Initializr:

  • Maven project
  • Java 17
  • Packaging: JAR
  • Dependency: Spring Web

Create a simple REST API:

@RestController public class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello from Spring Boot on EC2!"; } }

Run the app locally and verify: http://localhost:8080/hello


Step 2: Create an EC2 Instance

  • EC2 → Launch Instance
  • Name: spring-boot-app-server
  • AMI: Amazon Linux
  • Type: t2.micro (Free-tier)
  • Create key pair (.pem)
  • Security Group: Allow SSH (22) and later 8080
  • Storage: Default 8GB SSD

Launch the instance and note the public IP or DNS.


Step 3: Build the Spring Boot JAR

From terminal:

mvn clean package

This generates a file like: target/spring-boot-ec2-demo-0.0.1-SNAPSHOT.jar


Step 4: SSH into the EC2 Instance

Open terminal and run:

chmod 400 your-key.pem ssh -i your-key.pem ec2-user@your-public-dns

You are now logged in as ec2-user.


Step 5: Copy the JAR to EC2

Use scp from your local machine:

scp -i your-key.pem your-app.jar ec2-user@your-public-dns:/home/ec2-user

Back in SSH, verify:

ls -l

Step 6: Install Java 17 on EC2

EC2 initially has no Java. Install it:

sudo yum update -y sudo yum install -y java-17

Verify:

java -version

Step 7: Run the Spring Boot JAR

Start your app:

java -jar your-app.jar

When logs show Tomcat started on port 8080, your app is running inside EC2.


Step 8: Open Port 8080 in Security Group

Go to EC2 → Instance → Security group → Edit inbound rules

  • SSH — port 22
  • Custom TCP — port 8080, Source: 0.0.0.0/0

Save the rule and test:

http://your-public-dns:8080/hello

Your Spring Boot app is now publicly accessible!


Step 9: Keep the App Running with nohup

If you close SSH, the app stops. Use nohup to keep it alive:

nohup java -jar your-app.jar > output.log 2>&1 &

This:

  • Runs the app in the background
  • Stores logs in output.log
  • Keeps app running even after disconnecting SSH

Check logs:

tail -f output.log

Summary: What You Learned

You now understand how to:

  • Build a Spring Boot JAR
  • Create and configure an AWS EC2 instance
  • SSH into EC2
  • Install Java
  • Copy files using SCP
  • Run your Spring Boot app
  • Expose ports through security groups
  • Keep the app running with nohup

This manual deployment approach demonstrates the fundamental steps of hosting a Java application in the cloud. In real-world projects, these same tasks are automated using CI/CD pipelines, but understanding the manual process helps you master AWS deployments.

Comments

Popular posts from this blog

Spring Boot on AWS EC2: Upload to S3 Securely Using IAM Role

Java Streams Intermediate Operations Explained with Examples

ConcurrentHashMap vs Synchronized HashMap in Java