πŸ’‘ Ask Tutor

Elastic Load Balancing & Auto Scaling in AWS

Mastering Scalability, High Availability, and Fault Tolerance

In this chapter, we’ll take a deep dive into two essential AWS servicesβ€”Elastic Load Balancing (ELB) and Auto Scalingβ€”which are foundational for building scalable, reliable, and resilient cloud applications.

Part 1: Elastic Load Balancing (ELB)

What is Elastic Load Balancing?

Elastic Load Balancing (ELB) automatically distributes incoming application traffic across multiple targets (EC2, containers, Lambda) in one or more Availability Zones, ensuring high availability, fault tolerance, and performance.

It acts as the β€œtraffic cop” in your cloud architecture, ensuring no single server bears too much load.

Types of Load Balancers

AWS provides three main types of ELBs. Each is tailored to different types of applications:

Load BalancerDescription
Application Load Balancer (ALB)Layer 7, HTTP/HTTPS, supports routing by path/host
Network Load Balancer (NLB)Layer 4, TCP/UDP, ultra-low latency & high throughput
Gateway Load Balancer (GWLB)Designed for third-party virtual appliances like firewalls

βš™οΈ Key Features of ELB

  • Health Checks: Automatically routes traffic only to healthy targets.
  • SSL Termination: Offload SSL processing at the load balancer.
  • Sticky Sessions: Maintain user sessions with a specific backend.
  • Path-Based Routing: Route /api to one service and /static to another (ALB).
  • WebSocket Support: ALB supports WebSocket protocols for real-time apps.
  • IPv6 Support, WAF Integration, and Cross-Zone Load Balancing.

πŸ› οΈ How to Create an Application Load Balancer (ALB)

Using AWS Console:

  1. Open EC2 β†’ Load Balancers β†’ Create Load Balancer β†’ Select ALB
  2. Define name, scheme (internet-facing), and VPC/subnets
  3. Choose Security Groups
  4. Add Listeners (e.g., HTTP/HTTPS)
  5. Configure Target Groups:
    • Target Type: Instance, IP, Lambda
    • Health checks path: /health or /
  6. Register Targets (EC2s)
  7. Review and Create

Using AWS CLI:

Bash
aws elbv2 create-load-balancer \
  --name my-alb \
  --subnets subnet-aaa subnet-bbb \
  --security-groups sg-12345678 \
  --scheme internet-facing \
  --type application

Real Use Case Scenarios for ELB

IndustryUse Case
E-CommerceDistribute traffic to frontend, API, payment gateways
SaaS ApplicationsMicroservice architecture routing with ALB
Media & GamingUltra-low latency using NLB
Security PlatformsUse GWLB for inspecting all inbound traffic

Best Practices for ELB

  • Enable Access Logs for monitoring and audit.
  • Use SSL/TLS termination to offload decryption from backend servers.
  • Always deploy across multiple AZs for high availability.
  • Protect with AWS WAF for web attack mitigation.
  • Use ALB with ECS/EKS for containerized environments.

Part 2: Auto Scaling (ASG)

πŸ”„ What is Auto Scaling?

Auto Scaling allows AWS to automatically add (scale out) or remove (scale in) compute resources (like EC2 instances) based on real-time demand, saving costs while ensuring consistent performance.

Auto Scaling involves:

  • Auto Scaling Group (ASG)
  • Launch Template or Launch Configuration
  • Scaling Policies

Key Components of Auto Scaling

ComponentDescription
Launch TemplateSpecifies the EC2 config (AMI, instance type, key pair, etc.)
Auto Scaling GroupLogical group of EC2s managed together
Scaling PoliciesRules to scale based on metrics (CPU, memory, request count)
Warm PoolsPre-initialized EC2s for faster scaling
Scheduled ScalingScale based on time/date patterns
Predictive ScalingUses machine learning to predict load trends

How to Configure an Auto Scaling Group

Step-by-Step (AWS Console):

  1. Go to EC2 β†’ Auto Scaling Groups β†’ Create ASG
  2. Choose or create a Launch Template
  3. Set Group Name, VPC, and Subnets
  4. Choose Load Balancer (optional but recommended)
  5. Set:
    • Desired Capacity (e.g., 2)
    • Minimum Instances (e.g., 1)
    • Maximum Instances (e.g., 5)
  6. Add Scaling Policies:
    • Target Tracking (e.g., maintain CPU at 60%)
    • Step Scaling (scale by count or percentage)
    • Scheduled Scaling (e.g., increase at 9 AM)

Example CLI for Auto Scaling

βœ… Create Launch Template:

Bash
aws ec2 create-launch-template \
  --launch-template-name my-template \
  --version-description "v1" \
  --launch-template-data file://template-data.json

βœ… Create Auto Scaling Group:

Bash
aws autoscaling create-auto-scaling-group \
  --auto-scaling-group-name my-asg \
  --launch-template "LaunchTemplateName=my-template,Version=1" \
  --min-size 1 --max-size 5 --desired-capacity 2 \
  --vpc-zone-identifier "subnet-aaa,subnet-bbb"

Real-World Examples of Auto Scaling

Use CaseDescription
Traffic Spikes on E-Commerce WebsiteAdd EC2s automatically during sale hours
SaaS with Variable WorkloadScale EC2s based on CPU usage or queue size
24/7 ApplicationsMaintain minimum capacity + rapid scale-out
Game ServersAdd capacity only during peak playing hours

πŸ”’ Security Considerations for ELB & ASG

  • Place EC2s in private subnets, only ALB in public
  • Use IAM roles for EC2s for minimal access
  • Configure Security Groups: ELB β†’ EC2s only on required ports
  • Regularly rotate AMIs and patch instances
  • Use Auto Recovery feature for failed instances

Monitoring & Logging

ToolWhat It Monitors
CloudWatchMetrics (CPU, Network, Latency)
CloudTrailAPI usage for ELB & ASG
ELB Access LogsAll incoming HTTP/HTTPS requests
Auto Scaling EventsScaling activity and reasons

Use CloudWatch Alarms to trigger scale-out or scale-in policies.

πŸ”„ ELB + ASG Architecture Diagram (Textual)

SCSS
User
 ↓
[Route 53 - DNS]
 ↓
[Application Load Balancer]
 ↓
[Auto Scaling Group]
 ↳ EC2 Instance 1
 ↳ EC2 Instance 2
 ↳ EC2 Instance (scaled dynamically)
 ↓
[Database - RDS/DynamoDB]

βœ… This pattern ensures resilience, cost optimization, and scalability.

Summary
FeatureLoad BalancingAuto Scaling
PurposeDistribute trafficDynamically adjust EC2 count
TypesALB, NLB, GWLBTarget tracking, step, scheduled
BenefitsHigh availability, SSL offloadCost-efficient scaling, redundancy
MonitoringAccess logs, CloudWatch, WAFCloudWatch, scaling activity logs
Real-World FitWeb apps, APIs, microservicesE-commerce, SaaS, event-driven apps