Load Balancer - VittorioDeMarzi/hero-beans GitHub Wiki
What is an Application Load Balancer? 🤔
An Application Load Balancer (ALB) is a service in AWS that acts like a smart traffic cop for your website or application. When users send requests to your application, the ALB catches them and distributes them across multiple backend targets, such as EC2 instances.
It operates at the application layer (Layer 7 of the OSI model), which means it can inspect the content of the traffic (like URLs or headers) to make intelligent routing decisions. Think of it as a restaurant host who doesn't just seat guests at any empty table but directs them to specific sections based on their reservation or party size.
Why is it Important? 🚀
Using an Application Load Balancer is crucial for building modern, resilient, and scalable applications.
-
High Availability and Fault Tolerance: If one of your EC2 instances fails or becomes unhealthy, the ALB automatically stops sending traffic to it and reroutes it to the healthy instances. This prevents your users from seeing errors and keeps your application online.
-
Scalability: It allows you to handle sudden spikes in traffic. As demand grows, you can add more EC2 instances in the background, and the ALB will immediately start distributing the load among them without any disruption.
-
Centralized Security: The ALB provides a single, public entry point to your application. This is the perfect place to enforce security. You can attach a Security Group to control what traffic is allowed and a Web Application Firewall (WAF) to protect against common web exploits like SQL injection and cross-site scripting.
-
Simplified SSL/TLS Management: You can install your HTTPS certificate on the load balancer itself. This encrypts traffic between the user and the ALB, and you only have to manage the certificate in one place instead of on every single EC2 instance.
How to Create an Application Load Balancer ✅
This step-by-step guide shows you how to create a public-facing ALB.
Step 1: Initial Configuration
- In the AWS Console, navigate to EC2 → Load Balancers.
- Click Create load balancer.
- Choose Application Load Balancer from the options and click Create.
- Load balancer name: Give it a clear and descriptive name (e.g.,
my-app-alb
). - Scheme: Select Internet-facing, as this load balancer needs to be accessible from the public internet.
- IP address type: Choose IPv4. Select
Dualstack
only if you need to serve both IPv4 and IPv6 clients.
Step 2: Network Mapping
- VPC: Select the VPC where your resources are located.
- Mappings: You must select at least two subnets in different Availability Zones (AZs). This is critical for high availability. For this project, you would select
project-lb-a
andproject-lb-b
.
Step 3: Security Groups and Listeners
- Security group: Detach the default security group and attach the one you created specifically for the load balancer. For this project, you would select
project-lb
. - Listeners and routing: A listener checks for connection requests.
- Set the protocol to HTTP and the port to 80.
- For the Default action, select Forward to... and choose the Target Group that contains your EC2 instances. The ALB will forward all incoming HTTP traffic to the instances in that group.
Step 4: Add-on Services (Optional but Recommended)
- AWS WAF web ACL: To protect your application from common web exploits, select your pre-configured WAF. For this project, you would choose
techcourse-project-waf
. Note: The WAF must be enabled for requests to be accepted.
Step 5: Review and Create
- Look over the Summary page to ensure all the settings are correct.
- Click Create load balancer.
After a few moments, your ALB will be provisioned and active. You can then use its public DNS name to access your application.