AWS Builder Center
Day 10: Elastic Load Balancing and Auto Scaling – Handling Traffic Automatically

Day 10: Elastic Load Balancing and Auto Scaling – Handling Traffic Automatically

Day 10 of my 90-day AWS challenge: how load balancers spread traffic across servers, how Auto Scaling adds and removes EC2 instances on demand, and how the two work together for a highly available application.

Recap of Day 9

Yesterday I learned how CloudWatch monitors metrics and triggers alarms. Today those ideas come together: when traffic grows, AWS can spread it across servers and add more of them automatically.

The problem

A single EC2 instance is a single point of failure. If it crashes, the app goes down. If traffic spikes, it slows down. If traffic is low, I'm paying for capacity I don't need.

Elastic Load Balancing (ELB)

ELB distributes incoming traffic across multiple targets, such as EC2 instances, in one or more Availability Zones. It also checks the health of each target and stops sending traffic to unhealthy ones.
TypeWorks atBest for
Application Load Balancer (ALB)Layer 7 (HTTP/HTTPS)Web apps, path-based and host-based routing
Network Load Balancer (NLB)Layer 4 (TCP/UDP)Very high performance and low latency
Gateway Load Balancer (GWLB)Layer 3Third-party virtual security appliances

Key ELB concepts

  • Listener: checks for connection requests on a port and protocol, e.g. HTTP on port 80
  • Target group: the set of targets that receive the traffic
  • Health check: a periodic request that decides whether a target is healthy

Auto Scaling

An Auto Scaling group (ASG) keeps the right number of EC2 instances running.
ConceptWhat it is
Launch templateDefines what to launch: AMI, instance type, key pair, security group
MinimumThe fewest instances that must always run
DesiredThe number the group tries to maintain right now
MaximumThe most instances the group is allowed to launch

Scaling policies

  • Target tracking: keep a metric near a target, e.g. average CPU at 50%
  • Step scaling: add or remove a set number of instances as an alarm crosses different thresholds
  • Scheduled scaling: scale at known times, e.g. before a daily traffic peak
Auto Scaling uses CloudWatch metrics and alarms to decide when to scale, which connects directly to what I learned on Day 9.

How they work together

  1. Users send requests to the load balancer
  2. The load balancer spreads them across instances in multiple Availability Zones
  3. The Auto Scaling group adds instances when load rises and removes them when it falls
  4. Unhealthy instances are replaced automatically
The result is an application that is highly available and cost-efficient.

What I practiced today

  1. Created a launch template for a small EC2 instance
  2. Created an Auto Scaling group across two Availability Zones with min 1, desired 1, and max 2
  3. Created an Application Load Balancer with a target group and health check
  4. Added a target tracking policy based on average CPU
  5. Deleted the Auto Scaling group and load balancer afterwards

Key takeaways

  • Load balancing gives availability, and Auto Scaling gives elasticity
  • Use at least two Availability Zones so one failure does not take the app down
  • Load balancers are billed by the hour, so delete practice ones to avoid charges

Up next: Day 11

Amazon Route 53 and CloudFront: DNS and content delivery.
Learning AWS too? Follow along and share what you're building. #90DaysOfAWS
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article