# Scaling Spring Boot with Auto Scaling Group (ASG) + Elastic Load Balancer (ELB)

Deploying a Spring Boot app on **multiple EC2 instances** with an **Application Load Balancer (ALB)** works great ✅
But what happens when traffic spikes? Or if one EC2 instance crashes?

👉 That’s where **Auto Scaling Groups (ASG)** step in.

By the end of this guide, you’ll know how to:

* Create an **AMI** of your Spring Boot app
* Build a **Launch Template**
* Configure an **Auto Scaling Group (ASG)**
* Integrate it with your **Application Load Balancer (ALB)**
* Auto-scale your application 🚀

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

---

## 🔑 Pre-requisite

From the **previous blog**, you should already have:

✅ 2 EC2 instances with Spring Boot JAR deployed on **port 8080**  
✅ Application Load Balancer (ALB) setup with a Target Group  

---

## 📦 Step 1: Create an AMI (Golden Image)

We’ll use one existing EC2 as a **base image (AMI)**.

1. Go to **EC2 Dashboard → Instances**
2. Select an EC2 instance where your Spring Boot app is running
3. From **Actions → Image and Templates → Create Image**
4. Name it: `springboot-ami`
5. Click **Create AMI**

⏳ Wait 8–10 minutes. This AMI will be the **blueprint** for new EC2s in the ASG.

---

## 📝 Step 2: Create a Launch Template

Launch Template = Recipe to spin up new EC2s.

1. Go to **EC2 → Auto Scaling Groups → Create Auto Scaling Group**

2. Under **Launch Template**, click **Create new launch template**

3. Fill details:

   * Name: `springboot-template`
   * AMI: Select **My AMIs → springboot-ami**
   * Instance type: `t3.micro`
   * Key Pair: Same as before
   * Security Group: Must allow **22 (SSH), 80 (HTTP), 8080 (Spring Boot)**

4. Add **User Data** (to auto-start the app):

```bash
#!/bin/bash
cd /home/ubuntu
sudo nohup java -jar app.jar > app.log 2>&1 &
```

5. Click **Create Launch Template** ✅

---

## ⚙️ Step 3: Create Auto Scaling Group (ASG)

1. Back to **Auto Scaling Group creation**
2. Name: `springboot-asg`
3. Select the **Launch Template** you just created
4. Click **Next**

---

## 🌐 Step 4: Configure Network Settings

* VPC → Default VPC
* Subnets → Select available ones (where ALB exists)
* Leave defaults → **Next**

---

## 🔗 Step 5: Integrate with Load Balancer

1. On **Load Balancer settings**:

   * Attach to **existing Load Balancer**
   * Select your **Spring Boot Target Group (8080)**
   * Enable **ALB health checks** for reliability

2. Click **Next**

---

## 📊 Step 6: Set Group Size & Scaling Policy

Define scaling behavior:

* **Desired capacity** → `2` (start with 2 instances)
* **Minimum capacity** → `1`
* **Maximum capacity** → `3`

Add a scaling policy:

* Policy type → **Target tracking**
* Metric → **ALB Request Count per Target**
* Target Group → `springboot-tg`
* Target Value → `50` (scale out if >50 requests per instance)

---

## ✅ Step 7: Review & Create

* Double-check everything
* Click **Create Auto Scaling Group**

Your ASG is now active 🎉

---

## 🔍 Step 8: Verify Setup

* Go to **EC2 → Instances** → You’ll see new instances launched by ASG
* Open **ELB → Target Group → Targets** → Should show healthy instances
* Try **terminating one instance manually** → ASG will auto-recreate it!

---

## 🧪 Step 9: Load Test Your Setup

To simulate traffic for 5 mins:

```bash
ab -n 1000 -c 50 http://<ALB_DNS_NAME>/api/adapt/welcome
```

(Using ApacheBench or any load testing tool)

🎯 ASG will launch new EC2s automatically if traffic increases.

---

## 🎯 Final Result

You’ve successfully:

✅ Created an AMI for your Spring Boot app  
✅ Configured a Launch Template  
✅ Built an Auto Scaling Group (ASG)  
✅ Integrated ASG with your Load Balancer  
✅ Enabled **self-healing + scaling** 🚀  

Now your app can handle failures and traffic surges without manual effort 💪

---

## ✅ 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)

---


