Route Traffic with Amazon Elastic Load Balancing

Learning Objectives

After completing this unit, you will be able to:

  • Explain the function of a load balancer.
  • Explain the integration between Elastic Load Balancing (ELB) and Amazon EC2 Auto Scaling.
  • Follow health check setup best practices.

In the previous units, you learned about the benefits of having more than one server to handle the traffic for your cat photo app and how you can automatically scale. How do you get traffic to all those instances? How would the client even know when you add new ones or remove others? 

This is all done using a load balancer.

What’s a Load Balancer?

Load balancing refers to the process of distributing tasks across a set of resources. In the case of your cat photo application, the resources are EC2 instances that handle the application, and the tasks are the different requests being sent. It’s time to distribute the requests across all those servers using a load balancer. 

You first need to enable the load balancer to take all of the traffic and redirect it to the backend servers based on an algorithm. The most popular algorithm is round-robin, which sends the traffic to each server one after the other.

A request from the client is sent to an Elastic Load Balancer and is then directed to an EC2 instance.

A typical request for the cat photo application would start from the browser of the client. It’s sent to a load balancer. Then, it’s sent to one of the EC2 instances that hosts the cat photo application. The return traffic would go back through the load balancer and back to the client browser. Thus, the load balancer is directly in the path of the traffic.

Although it is possible to install your own software load balancing solution on EC2 instances, AWS provides a service for that called Elastic Load Balancing (ELB).

Know the Features of Elastic Load Balancing

The ELB service provides a major advantage over using your own solution to do load balancing, in that you don’t need to manage or operate it. It can distribute incoming application traffic across EC2 instances as well as containers, IP addresses, and AWS Lambda functions.

  • The fact that ELB can load balance to IP addresses means that it can work in a hybrid mode as well, where it also load balances to on-premises servers.
  • ELB takes care of the high availability for you so that you don’t need to think about it. The only option you have to ensure is that the load balancer is deployed across multiple Availability Zones.
  • In terms of scalability, ELB automatically scales depending on the traffic so you don’t need to worry about it. It handles the incoming traffic and sends it to your backend application.

Integrate ELB with EC2 Auto Scaling

The ELB service integrates seamlessly with EC2 Auto Scaling. As soon as a new EC2 instance is added to or removed from the EC2 Auto Scaling group, ELB is notified. However, before it can send traffic to a new EC2 instance, it needs to validate that the application running on that EC2 instance is available.

This validation is done via the health checks feature of ELB. Monitoring is an important part of load balancers, as it should route traffic to only healthy EC2 instances. That’s why ELB supports two types of health checks. 

  • Establishing a connection to a backend EC2 instance using TCP, and marking the instance as available if that connection is successful.
  • Making an HTTP or HTTPS request to a webpage that you specify, and validating that an HTTP response code is returned.

Define the Right Health Check

Taking the time to define an appropriate health check is critical. Only verifying that the port of an application is open doesn’t mean that the application is working. It also doesn’t mean that simply making a call to the home page of an application is the right way either. 

ELB health check that calls the webpage /monitor.php to ensure that local storage, the database, and the remote API are configured properly and working.The cat photo application depends on local storage, a database, and S3. The health check should validate all of those elements. One way to do that would be to create a monitoring webpage like “/monitor” that will test if the local storage is working, make a call to the database to ensure it can connect and get data, and make a call to S3. Then, you point the health check on the load balancer to the “/monitor” page.

After determining the availability of a new EC2 instance, the load balancer starts sending traffic to it. If ELB determines that an EC2 instance is no longer working, it stops sending traffic to it and lets EC2 Auto Scaling know. EC2 Auto Scaling’s responsibility is to remove it from the group and replace it with a new EC2 instance. Traffic only sends to the new instance if it passes the health check.

In the case of a scale down action that EC2 Auto Scaling needs to take due to a scaling policy, it lets ELB know that EC2 instances will be terminated. ELB can prevent EC2 Auto Scaling from terminating the EC2 instance until all connections to that instance end, while preventing any new connections. That feature is called connection draining.

Learn How to Use ELB

A load balancer is made up of listeners, target groups, rules, and health checks.

The ELB service is made up of three main components. 

  • Listeners: The client connects to the listener. This is often referred to as client-side. To define a listener, a port must be provided as well as the protocol, depending on the load balancer type. There can be many listeners for a single load balancer.
  • Target groups: The backend servers, or server-side, is defined in one or more target groups. This is where you define the type of backend you want to direct traffic to, such as EC2 Instances, AWS Lambda functions, or IP addresses. Also, a health check needs to be defined for each target group.
  • Rules: To associate a target group to a listener, a rule must be used. Rules are made up of a condition that can be the source IP address of the client and a condition to decide which target group to send the traffic to.

Learn how all this comes together in the next unit.

Wrap Up

In this unit, you learned about load balancing technology and the advantages of using the ELB service. One of those features is the integration with the EC2 Auto Scaling service to automatically scale your backend infrastructure. 

In the next unit, you learn about the features of the different types of ELB and how to choose the appropriate one for your cat photo application.


Keep learning for
Sign up for an account to continue.
What’s in it for you?
  • Get personalized recommendations for your career goals
  • Practice your skills with hands-on challenges and quizzes
  • Track and share your progress with employers
  • Connect to mentorship and career opportunities