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

Last updated: 2020-05-14

I want to register weighted target groups for my Application Load Balancer. How can I do this?

Short Description

You can use a target group registered with a load balancer to direct traffic to Amazon Elastic Compute Cloud (Amazon EC2) instances, IP addresses, or AWS Lambda functions. With weighted routing, you can switch traffic between different versions of your applications. This configuration allows you to control the distribution of traffic to your applications. You can add more than one target group to the forward action of a listener rule, and specify a weight for each group. This feature is often used for blue-green deployments.


Create target groups

1.    Open the Amazon EC2 console.

2.    On the navigation bar, choose the Region that you selected for your EC2 instances.

3.    On the navigation pane, under LOAD BALANCING, choose Target Groups.

4.    Create the first target group:
Choose Create Target group.
For Target group name, specify a name for the 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 Add to registered, and then choose Save.

5.    Repeat step 4 to create a second target group.

Create an Application Load Balancer (if you don't already have one)

1.    On the navigation pane, under LOAD BALANCING, choose Load Balancers.

2.    Choose Create Load Balancer.

3.    For Select load balancer type, choose Application Load Balancer.

4.    Choose Continue.

5.    Complete the steps in Create an Application Load Balancer.

6.    Complete the Configure Routing steps:
For Target group, choose Existing Target.
For Name, choose the first target group that you created.
Choose Next: Register Targets.

7.    On the Register Targets page, notice that the instances you registered with the target group appear under Registered instances. You can't modify the targets registered with the target group here.

8.    On the Review page, choose Create.

9.    After you're notified that your load balancer was created successfully, choose Close.

10.    Select the newly created load balancer.

Configure the Listener rules and add weight to the target groups

1.    On the Listeners tab, choose View/edit rules.

2.    Choose Edit rules (the pencil icon).

3.    Select Edit next to the Forward to option.

4.    Add the other target group.

5.    Enter the target group weight values. These values must be numeric values between 0 and 999.

6.    Select the check mark and choose Update.

7.    (Optional) Set the Group level Stickiness if the target group is sticky. With this setting configured, routed requests remain in the target group for the duration of the session. The default value is one hour. After the duration of stickiness ends, requests are distributed according to the weights of the target group.

Note: The Application Load Balancer distributes traffic only to the target groups based on their weights. However, if all targets in a particular target group fail health checks, the Application Load Balancer doesn't automatically fail over. Instead, the Application Load Balancer sends all requests to the other target group.

For example, if the weight of the first target is 70%, and the second target is 30%, the majority of requests are from the first target group:

$ for X in `seq 6`; do curl -so -i /dev/null -w ""; done

<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>

If you switch the weights and set the weight of the second target as 70% and the first as 30%, the majority of requests are from the second target group:

$ for X in `seq 7`; do curl -so -i /dev/null -w ""; done

<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>

Did this article help?

Do you need billing or technical support?