# AWS DevOps- Day25

# A Beginner's Guide to AWS Load Balancers 🌐

Load balancing is a critical aspect of cloud computing that ensures your applications remain reliable, scalable, and available. AWS offers three main types of load balancers—Application Load Balancer (ALB), Network Load Balancer (NLB), and Gateway Load Balancer (GWLB)—each tailored to specific use cases. In this beginner-friendly guide, we’ll explore the basics of these load balancers and provide a step-by-step walkthrough for setting one up. 🚀

## Understanding AWS Load Balancers 🤔

### **1\. Application Load Balancer (ALB)**

The ALB operates at the application layer (Layer 7 of the OSI model) and is designed for advanced load balancing of HTTP and HTTPS traffic. Key features include:

* **Content-based routing**: Route traffic based on URL paths or host headers.
    
* **WebSocket Support**: Ideal for real-time communication applications.
    
* **Target Groups**: Route traffic to specific EC2 instances, IP addresses, or Lambda functions.
    

💡 **Use Case**: Choose ALB for complex web applications requiring intelligent traffic management.

### **2\. Network Load Balancer (NLB)**

The NLB operates at the transport layer (Layer 4) and is optimized for high-performance TCP, UDP, and TLS traffic. Key features include:

* **Low latency**: Designed for applications requiring ultra-fast response times.
    
* **High throughput**: Can handle millions of requests per second.
    
* **Static IP**: Offers a single static IP per availability zone.
    

💡 **Use Case**: Ideal for real-time applications, gaming servers, or workloads requiring extremely low latency.

### **3\. Gateway Load Balancer (GWLB)**

The GWLB operates at the network layer and is specifically designed for routing traffic through third-party appliances like firewalls and intrusion detection systems. Key features include:

* **Traffic Flow Management**: Route traffic securely through security appliances.
    
* **Scalability**: Automatically scales with traffic demand.
    

💡 **Use Case**: Best suited for VPNs, firewalls, and security-focused applications.

## Setting Up Load Balancers on AWS 🛠️

Follow this step-by-step guide to set up an AWS load balancer and start distributing traffic across your application servers.

### **Prerequisites**:

1. **AWS Account**: Make sure you have an active AWS account. Sign up [here](https://aws.amazon.com/free/) if you don’t have one.
    
2. **IAM Role/Policy**: Ensure your user/role has permissions like `ElasticLoadBalancingFullAccess` and `AmazonEC2FullAccess`.
    

### **Step 1: Launch EC2 Instances**

1. Go to the **EC2 Dashboard** in the AWS Management Console.
    
2. Click **Launch Instances** and configure at least two instances:
    
    * Choose an Amazon Machine Image (AMI), such as Amazon Linux 2.
        
    * Select an instance type (e.g., `t2.micro` for Free Tier).
        
    * Configure security group rules to allow necessary traffic (e.g., HTTP on port 80).
        
    * Launch the instances.
        
3. Once the instances are running, note down their **Instance IDs** or **Private IPs** (you’ll need these later).
    

### **Step 2: Navigate to Load Balancers**

1. In the AWS Management Console, go to **EC2 Dashboard** &gt; **Load Balancers** (under "Load Balancing").
    
2. Click **Create Load Balancer**.
    

### **Step 3: Choose Your Load Balancer Type**

You’ll see three options:

* **Application Load Balancer (ALB)**
    
* **Network Load Balancer (NLB)**
    
* **Gateway Load Balancer (GWLB)**
    

For this guide, we’ll set up an **Application Load Balancer (ALB)**. Select ALB and click **Create**.

### **Step 4: Configure Your Load Balancer**

1. **Basic Configuration**:
    
    * **Name**: Give your load balancer a descriptive name (e.g., `My-ALB`).
        
    * **Scheme**: Choose **Internet-facing** (for public traffic) or **Internal** (for private applications).
        
    * **IP Address Type**: Choose IPv4.
        
2. **Listeners**:
    
    * Ensure there’s a default listener for **HTTP** on port **80**.
        
    * If you want secure traffic (HTTPS), set up an SSL certificate.
        
3. **Availability Zones**:
    
    * Select the VPC where your EC2 instances are running.
        
    * Choose at least two subnets from different availability zones for redundancy.
        

### **Step 5: Create a Target Group**

1. **Target Group Type**: Select **Instances** as the target type.
    
2. **Name**: Provide a name for the target group (e.g., `My-Target-Group`).
    
3. **Protocol**: Choose **HTTP** for this example.
    
4. **Health Checks**:
    
    * Use the default health check settings or configure a custom path (e.g., `/health`).
        
5. Click **Next** and add your EC2 instances to the target group.
    
    * Select your instances from the list and click **Include as Pending Below**.
        
    * Click **Create Target Group**.
        

### **Step 6: Finalize and Create the Load Balancer**

1. **Associate Target Group**:
    
    * On the load balancer setup page, choose the target group you just created.
        
2. **Review Configuration**: Double-check all the settings.
    
3. Click **Create Load Balancer**.
    

### **Step 7: Test Your Load Balancer**

1. Once the load balancer is created, go back to the **Load Balancer** section in the console.
    
2. Copy the **DNS name** of your load balancer (found in the details section).
    
3. Open the DNS name in a browser. If everything is set up correctly, you should see the content from one of your EC2 instances. The load balancer will automatically distribute traffic between instances.
    

### **Optional: Add HTTPS for Secure Traffic**

1. Go to **Listeners** for your load balancer and click **Add Listener**.
    
2. Choose **HTTPS** and upload an SSL certificate.
    
3. Configure the target group to handle secure traffic.
    

### Common Troubleshooting Tips 🔍

1. **Health Check Failing?**: Verify the health check path and ensure your instances respond correctly to the specified endpoint.
    
2. **Traffic Not Reaching Instances?**: Check your security group rules. Both the load balancer and instances need rules to allow traffic on the desired ports.
    
3. **DNS Not Resolving?**: Ensure your load balancer is in a public subnet if it’s Internet-facing.
    

By following these steps, you’ll have a working load balancer distributing traffic across your EC2 instances. 🎉 This not only ensures better application performance but also enhances reliability by preventing single points of failure. Happy Load Balancing! 😊
