Load Balancers in AWS

load-balancers-in-aws

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 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
  • Select Application Load Balancer as a type and click create.
    ALB 2
  • Give a name to the load balancer, select internet facing as a scheme and IPv4 as IP address type.
    ALB 3
  • Select availability zone mapping in which load balancer will route the traffic.
    ALB 4
  • 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
  • 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
  • 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
    ALB 8
  • 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
  • Select the newly created target group in load balancer configuration page under Listening and routing section.
    ALB 10
  • 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
  • Click on Add rule option and add a name to the custom rule.
    Rule 2
  • In Define rule conditions, add a path based condition to match /error path.
    Rule 3
  • Under Define rule actions, select Return fixed response option add a response body to be displayed when the /error route is accessed.
    Rule 4
  • 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.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
automating-email-notifications-for-s3-object-uploads-using-sns

Automating Email Notifications for S3 Object Uploads using SNS

Next Post
15-clever-ways-to-save-time-at-work

15 Clever ways to save time at work

Related Posts