Attaching a Load Balancer to Your Auto Scaling Group - Amazon EC2 Auto Scaling

Attaching a Load Balancer to Your Auto Scaling Group

This topic describes how to attach your Elastic Load Balancing load balancer to an existing Auto Scaling group. To attach your load balancer to your Auto Scaling group when you create the group, see Tutorial: Set Up a Scaled and Load-Balanced Application.

Amazon EC2 Auto Scaling integrates with Elastic Load Balancing to enable you to attach one or more load balancers to an existing Auto Scaling group. After you attach the load balancer, it automatically registers the instances in the group and distributes incoming traffic across the instances.

When you attach a load balancer, it enters the Adding state while registering the instances in the group. After all instances in the group are registered with the load balancer, it enters the Added state. After at least one registered instance passes the health checks, it enters the InService state. After the load balancer enters the InService state, Amazon EC2 Auto Scaling can terminate and replace any instances that are reported as unhealthy. If no registered instances pass the health checks (for example, due to a misconfigured health check), the load balancer doesn't enter the InService state. Amazon EC2 Auto Scaling doesn't terminate and replace the instances.

When you detach a load balancer, it enters the Removing state while deregistering the instances in the group. The instances remain running after they are deregistered. If connection draining is enabled, Elastic Load Balancing waits for in-flight requests to complete or for the maximum timeout to expire (whichever comes first) before deregistering the instances. By default, connection draining is enabled for Application Load Balancers but must be enabled for Classic Load Balancers. For more information, see Connection Draining in the User Guide for Classic Load Balancers.

Prerequisites

Before you begin, create an Application Load Balancer or Network Load Balancer in the same AWS Region as the Auto Scaling group. We recommend the new load balancers, but you can still use a Classic Load Balancer if it supports the features you're looking for. To learn more about the different types of load balancers, see Elastic Load Balancing Types.

(Optional) To configure your Auto Scaling group to use Elastic Load Balancing health checks, see Adding Elastic Load Balancing Health Checks to an Auto Scaling Group.

Note

Amazon EC2 Auto Scaling has changed the Auto Scaling group interface. By default, you're shown the old user interface, but you can switch to the new user interface. This topic contains steps for both.

Attach a Load Balancer (Console)

Use the following procedure to attach a load balancer to an existing Auto Scaling group.

To attach a load balancer to a group (new console)

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. On the navigation pane, under AUTO SCALING, choose Auto Scaling Groups.

  3. The original console is open by default. To access the new console, on the banner at the top of the page, choose Go to the new console.

  4. Select the check box next to an existing group.

    A split pane opens up in the bottom part of the Auto Scaling groups page, showing information about the group that's selected.

  5. On the Details tab, choose Load balancing, Edit.

  6. Under Load balancing, do one of the following:

    1. [Application/Network Load Balancers] For Choose a target group for your load balancer, choose your target group.

    2. [Classic Load Balancers] For Choose a load balancer, choose your load balancer.

  7. Choose Update.

To attach a load balancer to a group (old console)

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. On the navigation pane, under AUTO SCALING, choose Auto Scaling Groups.

  3. Choose an existing group from the list.

  4. On the Details tab, choose Edit.

  5. Do one of the following:

    1. [Classic Load Balancers] For Classic Load Balancers, choose your load balancer.

    2. [Application/Network Load Balancers] For Target Groups, choose your target group.

  6. Choose Save.

When you no longer need the load balancer, use the following procedure to detach it from your Auto Scaling group.

To detach a load balancer from a group (new console)

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. On the navigation pane, under AUTO SCALING, choose Auto Scaling Groups.

  3. The original console is open by default. To access the new console, on the banner at the top of the page, choose Go to the new console.

  4. Select the check box next to an existing group.

    A split pane opens up in the bottom part of the Auto Scaling groups page, showing information about the group that's selected.

  5. On the Details tab, choose Load balancing, Edit.

  6. Under Load balancing, do one of the following:

    1. [Application/Network Load Balancers] For Choose a target group for your load balancer, choose the delete icon (X) next to the target group.

    2. [Classic Load Balancers] For Choose a load balancer, choose the delete icon (X) next to the load balancer.

  7. Choose Update.

To detach a load balancer from a group (old console)

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. On the navigation pane, under AUTO SCALING, choose Auto Scaling Groups.

  3. Choose an existing group from the list.

  4. On the Details tab, choose Edit.

  5. Do one of the following:

    1. [Classic Load Balancers] For Classic Load Balancers, remove the load balancer.

    2. [Application/Network Load Balancers] For Target Groups, remove the target group.

  6. Choose Save.

Attach a Load Balancer (AWS CLI)

To attach a target group for an Application Load Balancer or Network Load Balancer

Use the following attach-load-balancer-target-groups command to attach the specified target group to your Auto Scaling group.

aws autoscaling attach-load-balancer-target-groups --auto-scaling-group-name my-asg \ --target-group-arns my-targetgroup-arn

To detach a target group for an Application Load Balancer or Network Load Balancer

Use the following detach-load-balancer-target-groups command to detach a target group from your Auto Scaling group if you no longer need it.

aws autoscaling detach-load-balancer-target-groups --auto-scaling-group-name my-asg \ --target-group-arns my-targetgroup-arn

To attach a Classic Load Balancer

Use the following attach-load-balancers command to attach the specified load balancer to your Auto Scaling group.

aws autoscaling attach-load-balancers --auto-scaling-group-name my-asg \ --load-balancer-names my-lb

To detach a Classic Load Balancer

Use the following detach-load-balancers command to detach a load balancer from your Auto Scaling group if you no longer need it.

aws autoscaling detach-load-balancers --auto-scaling-group-name my-asg \ --load-balancer-names my-lb