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:
- Classic Load Balancer (old generation)
- Application Load Balancer
- Network Load Balancer
- 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 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 onCreate load balancer
option. - Select
Application Load Balancer
as a type and click create. - Give a name to the load balancer, select internet facing as a scheme and
IPv4
as IP address type. - Select availability zone mapping in which load balancer will route the traffic.
- 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.
- 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.
- On
Create target group
page, selectInstances
as a target type and add a name for the target group. You can keep other settings as default. - 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 onCreate target group
option. - Select the newly created target group in load balancer configuration page under
Listening and routing section
. - 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 defaultHTTP:80
listener. - Click on
Add rule
option and add a name to the custom rule. - In
Define rule conditions
, add a path based condition to match/error
path. - Under
Define rule actions
, selectReturn fixed response
option add a response body to be displayed when the/error
route is accessed. - 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 to learn more about load balancing in AWS.
Top comments (0)