04. AWS ASG
An Auto Scaling Group (ASG) in AWS automatically manages the number of EC2 instances based on application load and health. It ensures that we always have the right number of instances running to handle current traffic efficiently.
π§© Why Use Auto Scaling Groups?β
Horizontal scaling = scale out / scale in
| Benefit | Description |
|---|---|
| βοΈ Elasticity | Scale out (add instances) or scale in (remove instances) automatically to match demand. |
| π° Cost Optimization | Pay only for the resources you need β no overprovisioning. |
| β€οΈ High Availability | Automatically replaces unhealthy instances with new healthy ones. |
| π Integration with Load Balancer | ASG works hand-in-hand with an Application Load Balancer (ALB) to distribute traffic evenly. |
βοΈ How Auto Scaling Worksβ
-
Define Group Parameters
- Minimum size: Lowest number of EC2 instances always running.
- Desired capacity: The target number of instances currently running.
- Maximum size: The upper limit of instances allowed in the group.
-
Scale Out
- Automatically adds EC2 instances when load increases (e.g., more traffic during the day).
-
Scale In
- Automatically removes EC2 instances when load decreases (e.g., less traffic at night).
-
Health Check & Replacement
- ASG automatically terminates unhealthy instances and launches new ones.
π Load Balancer Integrationβ
- ASG instances are automatically registered and deregistered from the Load Balancer.
- As instances are added or removed, the Load Balancer updates its target group accordingly.
- Ensures smooth traffic distribution at all times.
π§ Example Scenarioβ
| Time | Expected Load | ASG Action | Result |
|---|---|---|---|
| Daytime | High traffic | Scale out | Add instances |
| Night | Low traffic | Scale in | Remove instances |
β‘ Auto Scaling Strategiesβ
There are four main types of scaling strategies in AWS Auto Scaling Groups:
1οΈβ£ Manual Scalingβ
- User manually changes the desired capacity (e.g., from 1 β 2 instances).
- Useful for testing or controlled environments.
2οΈβ£ Dynamic Scalingβ
- Automatically adjusts capacity based on CloudWatch alarms.
πΉ Simple or Step Scalingβ
-
Triggered by specific CloudWatch alarms.
-
Example:
- If CPU > 70% for 5 minutes β Add 2 instances.
- If CPU < 30% for 10 minutes β Remove 1 instance.
-
"Step" scaling allows incremental scaling based on thresholds.
πΉ Target Tracking Scalingβ
-
Simplified approach.
-
Define a target metric value, and ASG automatically adjusts to maintain it.
-
Example:
- Target average CPU utilization = 40%.
- ASG adds/removes instances automatically to maintain this value.
3οΈβ£ Scheduled Scalingβ
- Scaling actions are performed at specific times.
- Ideal for predictable, time-based workloads.
β° Example:β
βIncrease minimum capacity to 10 EC2 instances every Friday at 5 PM before a big event.β
4οΈβ£ Predictive Scaling (AI/ML Based)β
- Uses Machine Learning to predict future traffic patterns.
- Automatically adjusts capacity in advance of expected load changes.
- Ideal for repetitive daily or weekly traffic spikes.
Example:β
If traffic spikes daily between 6β9 PM, ASG will automatically add instances at 5:50 PM.
π§ Summaryβ
| Scaling Type | Trigger | Automation | Example Use Case |
|---|---|---|---|
| Manual Scaling | User input | β | Testing or fixed capacity |
| Simple/Step Scaling | CloudWatch alarm | β | CPU spikes or drops |
| Target Tracking | Metric target | β | Maintain steady CPU utilization |
| Scheduled Scaling | Time-based schedule | β | Known traffic patterns |
| Predictive Scaling | ML-based prediction | β β | Regular, repeating patterns |
π Key Takeawaysβ
- ASG ensures elasticity, cost savings, and fault tolerance.
- Scaling strategies give flexibility β from manual control to ML-powered automation.
- Load Balancer + ASG = reliable and scalable cloud application setup.
- Remember: Elasticity = scaling in and out automatically based on demand.