Amazon EC2 Auto Scaling
User Guide

Attaching a Load Balancer to Your Auto Scaling Group

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 a load balancer in the same AWS Region as the Auto Scaling group. Elastic Load Balancing supports three types of load balancers: Application Load Balancers, Network Load Balancers, and Classic Load Balancers. You can attach any of these types of load balancers to your Auto Scaling group. For more information, see Elastic Load Balancing Types .

To configure the Auto Scaling group to use Elastic Load Balancing health check, see Adding Elastic Load Balancing Health Checks to an Auto Scaling Group.

Add a Load Balancer (Console)

Use the following procedure to attach a 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.

To attach a load balancer to a group

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

  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 Load Balancers, remove the load balancer.

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

  6. Choose Save.

Add a Load Balancer (AWS CLI)

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

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