Menu
Auto Scaling
User Guide

Attaching a Load Balancer to Your Auto Scaling Group

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. To use an Elastic Load Balancing health check with your instances to ensure that traffic is routed only to the healthy instances, see Using ELB Health Checks with Auto Scaling.

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, Auto Scaling can terminate and replace any instances that are reported as unhealthy. Note that 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, so Auto Scaling wouldn't terminate and replace the instances.

When you detach a load balancer, it enters the Removing state while deregistering the instances in the group. Note that 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. Note that connection draining is always 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.

Elastic Load Balancing sends data about your load balancers and EC2 instances to Amazon CloudWatch. CloudWatch collects performance data for your resources and presents it as metrics. For more information, see Monitoring Your Auto Scaling Groups and Instances Using Amazon CloudWatch. After you attach a load balancer to your Auto Scaling group, you can create scaling policies that use Elastic Load Balancing metrics to scale your application automatically. For more information, see Create an Auto Scaling Group with Target Tracking Scaling Policies.

Prerequisites

Before you begin, create a load balancer in the same 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 the Elastic Load Balancing User Guide.

With Classic Load Balancers, instances are registered with the load balancer. With Application Load Balancers and Network Load Balancers, instances are registered as targets with a target group. When you plan to use your load balancer with an Auto Scaling group, you don't need to register your EC2 instances with the load balancer or target group. After you attach a load balancer or target group to your Auto Scaling group, Auto Scaling registers your instances with the load balancer or target group when it launches them.

Add a Load Balancer Using the 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 Auto Scaling 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. Select your group.

  4. On the Details tab, choose Edit.

  5. Do one of the following:

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

    2. [Target groups] For Target Groups, select 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. Select your group.

  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. [Target groups] For Target Groups, remove the target group.

  6. Choose Save.

Add a Load Balancer Using the 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:

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

To attach a target group

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

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

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

To detach a target group

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:

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