Skip to content

How do I set up weighted target groups for my Application Load Balancer?

5 minute read
1

I want to register weighted target groups for my Application Load Balancer.

Resolution

You can assign different weights to target groups for your Application Load Balancer to distribute traffic proportionally.

Create target groups

To create the target groups that you want to register with your Application Load Balancer, complete the following steps:

  1. Open the Amazon Elastic Compute Cloud (Amazon EC2) console.
  2. Select the AWS Region that your Amazon EC2 instances are located in.
  3. In the navigation pane, under Load balancing, choose Target groups.
  4. To create the first target group, complete the following steps:
    Choose Create target group.
    For Target group name, specify a name for your target group.
    Configure the protocol, port, and virtual private cloud (VPC) for the target group.
    Choose Create.
    For Instances, select one or more instances.
    Specify a port for the instances.
    Choose Include as pending below, and then choose Create target group.
  5. To set up weighted target groups, you must create at least two target groups. Repeat step 4 to create a second target group.

Create an Application Load Balancer

Important: If you have an Application Load Balancer that you would like to use, then continue to the Configure listener rules and assign weights section. To create an Application Load Balancer, complete the following steps:

  1. In the navigation pane, under Load balancing, choose Load Balancers.
  2. Choose Create load balancer.
  3. For Select load balancer type, choose Application Load Balancer, then choose Create.
  4. In the Create Application Load Balancer wizard, complete the following steps:
    Enter a name for your load balancer.
    Choose Internet-facing or Internal.
    Choose IP Address type.
    Select your VPC and at least two Availability Zones.
    For Security groups, select or create a security group.
    For Listeners and routing, keep the default HTTP listener on port 80.
  5. On the Configure routing page, complete the following steps:
    For Target group, choose Existing Target.
    For Name, select the first target group that you created.
    Choose Next: Register Targets.
  6. On the Register Targets page, confirm that your registered instances appear under Registered instances.
    Note: To modify targets, return to the Target Groups console. The Register Targets page is for review only.
  7. On the Review page, choose Create.
  8. After you create the Application Load Balancer, choose Close.
  9. After the creation wizard closes, you return to the Load Balancer page. Select your new load balancer from the load balancers list.

Configure listener rules and assign weights

To assign weights to the target groups, complete the following steps configure the listener rules for your Application Load Balancer:

  1. On the Listeners tab, choose View/edit rules.
  2. Choose Edit rules.
  3. Next to Forward to, choose Edit.
  4. Add the second target group that you created earlier.
  5. Enter a value for your target group weight between 0 and 999.
  6. Select the Check mark, and then choose Update.
  7. (Optional) If the target group is sticky, then set the Group level stickiness.
    Note: When you configure group level stickiness, routed requests remain in the target group for the duration of the session. The default value is 1 hour. After the session duration ends, the load balancer distributes requests based on the target group weights.

The Application Load Balancer distributes traffic based on target group weights. If all targets in a group fail the health checks, your Application Load Balancer doesn't failover to another group. The balancer continues to route traffic to the unhealthy targets. It's a best practice to not use weighted target groups as a failover mechanism.

If the weight of the first target is 70% and the second target is 30%, then most requests are from the first target group.

Example command:

for X in seq 6; do curl -so -i /dev/null -w "" http://FINAL-721458494.us-east-2.elb.amazonaws.com; done

Example output:

<h1> This is T1 </h1>
<h1> This is T1 </h1>
<h1> This is T1 </h1>
<h1> This is T1 </h1>
<h1> This is T2 </h1>
<h1> This is T2 </h1>

In this example, T1 represents responses from the first target group (70% weight) and T2 represents responses from the second target group (30% weight).

If the weight of the second target is 70% and the first target is 30%, then most requests are from the second target group.

Example command:

for X in seq 7; do curl -so -i /dev/null -w "" http://FINAL-721458494.us-east-2.elb.amazonaws.com; done

Example output:

<h1> This is T2 </h1>
<h1> This is T2 </h1>
<h1> This is T2 </h1>
<h1> This is T1 </h1>
<h1> This is T1 </h1>
<h1> This is T2 </h1>
<h1> This is T2 </h1>

In this example, T2 represents responses from the second target group (70% weight) and T1 represents responses from the first target group (30% weight).Enter a name for your load balancer.