What is a Load Balancer?
A load balancer distributes incoming traffic across multiple EC2 instances. Instead of one server handling all requests (and potentially crashing under heavy load), a load balancer spreads the work evenly. It's like having multiple cashiers at a grocery store instead of just one.
If one server goes down, the load balancer automatically routes traffic to the healthy ones. Your users never notice the difference — the application just keeps running.
Types of Load Balancers
Application Load Balancer (ALB) works at the HTTP/HTTPS layer. It can route traffic based on URL paths, hostnames, or headers. Perfect for web applications where you want to route /api traffic to one group of servers and /static traffic to another.
Network Load Balancer (NLB) works at the TCP/UDP layer. It's ultra-fast and can handle millions of requests per second with microsecond latencies. Great for gaming servers, IoT, or any TCP-based workload.
Gateway Load Balancer (GWLB) is for third-party virtual appliances like firewalls and intrusion detection systems.
Health Checks
Load balancers constantly check if your instances are healthy. They send periodic requests to a health check endpoint — if an instance fails to respond correctly, the load balancer stops sending traffic to it.
Health checks are configurable: you can set the path, port, interval, and how many failures before an instance is marked unhealthy. Think of them as a doctor performing regular checkups on your servers.
Creating an Application Load Balancer
The easiest way to create a load balancer is through the console. Select your VPC, subnets (at least two across different AZs), and security group. Then register your EC2 instances as targets.
aws elbv2 create-load-balancer \
--name my-alb \
--subnets subnet-aaa subnet-bbb \
--security-groups sg-xxx \
--type application
Once created, the load balancer gives you a DNS name. Point your domain's DNS to this name, and traffic will flow through the load balancer to your instances.
SSL/TLS Termination
Load balancers can handle SSL/TLS certificates, so your instances don't have to. You upload your SSL certificate to AWS Certificate Manager (ACM) and attach it to the load balancer. All HTTPS traffic is decrypted at the load balancer, and unencrypted traffic goes to your instances.
This is called SSL termination. It simplifies certificate management — you only need to update one certificate instead of every individual server. ACM also provides free SSL certificates.