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 insert one or more Classic Load Balancers or a single Application Load Balancer or Network Load Balancer with multiple target groups in front of your Auto Scaling group. To learn more about the different types of load balancers, see Elastic Load Balancing types.

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.

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

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

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

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

  6. Choose Update.

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

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

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

  6. Choose Update.

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