Load Balancers in AWS
Tags: aws, cloud
Published: 2024-06-30 18:12:23

Load balancers are servers that forward traffic to multiple servers downstream. They are crucial for distributing incoming traffic across different servers such as EC2 instances, in multiple Availability Zones. This increases high availability of your application. A load balancer ensures that no single server bears too much load, thus enhancing the performance and reliability of your application.

AWS Elastic Load Balancer (ELB) is a managed load balancer. It is integrated with many AWS services including EC2, ECS, Route53, and CloudWatch. While it might be costlier than setting up your own load balancer, the time and effort saved in managing and configuring ELB make it a preferred choice for many.

AWS Elastic Load Balancer has following types of managed load balancers:
1. Classic Load Balancer (old generation)
2. Application Load Balancer
3. Network Load Balancer
4. Gateway Load Balancer

Classic Load Balancer (CLB) comes under the old generation in AWS. It is recommended to use the newer generation of load balancers as they provide more features.

Following are some of the key features of load balancers:
- They distribute traffic across multiple downstream instances, ensuring efficient handling of requests.
- They provide a single DNS point of access for your application.
- They can seamlessly manage failures in downstream instances.
- They perform regular health checks on your instances to ensure only healthy instances receive traffic.
- They can operate across multiple AZs for high availability.
- They can segment public and private traffic.

In this article, we will explore Application Load Balancer (ALB) in detail.
### Application Load Balancer (ALB)
An Application Load Balancer (ALB) operates at application layer of the OSI model making it ideal for HTTP/HTTPS traffic. It supports advanced routing mechanisms such as:
- Path based routing
- Hostname based routing
- Query string or header based routing

ALB can route traffic to multiple target groups, including EC2 instances, ECS tasks, Lambda functions, or private IP addresses. It supports modern HTTP, HTTPS, HTTP/2, WebSocket protocols. ALB provides a single DNS name that clients can use to access your application thus simplifying DNS management.

Let's create a simple Application Load Balancer between two EC2 instances. The purpose of this load balancer will be to distribute traffic to EC2 instances. To follow the below steps, you need to create minimum 2 EC2 instances. You can refer [this](https://dev.to/vivekalhat/beginners-guide-to-aws-ec2-2adk) article to learn about creating a new EC2 instance.
### Steps for creating a new Application Load Balancer
- On EC2 homepage, select `Load Balancers` option in the menu and click on `Create load balancer` option.
![ALB 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v2vwfidz5guvlnyzkqa3.png)
- Select `Application Load Balancer` as a type and click create.
![ALB 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xiyhwofnhfbvjyhqhvz8.png)
- Give a name to the load balancer, select internet facing as a scheme and `IPv4` as IP address type.
![ALB 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bfb5yi0g1f730iwv36rm.png)
- Select availability zone mapping in which load balancer will route the traffic.
![ALB 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fkgpvd6lylhssvodaqf6.png)
- Select or create a new security group for load balancer. You can add inbound rules specific to your use case to allow traffic to the EC2 instances.
![ALB 5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvcp7eu7ozyud2dijkyc.png)
- Create a new target group to which load balancer will route the incoming traffic. In this example, we will be routing the traffic via load balancers to two EC2 instances.
![ALB 6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wrkst15jes8olhpjnydt.png)
- On `Create target group` page, select `Instances` as a target type and add a name for the target group. You can keep other settings as default.
![ALB 7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/phurrm0lorkpqf16sy72.png)
![ALB 8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4m1cib1p6jpumboucdx1.png)
- On the register targets page, select the EC2 instances to which load balancer will route the incoming traffic and click on `Include as pending below` option. After registering the targets, click on `Create target group` option.
![ALB 9](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xlx7m2jl2nsq6mtwg8ls.png)
- Select the newly created target group in load balancer configuration page under `Listening and routing section`.
![ALB 10](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kycndshtl7mt1el3vvyz.png)
- Click on `Create load balancer` option to create a new load balancer to route traffic between the selected target group based on the configuration.

After creating a new load balancer, any incoming traffic to the EC2 instances will be handled by the rules mentioned in the load balancer. You can also specify custom rules inside the load balancer. Let's create a new custom rule to handle error route in the application.

### Creating a custom rule in Load Balancer
- Select the load balancer and under `Listeners and rules` section, select the default `HTTP:80` listener.
![Rule 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dw92uknzd8k7c1vwq910.png)
- Click on `Add rule` option and add a name to the custom rule.
![Rule 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjdpqqb0fzshxfquort7.png)
- In `Define rule conditions`, add a path based condition to match `/error` path.
![Rule 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c31paep9jy7c8p76svch.png)
- Under `Define rule actions`, select `Return fixed response` option add a response body to be displayed when the `/error` route is accessed.
![Rule 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekz1g1lzcdedd2a3fx9u.png)
- Set rule priority as `1` and click next and create the custom rule.

After creating a custom rule, if you access the `/error` path on the Load Balancer's DNS address, you will see the custom error response body as configured.

In this way, you can create a load balancer and custom rules using AWS Elastic Load Balancer. You can refer the official user [guide](https://aws.amazon.com/elasticloadbalancing/) to learn more about load balancing in AWS.