Skip to content

ALB


When traffic is inbound from the Internet (or indeed from internal traffic, but we don't have any of that) we need to send it on to our servers, but which server should get the traffic? The job of a load balancer is to decide that for us. Given a pool of servers, the load balancer can determine which server is the best choice to send the user's traffic to. It does this based on a few things, and they're generally all configurable:

  1. Which server has the least traffic?
  2. Which server has the least load on its CPU, disk, RAM
  3. Which server hasn't had any traffic yet?

And probably more importantly than any of those metrics: what servers are currently working and available to serve traffic? If one or more servers are failing to pass health checks, then the load balancer considers those servers as failed and does not send traffic to them. That's why we're configuring at least two servers even though we could just use one for our use case (educational purposes.)

There's a lot you can do to configure a load balancer, helping it to make better decisions about how and where to route traffic. At this point in time we'll be keeping things simple and just using what is referred to as "round robin": we send the first request to the first server, the second to the second server, the third to first, the fourth to the second, and so on, in a yo-yo, a-b-a-b like system. It looks a bit like this:

graph TD
    A[Traffic] -- This Way --> B[Server A]
    A[Traffic] --> C[Server B]

Then after some more traffic comes in, it'll do something like this:

graph TD
    A[Traffic] --> B[Server A]
    A[Traffic] -- This Way --> C[Server B]

With more servers, it might look a bit like this:

graph TD
    A[Traffic] --> B[Server A]
    A[Traffic] --> C[Server B]
    A[Traffic] -- This Way --> D[Server C]
    A[Traffic] --> E[Server D]
    A[Traffic] --> F[Server E]

We'll be keeping things pretty simple with only two servers at this point in time.

To accomplish this goal for our application, we're going to be using an AWS Application Load Balancer (ALB). This handy service from AWS will handle the inbound HTTPS connections and equally distribute the requests between two servers. In the event one of the servers fails, it will stop sending traffic to it automatically for us.

Our usage of the ALB will be very simple: just load balance traffic across two static EC2 instances.

Implementation Details

Name Description
httpcats The primary HTTPS network entry point for the HTTP DevOps Cats application

Subnets

Subnet AZ
aws_subnet.httpcats-http-az-a.id ap-southeast-2a
aws_subnet.httpcats-http-az-b.id ap-southeast-2b

Security Groups

Security Group ID
aws_security_group.alb.id

Listeners

Port Protocol Certificate ARN
443 HTTPS aws_acm_certificate_validation.cert.certificate_arn

Default Action

Type Content Type Status Code Message Body
fixed-response text/plain 404 Meow?

Rules

Priority Action Target Conditions
100 forward aws_alb_target_group.cats.arn path_pattern{ values = ["*] }

Target Group

Name Port Protocol VPC ID
httpcats-web-servers 8080 HTTP aws_vpc.httpcats.id

Last update: August 25, 2021