Full Stack • Java • System Design • Cloud • AI Engineering

Deploy Spring Boot Application on AWS EC2

Learn how to deploy a Spring Boot application on AWS EC2 step by step using Java, Maven, SSH, Security Groups, systemd services, Nginx reverse proxy, and production deployment best practices.

Introduction

Amazon EC2 (Elastic Compute Cloud) is one of the most widely used AWS services for running applications in the cloud. It provides virtual machines where you can deploy Java, Spring Boot, Node.js, Python, databases, and many other workloads.

In this article, we'll deploy a Spring Boot application from scratch to an EC2 instance, configure it as a Linux service, and prepare it for production.


What You Will Learn

  • What is Amazon EC2?
  • EC2 deployment architecture
  • Launch an EC2 instance
  • Connect using SSH
  • Install Java
  • Build Spring Boot JAR
  • Upload application
  • Run Spring Boot
  • Configure systemd
  • Configure Security Groups
  • Production best practices
  • Common troubleshooting tips

Architecture

flowchart LR
    DEV[Developer Laptop]
    GIT[Spring Boot Project]
    JAR[Build JAR]
    SCP[Upload using SCP]
    EC2[Amazon EC2]
    APP[Spring Boot Application]
    USER[End User]

    DEV --> GIT
    GIT --> JAR
    JAR --> SCP
    SCP --> EC2
    EC2 --> APP
    USER --> APP

Step 1: Build Spring Boot Application

Generate a Spring Boot project from Spring Initializr.

Dependencies:

  • Spring Web
  • Spring Boot Actuator
  • Lombok (Optional)

Project Structure

springboot-ec2-demo
│
├── src
├── pom.xml
└── application.yml

Build the application:

mvn clean package

Output:

target/springboot-ec2-demo-0.0.1-SNAPSHOT.jar

Step 2: Launch EC2 Instance

Navigate to

AWS Console
→ EC2
→ Launch Instance

Configuration:

Setting Value
Name springboot-demo
AMI Amazon Linux 2023
Instance t2.micro
Storage 8 GB
Key Pair Create New
Security Group Custom

Step 3: Configure Security Group

Allow these inbound ports.

Port Purpose
22 SSH
8080 Spring Boot
80 HTTP (Optional)
443 HTTPS (Optional)

Security Group Flow

flowchart TD
    USER[Developer]
    SG[Security Group]
    SSH[Port 22]
    APP[Port 8080]
    EC2[EC2 Instance]

    USER --> SG
    SG --> SSH
    SG --> APP
    SSH --> EC2
    APP --> EC2

Step 4: Connect to EC2

Amazon Linux

ssh -i springboot.pem ec2-user@<PUBLIC-IP>

Ubuntu

ssh -i springboot.pem ubuntu@<PUBLIC-IP>

Verify connection.


Step 5: Install Java

Amazon Linux

sudo dnf install java-17-amazon-corretto -y

Ubuntu

sudo apt install openjdk-17-jdk -y

Verify

java -version

Step 6: Upload JAR

From your local machine

scp -i springboot.pem \
target/springboot-ec2-demo.jar \
ec2-user@<PUBLIC-IP>:/home/ec2-user/app.jar

Step 7: Run Application

java -jar app.jar

Open browser

http://<PUBLIC-IP>:8080

Step 8: Run in Background

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

Check process

ps -ef | grep java

View logs

tail -f app.log

Step 9: Create Linux Service

Create service

sudo vi /etc/systemd/system/springboot.service
[Unit]
Description=Spring Boot Application
After=network.target

[Service]
User=ec2-user
ExecStart=/usr/bin/java -jar /home/ec2-user/app.jar
SuccessExitStatus=143
Restart=always

[Install]
WantedBy=multi-user.target

Reload

sudo systemctl daemon-reload

Start

sudo systemctl start springboot

Enable

sudo systemctl enable springboot

Status

sudo systemctl status springboot

Service Deployment Flow

flowchart LR
    JAR[JAR File]
    SERVICE[systemd Service]
    START[systemctl start]
    RUN[Spring Boot Running]

    JAR --> SERVICE
    SERVICE --> START
    START --> RUN

Step 10: Verify Deployment

Browser

http://<PUBLIC-IP>:8080

Health endpoint

http://<PUBLIC-IP>:8080/actuator/health

Curl

curl http://localhost:8080

Optional: Configure Nginx

Install

sudo dnf install nginx -y

Configure reverse proxy

server {

    listen 80;

    location / {

        proxy_pass http://localhost:8080;

    }

}

Restart

sudo systemctl restart nginx

Architecture

flowchart LR
    USER[User]
    NGINX[Nginx]
    APP[Spring Boot]

    USER --> NGINX
    NGINX --> APP

Production Architecture

flowchart TD
    USER[Users]
    DNS[Route53]
    ALB[Application Load Balancer]

    APP1[Spring Boot EC2 AZ1]
    APP2[Spring Boot EC2 AZ2]

    DB[(Amazon RDS Multi-AZ)]

    CW[CloudWatch]
    SNS[SNS]

    USER --> DNS
    DNS --> ALB

    ALB --> APP1
    ALB --> APP2

    APP1 --> DB
    APP2 --> DB

    APP1 --> CW
    APP2 --> CW

    CW --> SNS

Deployment Checklist

  • Build Spring Boot application
  • Launch EC2
  • Configure Security Group
  • Install Java
  • Upload JAR
  • Run application
  • Configure systemd
  • Verify application
  • Configure Nginx
  • Monitor logs

Common Issues

Connection Refused

Check

sudo systemctl status springboot

Port 8080 Not Accessible

Verify Security Group allows port 8080.


SSH Permission Denied

Run

chmod 400 springboot.pem

Java Not Installed

java -version

Install Java if missing.


Best Practices

  • Use IAM Roles instead of Access Keys
  • Never expose port 8080 publicly in production
  • Use Application Load Balancer
  • Enable HTTPS using ACM
  • Configure Auto Scaling
  • Store logs in CloudWatch
  • Use Route53 for custom domains
  • Monitor CPU and Memory
  • Enable automatic backups
  • Use CI/CD for deployments

Interview Questions

What is Amazon EC2?

Amazon EC2 is a virtual machine service that provides scalable compute capacity in AWS.

How do you deploy Spring Boot on EC2?

Build the application, upload the JAR, install Java, run using java -jar, and configure systemd.

Why use systemd?

It automatically starts the application on reboot and restarts it if it crashes.

Why use Nginx?

It acts as a reverse proxy, handles HTTPS, and hides the Spring Boot port.


Summary

In this article, we learned how to deploy a Spring Boot application on AWS EC2 using Java, SSH, Security Groups, Linux services, and production deployment practices.

This deployment approach is commonly used in enterprise environments and provides a strong foundation before moving to Elastic Beanstalk, ECS, EKS, or Kubernetes.


Loading likes...

Comments

Share a question, correction, or practical insight about this article.

Loading approved comments...