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:
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:
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:
You are now logged in as ec2-user.
Step 5: Copy the JAR to EC2
Use scp from your local machine:
Back in SSH, verify:
Step 6: Install Java 17 on EC2
EC2 initially has no Java. Install it:
Verify:
Step 7: Run the Spring Boot JAR
Start your app:
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:
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:
This:
- Runs the app in the background
- Stores logs in output.log
- Keeps app running even after disconnecting SSH
Check logs:
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
Post a Comment