# Deploy a Spring Boot App on AWS EC2 with Load Balancer (Step-by-Step Guide)

---

## 🌍 Introduction

Deploying your application manually on AWS can be time-consuming and error-prone. You might face:

* Traffic distributed unevenly across instances ❌
* Manual setup of health checks and failover ❌
* Lack of scalability and high availability ❌

👉 That’s where **EC2 + ELB (Elastic Load Balancer)** deployment shines.

By the end, you’ll understand how to deploy a **Spring Boot app** on **multiple EC2 instances**, set up an **Application Load Balancer**, and route traffic intelligently.

👉 Flow: **Browser → ELB → EC2 Instances running Spring Boot**

---

## 🔒 Step 1: Prepare Your Spring Boot App

Before we touch AWS, package your Spring Boot project into a runnable `.jar` file:

```bash
mvn clean package
```

You’ll get something like `app.jar` in the `target/` directory.

✅ We assume the app runs on **port 8080**
✅ Requires **Java** and **Maven** installed locally
✅ Target is `.jar` deployment, not Docker

---

## ⚡ Step 2: Launch EC2 Instances

Let’s spin up two EC2 instances for our Spring Boot app:

1. Go to **AWS Console → EC2 → Launch Instance**
2. OS: **Ubuntu Server 24.04 LTS** (Free Tier eligible)
3. Instance type: **t2.micro**
4. Key Pair: Create/choose one (for SSH access)
5. Configure Security Group:

   * **SSH (22)** → Your IP
   * **HTTP (80)** → Anywhere
   * **Custom TCP (8080)** → Anywhere (for Spring Boot)

🎯 **Launch two EC2 instances** with the same configuration.

---

## 📜 Step 3: Install Java & Deploy Spring Boot App

SSH into both instances:

```bash
ssh -i your-key.pem ubuntu@<EC2_PUBLIC_IP>
```

Install Java:

```bash
sudo apt update
sudo apt install openjdk-21-jdk -y
```

Upload your app using `scp`:

```bash
scp -i your-key.pem app.jar ubuntu@<EC2_PUBLIC_IP>:/home/ubuntu/
```

Run the app in the background:

```bash
sudo nohup java -jar app.jar > app.log 2>&1 &
```

**Breakdown**:

* `nohup`: Keeps process running after logout
* `java -jar`: Runs your Spring Boot app
* `> app.log 2>&1 &`: Logs output and runs in background

✅ Now access your app via:

```bash
http://<EC2_PUBLIC_IP>:8080/api/adapt/welcome
```

Repeat for the second EC2 instance.

---

## 🌐 Step 4: Create an Elastic Load Balancer (ALB)

Now we’ll route traffic to both EC2 instances via a Load Balancer.

1. Go to **EC2 → Load Balancers**
2. Click **Create Load Balancer → Application Load Balancer**
3. Name: `springboot-alb`
4. Scheme: **Internet-facing**
5. VPC: Use **default VPC**
6. Select AZs: Pick where your EC2s are running
7. Security Group:

   * Create a new one: `springboot-sg`
   * Inbound Rules: Allow **HTTP (80)** and **SSH (22)**

### Listener Setup

* Protocol: **HTTP**
* Port: **80**

### Create Target Group

* Type: **Instances**
* Name: `springboot-tg`
* Protocol: **HTTP**
* Port: **8080**
* Health check path: `/actuator/health/readiness`

✅ Register your two EC2 instances
✅ Attach this Target Group to the Load Balancer
✅ Click **Create Load Balancer**

Your **ALB is now live** and routing traffic.

---

## 🧪 Step 5: Test Your Setup

🎯 Go to **Load Balancer → Description** tab
📄 Copy the **DNS name** (e.g., `springboot-alb-123456.ap-south-1.elb.amazonaws.com`)

Visit:

```bash
http://<DNS_NAME>/api/adapt/welcome
```

Expected Output:

```
Welcome to Adaptation Controller - Techeazy
```

✅ Confirms ELB is routing to healthy EC2 instances
✅ Confirms Spring Boot app is working across both instances

🛠 You can now explore:

* **Manual deployment**
* **Creating AMIs**
* **Using Launch Templates with Auto Scaling**

---

## 🎯 Conclusion

You’ve successfully:

✅ Deployed a Spring Boot app on EC2
✅ Set up a Load Balancer to route traffic
✅ Configured health checks to ensure uptime
✅ Created a production-ready architecture for web-scale apps

👉 Next, pair this setup with **Auto Scaling Groups** for **true elasticity and high availability**.

---

## ✅ Next Steps

🚀 Be interview-ready in the era of AI & Cloud — start your DevOps journey today!
💡 YouTube won’t get you a job. Real projects + real internship certificate will.
🔥 AI is reshaping jobs. Don’t watch it happen, be part of it with DevOps & Cloud skills.
🎯 ₹2000/month today = Dream job tomorrow. Secure your spot now.
⏳ Every month you wait, Cloud + AI jobs are being filled. Don’t miss out!
🌐 DevOps + AWS + AI = The skillset every recruiter is hunting for in 2025.

👉 Register now at [TechEazy Consulting](https://www.techeazyconsulting.com)

---

