

# Use Elastic Load Balancing to distribute incoming application traffic in your Auto Scaling group
<a name="autoscaling-load-balancer"></a>

Elastic Load Balancing automatically distributes your incoming application traffic across all the EC2 instances that you are running. Elastic Load Balancing helps to manage incoming requests by optimally routing traffic so that no one instance is overwhelmed. To use Elastic Load Balancing with your Auto Scaling group, [attach the load balancer to your Auto Scaling group](attach-load-balancer-asg.md). This registers the group with the load balancer, which acts as a single point of contact for all incoming web traffic to your Auto Scaling group. 

When you use Elastic Load Balancing with your Auto Scaling group, it's not necessary to register individual EC2 instances with the load balancer. Instances that are launched by your Auto Scaling group are automatically registered with the load balancer. Likewise, instances that are terminated by your Auto Scaling group are automatically deregistered from the load balancer.

After attaching a load balancer to your Auto Scaling group, you can configure your Auto Scaling group to use Elastic Load Balancing metrics (such as the Application Load Balancer request count per target) to scale the number of instances in the group as demand fluctuates.

Optionally, you can add Elastic Load Balancing health checks to your Auto Scaling group so that Amazon EC2 Auto Scaling can identify and replace unhealthy instances based on these additional health checks. Otherwise, you can create a CloudWatch alarm that notifies you if the healthy host count of the target group is lower than allowed. 

**Topics**
+ [Elastic Load Balancing types](#integrations-aws-elastic-load-balancing-types)
+ [Prepare to attach a load balancer](getting-started-elastic-load-balancing.md)
+ [Attach a load balancer](attach-load-balancer-asg.md)
+ [Configure a load balancer](as-create-load-balancer-console.md)
+ [Verify the attachment status](load-balancer-status.md)
+ [Add an Availability Zone](as-add-az-console.md)
+ [Remove an Availability Zone](as-remove-az-console.md)
+ [Detach a load balancer](as-remove-load-balancer.md)
+ [AWS CLI examples for working with Elastic Load Balancing](examples-elastic-load-balancing-aws-cli.md)

## Elastic Load Balancing types
<a name="integrations-aws-elastic-load-balancing-types"></a>

Elastic Load Balancing provides four types of load balancers that can be used with your Auto Scaling group: Application Load Balancers, Network Load Balancers, Gateway Load Balancers, and Classic Load Balancers. 

There is a key difference in how the load balancer types are configured. With Application Load Balancers, Network Load Balancers, and Gateway Load Balancers, instances are registered as targets with a target group, and you route traffic to the target group. With Classic Load Balancers, instances are registered directly with the load balancer. 

Application Load Balancer  
Routes and load balances at the application layer (HTTP/HTTPS), and supports path-based routing. An Application Load Balancer can route requests to ports on one or more registered targets, such as EC2 instances, in your virtual private cloud (VPC).

Network Load Balancer  
Routes and load balances at the transport layer (TCP/UDP Layer-4), based on address information extracted from the Layer-4 header. Network Load Balancers can handle traffic bursts, retain the source IP of the client, and use a fixed IP for the life of the load balancer. 

Gateway Load Balancer  
Distributes traffic to a fleet of appliance instances. Provides scale, availability, and simplicity for third-party virtual appliances, such as firewalls, intrusion detection and prevention systems, and other appliances. Gateway Load Balancers work with virtual appliances that support the GENEVE protocol. Additional technical integration is required, so make sure to consult the user guide before choosing a Gateway Load Balancer. 

Classic Load Balancer  
Routes and load balances either at the transport layer (TCP/SSL), or at the application layer (HTTP/HTTPS).

To gain a deeper understanding of the different types of load balancers available, see the following resources:
+ [What is Elastic Load Balancing?](https://docs.aws.amazon.com/elasticloadbalancing/latest/userguide/what-is-load-balancing.html)
+ [What is an Application Load Balancer?](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/introduction.html)
+ [What is a Network Load Balancer?](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/introduction.html)
+ [What is a Gateway Load Balancer?](https://docs.aws.amazon.com/elasticloadbalancing/latest/gateway/introduction.html)
+ [What is a Classic Load Balancer?](https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/introduction.html)

# Prepare to attach an Elastic Load Balancing load balancer
<a name="getting-started-elastic-load-balancing"></a>

Before you attach an Elastic Load Balancing load balancer to your Auto Scaling group, you must complete the following prerequisites:
+ You must have already created the load balancer and target group that is used to route traffic to your Auto Scaling group. 

  There are two ways to create the load balancer and target group:
  + **Using Elastic Load Balancing** – Follow the procedures in the Elastic Load Balancing documentation to create and configure the load balancer and target group before creating the Auto Scaling group. Skip the step for registering your Amazon EC2 instances. Amazon EC2 Auto Scaling automatically takes care of registering (and deregistering) instances when you attach a target group to your Auto Scaling group. For more information, see [Getting started with Elastic Load Balancing](https://docs.aws.amazon.com/elasticloadbalancing/latest/userguide/load-balancer-getting-started.html) in the *Elastic Load Balancing User Guide*. 
  + **Using Amazon EC2 Auto Scaling** – Create, configure, and attach the load balancer and target group with a basic configuration from the Amazon EC2 Auto Scaling console. For more information, see [Configure an Application Load Balancer or Network Load Balancer from the console](as-create-load-balancer-console.md). 
+ Before creating a load balancer, know the type of load balancer that you need. For more information, see [Elastic Load Balancing types](autoscaling-load-balancer.md#integrations-aws-elastic-load-balancing-types).
+ The load balancer and its target group must be in the same AWS account, VPC, and Region as your Auto Scaling group.
+ The target group must specify a target type of `instance`. You can't specify a target type of `ip` when using an Auto Scaling group.
+ If the launch template for your Auto Scaling group does not contain the correct security group to allow the necessary inbound traffic from the load balancer, you must update the launch template. The recommended rules depend on the type of load balancer and the types of backends that the load balancer uses. For example, to route traffic to web servers, allow inbound HTTP access on port 80 from the load balancer. Existing instances are not updated with the new settings when the launch template is modified. To update existing instances, you can start an instance refresh to replace the instances. For more information, see [Use an instance refresh to update instances in an Auto Scaling group](asg-instance-refresh.md).
+ The security groups in the launch template must also allow access from the load balancer on the correct port for Elastic Load Balancing to perform its health checks. 
+ When deploying virtual appliances behind a Gateway Load Balancer, the Amazon Machine Image (AMI) in the launch template must specify the ID of an AMI that supports the GENEVE protocol to allow the Auto Scaling group to exchange traffic with a Gateway Load Balancer. Also, the security groups in the launch template must allow UDP traffic on port 6081.

**Tip**  
If you have bootstrapping scripts that take a while to complete, you can optionally add a launch lifecycle hook to your Auto Scaling group to delay instances from registering behind the load balancer before your bootstrap scripts have completed successfully and the applications on the instances are ready to accept traffic. You can't add a lifecycle hook when you initially create an Auto Scaling group in the Amazon EC2 Auto Scaling console. However, you can add a lifecycle hook after the group is created. For more information, see [Amazon EC2 Auto Scaling lifecycle hooks](lifecycle-hooks.md). 

## Configure health checks for targets
<a name="elb-health-checks-for-targets"></a>

You can configure health checks for your targets registered with an Elastic Load Balancing load balancer to ensure they are able to handle traffic properly. The specific steps vary based on the type of load balancer you are using. For more information, see the following resources:
+ **Application Load Balancer** – See [Health checks for your target groups](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/target-group-health-checks.html) in the *User Guide for Application Load Balancers*.
+ **Network Load Balancer** – See [Health checks for your target groups](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/target-group-health-checks.html) in the *User Guide for Network Load Balancers*.
+ **Gateway Load Balancer** – See [Health checks for your target groups](https://docs.aws.amazon.com/elasticloadbalancing/latest/gateway/health-checks.html) in the *User Guide for Gateway Load Balancers*.
+ **Classic Load Balancer** – See [Configure health checks for your Classic Load Balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-healthchecks.html) in the *User Guide for Classic Load Balancers*.

By default, Amazon EC2 Auto Scaling does not consider an instance unhealthy and replace it if it fails the Elastic Load Balancing health checks. The default health checks for an Auto Scaling group are EC2 health checks only. For more information, see [Health checks for instances in an Auto Scaling group](ec2-auto-scaling-health-checks.md).

To enable Amazon EC2 Auto Scaling to replace instances that are reported unhealthy by Elastic Load Balancing, you can configure your Auto Scaling group to use Elastic Load Balancing health checks. By doing so, Amazon EC2 Auto Scaling considers the instance unhealthy if it fails either the EC2 health checks or the Elastic Load Balancing health checks. If you attach multiple load balancer target groups or Classic Load Balancers to the group, all of them must report that an instance is healthy in order for it to consider the instance healthy. If any one of them reports an instance as unhealthy, the Auto Scaling group replaces the instance, even if others report it as healthy. 

For information about how to enable these health checks for your Auto Scaling group, see [Attach an Elastic Load Balancing load balancer to your Auto Scaling group](attach-load-balancer-asg.md).

**Note**  
To make sure that these health checks start as soon as possible, make sure your group's health check grace period is not set too high, but high enough for your Elastic Load Balancing health checks to determine whether a target is available to handle requests. For more information, see [Set the health check grace period for an Auto Scaling group](health-check-grace-period.md).

# Attach an Elastic Load Balancing load balancer to your Auto Scaling group
<a name="attach-load-balancer-asg"></a>

This topic describes how to attach an Elastic Load Balancing load balancer to an Auto Scaling group. It also describes how to turn on Elastic Load Balancing health checks to let Amazon EC2 Auto Scaling replace instances that Elastic Load Balancing reports as unhealthy. 

By default, Amazon EC2 Auto Scaling only replaces instances that are unhealthy or unreachable based on Amazon EC2 health checks. If you turn on Elastic Load Balancing health checks, Amazon EC2 Auto Scaling can replace a running instance if any of the Elastic Load Balancing load balancers you attach to the Auto Scaling group report it as unhealthy.

For a tutorial on attaching an Application Load Balancer to your Auto Scaling group, see [Tutorial: Set up a scaled and load-balanced application](tutorial-ec2-auto-scaling-load-balancer.md).

**Important**  
Before you continue, complete all [prerequisites](getting-started-elastic-load-balancing.md) in the previous section.

**Contents**
+ [Attach a target group or Classic Load Balancer](#as-add-load-balancer-console)
+ [Detach a target group or Classic Load Balancer](#as-remove-load-balancer)

## Attach a target group or Classic Load Balancer
<a name="as-add-load-balancer-console"></a>

When you create or update an Auto Scaling group, you can attach one or more target groups or Classic Load Balancers. When you attach an Application Load Balancer, Network Load Balancer, or Gateway Load Balancer, you attach a target group rather than the load balancer itself.

Follow the steps in this section to use the console to:
+ Attach a target group or Classic Load Balancer to an Auto Scaling group
+ Turn on the health checks for Elastic Load Balancing

**To attach an existing load balancer as you are creating a new Auto Scaling group**

1. Open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/), and choose **Auto Scaling Groups** from the navigation pane.

1. On the navigation bar at the top of the screen, choose the AWS Region that you created your load balancer in.

1. Choose **Create Auto Scaling group**.

1.  In steps 1 and 2, choose the options as desired and proceed to **Step 3: Configure advanced options**.

1. For **Load balancing**, choose **Attach to an existing load balancer**.

1. Under **Attach to an existing load balancer**, do one of the following:

   1. For Application Load Balancers, Network Load Balancers, and Gateway Load Balancers:

      Choose **Choose from your load balancer target groups**, and then choose a target group in the **Existing load balancer target groups** field.

   1. For Classic Load Balancers:

      Choose **Choose from Classic Load Balancers**, and then choose your load balancer in the **Classic Load Balancers** field.

1. (Optional) For **Health checks**, **Additional health check types**, select **Turn on Elastic Load Balancing health checks**.

1. (Optional) For **Health check grace period**, enter the amount of time, in seconds. This is how long Amazon EC2 Auto Scaling needs to wait before checking the health status of an instance after it enters the `InService` state. For more information, see [Set the health check grace period for an Auto Scaling group](health-check-grace-period.md). 

1. Proceed to create the Auto Scaling group. Your instances will be automatically registered to the load balancer after the Auto Scaling group has been created. 

**To attach an existing load balancer to your Auto Scaling group after it's created**

1. Open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/), and choose **Auto Scaling Groups** from the navigation pane.

1. Select the check box next to your Auto Scaling group.

   A split pane opens up in the bottom of the **Auto Scaling groups** page. 

1. On the **Integrations** tab, choose **Load balancing**, **Edit**.

1. Under **Load balancing**, do one of the following:

   1. For **Application, Network or Gateway Load Balancer target groups**, select its check box and choose a target group.

   1. For **Classic Load Balancers**, select its check box and choose your load balancer.

1. Choose **Update**.

When you finish attaching the load balancer, you can optionally turn on the health checks that use it.

**To turn on the Elastic Load Balancing health checks**

1. On the **Details** tab, choose **Health checks**, **Edit**.

1. For **Health checks**, **Additional health check types**, select **Turn on Elastic Load Balancing health checks**.

1. For **Health check grace period**, enter the amount of time, in seconds. This is how long Amazon EC2 Auto Scaling needs to wait before checking the health status of an instance after it enters the `InService` state. For more information, see [Set the health check grace period for an Auto Scaling group](health-check-grace-period.md). 

1. Choose **Update**.

**Note**  
You can monitor the status of the load balancer while it is being attached by using the AWS CLI. When Amazon EC2 Auto Scaling has successfully registered the instances and at least one registered instance passes the health checks, you receive a status of `InService`. For more information, see [Verify the attachment status of your load balancer](load-balancer-status.md).

## Detach a target group or Classic Load Balancer
<a name="as-remove-load-balancer"></a>

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/](https://console.aws.amazon.com/ec2/), and choose **Auto Scaling Groups** from the navigation pane.

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

   A split pane opens up in the bottom of the **Auto Scaling groups** page.

1. On the **Details** tab, choose **Load balancing**, **Edit**.

1. Under **Load balancing**, do one of the following:

   1. For **Application, Network or Gateway Load Balancer target groups**, choose the delete (X) icon next to the target group.

   1. For **Classic Load Balancers**, choose the delete (X) icon next to the load balancer. 

1. Choose **Update**.

When you finish detaching the target group, you can turn off the Elastic Load Balancing health checks.

**To turn off the Elastic Load Balancing health checks**

1. On the **Details** tab, choose **Health checks**, **Edit**.

1. For **Health checks**, **Additional health check types**, deselect **Turn on Elastic Load Balancing health checks**.

1. Choose **Update**.

# Configure an Application Load Balancer or Network Load Balancer from the console
<a name="as-create-load-balancer-console"></a>

Use the following procedure to create and attach an Application Load Balancer or a Network Load Balancer as you create your Auto Scaling group. 

**To create and attach a new load balancer as you create a new Auto Scaling group**

1. Open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/), and choose **Auto Scaling Groups** from the navigation pane.

1. Choose **Create Auto Scaling group**.

1. In steps 1 and 2, choose the options as desired and proceed to **Step 3: Configure advanced options**.

1. For **Load balancing**, choose **Attach to a new load balancer**.

   1. Under **Attach to a new load balancer**, for **Load balancer type**, choose whether to create an Application Load Balancer or Network Load Balancer. 

   1. For **Load balancer name**, enter a name for the load balancer, or keep the default name.

   1. For **Load balancer scheme**, choose whether to create a public internet-facing load balancer, or keep the default for an internal load balancer.

   1. For **Availability Zones and subnets**, select the public subnet for each Availability Zone in which you chose to launch your EC2 instances. (These prepopulate from step 2.).

   1. For **Listeners and routing**, update the port number for your listener (if necessary), and under **Default routing**, choose **Create a target group**. Alternatively, you can choose an existing target group from the drop-down list.

   1. If you chose **Create a target group** in the last step, for **New target group name**, enter a name for the target group, or keep the default name. 

   1. To add tags to your load balancer, choose **Add tag**, and provide a tag key and value for each tag.

1. (Optional) For **Health checks**, **Additional health check types**, select **Turn on Elastic Load Balancing health checks**.

1. (Optional) For **Health check grace period**, enter the amount of time, in seconds. This is how long Amazon EC2 Auto Scaling needs to wait before checking the health status of an instance after it enters the `InService` state. For more information, see [Set the health check grace period for an Auto Scaling group](health-check-grace-period.md). 

1. Proceed to create the Auto Scaling group. Your instances will be automatically registered to the load balancer after the Auto Scaling group has been created. 
**Note**  
After creating your Auto Scaling group, you can use the Elastic Load Balancing console to create additional listeners. This is useful if you need to create a listener with a secure protocol, such as HTTPS, or a UDP listener. You can add more listeners to existing load balancers, as long as you use distinct ports.

# Verify the attachment status of your load balancer
<a name="load-balancer-status"></a>

After you attach a load balancer, it enters the `Adding` state while registering the instances in the group. When all instances in the group are registered, it enters the `Added` state. After at least one registered instance passes the health checks, it enters the `InService` state. When the load balancer is in 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 deregister. By default, connection draining (deregistration delay) is enabled for Application Load Balancers, Network Load Balancers, and Gateway Load Balancers. 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 it deregisters the instances. 

You can verify the attachment status by using the AWS Command Line Interface (AWS CLI) or AWS SDKs. You cannot verify the attachment status from the console.

**To use the AWS CLI to verify the attachment status**  
The following [describe-traffic-sources](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/describe-traffic-sources.html) command returns the attachment status of all traffic sources for the specified Auto Scaling group.

```
aws autoscaling describe-traffic-sources --auto-scaling-group-name my-asg
```

The example returns the ARN of the Elastic Load Balancing target group that's attached to the Auto Scaling group, along with the attachment status of the target group in the `State` element.

```
{
    "TrafficSources": [
        {
            "Identifier": "arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-targets/1234567890123456",
            "State": "InService",
            "Type": "elbv2"
        }
    ]
}
```

# Add an Availability Zone
<a name="as-add-az-console"></a>

To take advantage of the safety and reliability of geographic redundancy, span your Auto Scaling group across multiple Availability Zones of the Region you are working in and attach a load balancer to distribute incoming traffic across those Availability Zones. 

When one Availability Zone becomes unhealthy or unavailable, Amazon EC2 Auto Scaling launches new instances in an unaffected Availability Zone. When the unhealthy Availability Zone returns to a healthy state, Amazon EC2 Auto Scaling automatically redistributes the application instances evenly across all the Availability Zones for your Auto Scaling group. Amazon EC2 Auto Scaling does this by attempting to launch new instances in the Availability Zone with the fewest instances. If the attempt fails, however, Amazon EC2 Auto Scaling attempts to launch in other Availability Zones until it succeeds.

Elastic Load Balancing creates a load balancer node for each Availability Zone you enable for the load balancer. If you enable cross-zone load balancing for your load balancer, each load balancer node distributes traffic evenly across the registered instances in all enabled Availability Zones. If cross-zone load balancing is disabled, each load balancer node distributes requests evenly across the registered instances in its Availability Zone only.

You must specify at least one Availability Zone when you are creating your Auto Scaling group. Later, you can expand the availability of your application by adding an Availability Zone to your Auto Scaling group and enabling that Availability Zone for your load balancer (if the load balancer supports it).

**Limitations**  
To update which Availability Zones are enabled for your load balancer, you need to be aware of the following limitations:
+ When you enable an Availability Zone for your load balancer, you specify one subnet from that Availability Zone. Note that you can enable at most one subnet per Availability Zone for your load balancer. 
+ For internet-facing load balancers, the subnets that you specify for the load balancer must have at least eight available IP addresses. 
+ For Application Load Balancers, you must enable at least two Availability Zones.
+ For Network Load Balancers, you cannot disable the enabled Availability Zones, but you can enable additional ones.
+ For Gateway Load Balancers, you cannot disable the enabled Availability Zones, but you can enable additional ones.

Use the following procedure to expand your Auto Scaling group and load balancer to a subnet in an additional Availability Zone.

**To add an Availability Zone**

1. Open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/), and choose **Auto Scaling Groups** from the navigation pane.

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

   A split pane opens up in the bottom of the **Auto Scaling groups** page. 

1. On the **Details** tab, choose **Network**, **Edit**.

1. In **Subnets**, choose the subnet corresponding to the Availability Zone that you want to add to the Auto Scaling group.

1. Choose **Update**.

1. To update the Availability Zones for your load balancer so that it shares the same Availability Zones as your Auto Scaling group, complete the following steps:

   1. On the navigation pane, under **Load Balancing**, choose **Load Balancers**.

   1. Choose your load balancer.

   1. Do one of the following:
      + For Application Load Balancers and Network Load Balancers:

        1. On the **Description** tab, for **Availability Zones**, choose **Edit subnets**. 

        1. On the **Edit subnets** page, for **Availability Zones**, select the check box for the Availability Zone to add. If there is only one subnet for that zone, it is selected. If there is more than one subnet for that zone, select one of the subnets. 
      + For Classic Load Balancers in a VPC:

        1. On the **Instances** tab, choose **Edit Availability Zones**. 

        1. On the **Add and Remove Subnets** page, for **Available subnets**, select the subnet using its add (\$1) icon. The subnet is moved under **Selected subnets**.

   1. Choose **Save**.

## Related resources
<a name="availability-zone-related-resources"></a>

Amazon EC2 Auto Scaling rebalances your group when you change Availability Zones. This means replacing and redistributing some instances. For more information, see [Example: Distribute instances across Availability Zones](auto-scaling-benefits.md#arch-AutoScalingMultiAZ).

If you have registered targets in Availability Zones that are not enabled for the load balancer, the load balancer does not route traffic to them. For more information, see [How Elastic Load Balancing works](https://docs.aws.amazon.com/elasticloadbalancing/latest/userguide/how-elastic-load-balancing-works.html) in the *Elastic Load Balancing User Guide*.

# Remove an Availability Zone
<a name="as-remove-az-console"></a>

To remove an Availability Zone from your Auto Scaling group and load balancer, use the following procedure.

**To remove an Availability Zone**

1. Open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/), and choose **Auto Scaling Groups** from the navigation pane.

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

   A split pane opens up in the bottom of the **Auto Scaling groups** page. 

1. On the **Details** tab, choose **Network**, **Edit**.

1. In **Subnets**, choose the delete (X) icon for the subnet corresponding to the Availability Zone that you want to remove from the Auto Scaling group. If there is more than one subnet for that zone, choose the delete (X) icon for each one. 

1. Choose **Update**.

1. To update the Availability Zones for your load balancer so that it shares the same Availability Zones as your Auto Scaling group, complete the following steps:

   1. On the navigation pane, under **Load Balancing**, choose **Load Balancers**.

   1. Choose your load balancer.

   1. Do one of the following:
      + For Application Load Balancers:

        1. On the **Description** tab, for **Availability Zones**, choose **Edit subnets**. 

        1. On the **Edit subnets** page, for **Availability Zones**, clear the check box to remove the subnet for that Availability Zone.
      + For Classic Load Balancers in a VPC:

        1. On the **Instances** tab, choose **Edit Availability Zones**. 

        1. On the **Add and Remove Subnets** page, for **Available subnets**, remove the subnet using its delete (-) icon. The subnet is moved under **Available subnets**.

   1. Choose **Save**.

# Detach a target group or Classic Load Balancer from your Auto Scaling group
<a name="as-remove-load-balancer"></a>

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/](https://console.aws.amazon.com/ec2/), and choose **Auto Scaling Groups** from the navigation pane.

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

   A split pane opens up in the bottom of the **Auto Scaling groups** page.

1. On the **Details** tab, choose **Load balancing**, **Edit**.

1. Under **Load balancing**, do one of the following:

   1. For **Application, Network or Gateway Load Balancer target groups**, choose the delete (X) icon next to the target group.

   1. For **Classic Load Balancers**, choose the delete (X) icon next to the load balancer. 

1. Choose **Update**.

When you finish detaching the target group, you can turn off the Elastic Load Balancing health checks.

**To turn off the Elastic Load Balancing health checks**

1. On the **Details** tab, choose **Health checks**, **Edit**.

1. For **Health checks**, **Additional health check types**, deselect **Turn on Elastic Load Balancing health checks**.

1. Choose **Update**.

# Examples for working with Elastic Load Balancing using the AWS CLI
<a name="examples-elastic-load-balancing-aws-cli"></a>

Use the AWS Command Line Interface (AWS CLI) to attach, detach, and describe load balancers and target groups, add and remove Elastic Load Balancing health checks, and change which Availability Zones are enabled.

This topic shows examples of AWS CLI commands that perform common tasks for Amazon EC2 Auto Scaling.

**Important**  
For additional command examples, see [https://awscli.amazonaws.com/v2/documentation/api/latest/reference/elbv2/index.html](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/elbv2/index.html) and [https://awscli.amazonaws.com/v2/documentation/api/latest/reference/elb/index.html](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/elb/index.html) in the *AWS CLI Command Reference*.

**Topics**
+ [Attach your target group or Classic Load Balancer](#example-attach-traffic-sources)
+ [Describe your target groups or Classic Load Balancers](#example-describe-traffic-sources)
+ [Add Elastic Load Balancing health checks](#example-add-elb-healthcheck)
+ [Change your Availability Zones](#example-specify-availability-zones)
+ [Detach your target group or Classic Load Balancer](#example-detach-traffic-sources)
+ [Remove Elastic Load Balancing health checks](#example-remove-elb-healthcheck)
+ [Legacy commands](#legacy-commands)

## Attach your target group or Classic Load Balancer
<a name="example-attach-traffic-sources"></a>

Use the following [create-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/create-auto-scaling-group.html) command to create an Auto Scaling group and simultaneously attach a target group by specifying its Amazon Resource Name (ARN). The target group can be associated with an Application Load Balancer, a Network Load Balancer, or a Gateway Load Balancer. 

Replace the sample values for `--auto-scaling-group-name`, `--vpc-zone-identifier`, `--min-size`, and `--max-size`. For the `--launch-template` option, replace `my-launch-template` and `1` with the name and version of a launch template for your Auto Scaling group. For the `--traffic-sources` option, replace the sample ARN with the ARN of a target group for an Application Load Balancer, Network Load Balancer, or Gateway Load Balancer.

```
aws autoscaling create-auto-scaling-group --auto-scaling-group-name my-asg \
  --launch-template LaunchTemplateName=my-launch-template,Version='1' \
  --vpc-zone-identifier "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782" \
  --min-size 1 --max-size 5 \
  --traffic-sources "Identifier=arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-targets/12345678EXAMPLE1"
```

Use the [attach-traffic-sources](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/attach-traffic-sources.html) command to attach additional target groups to the Auto Scaling group after it's created.

The following command adds another target group to the same group.

```
aws autoscaling attach-traffic-sources --auto-scaling-group-name my-asg \
  --traffic-sources "Identifier=arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-targets/12345678EXAMPLE2"
```

Alternatively, to attach a Classic Load Balancer to your group, specify the `--traffic-sources` and `--type` options when you use **create-auto-scaling-group** or **attach-traffic-sources**, as in the following example. Replace `my-classic-load-balancer` with the name of a Classic Load Balancer. For the `--type` option, specify a value of `elb`.

```
--traffic-sources "Identifier=my-classic-load-balancer" --type elb
```

## Describe your target groups or Classic Load Balancers
<a name="example-describe-traffic-sources"></a>

To describe the load balancers or target groups attached to your Auto Scaling group, use the following [describe-traffic-sources](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/describe-traffic-sources.html) command. Replace `my-asg` with the name of your group. 

```
aws autoscaling describe-traffic-sources --auto-scaling-group-name my-asg
```

The example returns the ARN of the Elastic Load Balancing target groups that you attached to the Auto Scaling group.

```
{
    "TrafficSources": [
        {
            "Identifier": "arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-targets/12345678EXAMPLE1",
            "State": "InService",
            "Type": "elbv2"
        },
        {
            "Identifier": "arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-targets/12345678EXAMPLE2",
            "State": "InService",
            "Type": "elbv2"
        }
    ]
}
```

For an explanation of the `State` field in the output, see [Verify the attachment status of your load balancer](load-balancer-status.md).

## Add Elastic Load Balancing health checks
<a name="example-add-elb-healthcheck"></a>

To add Elastic Load Balancing health checks to the health checks that your Auto Scaling group performs on instances, use the following [update-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/update-auto-scaling-group.html) command and specify `ELB` as the value for the `--health-check-type` option. Replace `my-asg` with the name of your group.

```
aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg \
  --health-check-type "ELB"
```

New instances often need time for a brief warmup before they can pass a health check. If the grace period doesn't provide enough warmup time, the instances might not appear ready to serve traffic. Amazon EC2 Auto Scaling might consider those instances unhealthy and replace them.

To update the health check grace period, use the `--health-check-grace-period` option when you use **update-auto-scaling-group**, as in the following example. Replace *300* with the number of seconds to keep new instances in service before terminating them if they're found to be unhealthy.

```
--health-check-grace-period 300
```

For more information, see [Health checks for instances in an Auto Scaling group](ec2-auto-scaling-health-checks.md).

## Change your Availability Zones
<a name="example-specify-availability-zones"></a>

Changing your Availability Zones has some limitations that you should be aware of. For more information, see [Add an Availability ZoneRemove an Availability Zone](as-add-az-console.md).

**To change the Availability Zones for an Application Load Balancer or Network Load Balancer**

1. Before you change the Availability Zones of the load balancer, it's a good idea to first update the Availability Zones of the Auto Scaling group to verify that there is availability for your instance types in the specified zones. 

   To update the Availability Zones for your Auto Scaling group, use the following [update-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/update-auto-scaling-group.html) command. Replace the sample subnet IDs with the IDs of the subnets in the Availability Zones to enable. The specified subnets replace the previously enabled subnets. Replace `my-asg` with the name of your group. 

   ```
   aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg \
     --vpc-zone-identifier "subnet-41767929,subnet-cb663da2,subnet-8360a9e7"
   ```

1. Use the following [describe-auto-scaling-groups](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/describe-auto-scaling-groups.html) command to verify that the instances in the new subnets have launched. If the instances have launched, you see a list of the instances and their statuses. Replace `my-asg` with the name of your group. 

   ```
   aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name my-asg
   ```

1. Use the following [set-subnets](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/elbv2/set-subnets.html) command to specify the subnets for your load balancer. Replace the sample subnet IDs with the IDs of the subnets in the Availability Zones to enable. You can specify only one subnet per Availability Zone. The specified subnets replace the previously enabled subnets. Replace `my-lb-arn` with the ARN of your load balancer. 

   ```
   aws elbv2 set-subnets --load-balancer-arn my-lb-arn \
     --subnets subnet-41767929 subnet-cb663da2 subnet-8360a9e7
   ```

**To change the Availability Zones for a Classic Load Balancer**

1. Before you change the Availability Zones of the load balancer, it's a good idea to first update the Availability Zones of the Auto Scaling group to verify that there is availability for your instance types in the specified zones. 

   To update the Availability Zones for your Auto Scaling group, use the following [update-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/update-auto-scaling-group.html) command. Replace the sample subnet IDs with the IDs of the subnets in the Availability Zones to enable. The specified subnets replace the previously enabled subnets. Replace `my-asg` with the name of your group.

   ```
   aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg \
     --vpc-zone-identifier "subnet-41767929,subnet-cb663da2"
   ```

1. Use the following [describe-auto-scaling-groups](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/describe-auto-scaling-groups.html) command to verify that the instances in the new subnets have launched. If the instances have launched, you see a list of the instances and their statuses. Replace `my-asg` with the name of your group.

   ```
   aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name my-asg
   ```

1. Use the following [attach-load-balancer-to-subnets](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/elb/attach-load-balancer-to-subnets.html) command to enable a new Availability Zone for your Classic Load Balancer. Replace the sample subnet ID with the ID of the subnet for the Availability Zone to enable. Replace `my-lb` with the name of your load balancer. 

   ```
   aws elb attach-load-balancer-to-subnets --load-balancer-name my-lb \
     --subnets subnet-cb663da2
   ```

   To disable an Availability Zone, use the following [detach-load-balancer-from-subnets](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/elb/detach-load-balancer-from-subnets.html) command. Replace the sample subnet ID with the ID of the subnet for the Availability Zone to disable. Replace `my-lb` with the name of your load balancer. 

   ```
   aws elb detach-load-balancer-from-subnets --load-balancer-name my-lb \
     --subnets subnet-8360a9e7
   ```

## Detach your target group or Classic Load Balancer
<a name="example-detach-traffic-sources"></a>

The following [detach-traffic-sources](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/detach-traffic-sources.html) command detaches a target group from your Auto Scaling group when you no longer need it. 

For the `--auto-scaling-group-name` option, replace `my-asg` with the name of your group. For the `--traffic-sources` option, replace the sample ARN with the ARN of a target group for an Application Load Balancer, Network Load Balancer, or Gateway Load Balancer.

```
aws autoscaling detach-traffic-sources --auto-scaling-group-name my-asg \
  --traffic-sources "Identifier=arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-targets/1234567890123456"
```

To detach a Classic Load Balancer from your group, specify the `--traffic-sources` and `--type` options, as in the following example. Replace `my-classic-load-balancer` with the name of a Classic Load Balancer. For the `--type` option, specify a value of `elb`.

```
--traffic-sources "Identifier=my-classic-load-balancer" --type elb
```

## Remove Elastic Load Balancing health checks
<a name="example-remove-elb-healthcheck"></a>

To remove Elastic Load Balancing health checks from your Auto Scaling group, use the following [update-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/update-auto-scaling-group.html) command and specify `EC2` as the value for the `--health-check-type` option. Replace `my-asg` with the name of your group. 

```
aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg \
  --health-check-type "EC2"
```

For more information, see [Health checks for instances in an Auto Scaling group](ec2-auto-scaling-health-checks.md).

## Legacy commands
<a name="legacy-commands"></a>

The following examples show how you can use legacy CLI commands to attach, detach, and describe load balancers and target groups. They remain in this document as a reference for any customers who want to use them. We continue to support the legacy CLI commands, but we recommend that you use the new "traffic sources" CLI commands, which can attach and detach multiple traffic sources types. You can use both the legacy CLI commands and the "traffic sources" CLI commands on the same Auto Scaling group.

### Attach your target group or Classic Load Balancer (legacy)
<a name="example-attach-load-balancer-target-group"></a>

**To attach your target group**  
The following [create-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/create-auto-scaling-group.html) command creates an Auto Scaling group with an attached target group. Specify the Amazon Resource Name (ARN) of a target group for an Application Load Balancer, Network Load Balancer, or Gateway Load Balancer.

```
aws autoscaling create-auto-scaling-group --auto-scaling-group-name my-asg \
  --launch-template LaunchTemplateName=my-launch-template,Version='1' \
  --vpc-zone-identifier "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782" \
  --target-group-arns "arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-targets/1234567890123456" \
  --min-size 1 --max-size 5
```

The following [attach-load-balancer-target-groups](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/attach-load-balancer-target-groups.html) command attaches a target group to an existing Auto Scaling group.

```
aws autoscaling attach-load-balancer-target-groups --auto-scaling-group-name my-asg \
  --target-group-arns "arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-targets/1234567890123456"
```

**To attach your Classic Load Balancer**  
The following [create-auto-scaling-group](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/create-auto-scaling-group.html) command creates an Auto Scaling group with an attached Classic Load Balancer.

```
aws autoscaling create-auto-scaling-group --auto-scaling-group-name my-asg \
  --launch-configuration-name my-launch-config \
  --vpc-zone-identifier "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782" \
  --load-balancer-names "my-load-balancer" \
  --min-size 1 --max-size 5
```

The following [attach-load-balancers](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/attach-load-balancers.html) command attaches the specified Classic Load Balancer to an existing Auto Scaling group.

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

### Describe your target group or Classic Load Balancer (legacy)
<a name="example-describe-load-balancer-target-groups"></a>

**To describe target groups**  
To describe the target groups associated with an Auto Scaling group, use the [describe-load-balancer-target-groups](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/describe-load-balancer-target-groups.html) command. The following example lists the target groups for *my-asg*. 

```
aws autoscaling describe-load-balancer-target-groups --auto-scaling-group-name my-asg
```

**To describe Classic Load Balancers**  
To describe the Classic Load Balancers associated with an Auto Scaling group, use the [describe-load-balancers](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/describe-load-balancers.html) command. The following example lists the Classic Load Balancers for *my-asg*. 

```
aws autoscaling describe-load-balancers --auto-scaling-group-name my-asg
```

### Detach your target group or Classic Load Balancer (legacy)
<a name="example-detach-load-balancer-target-group"></a>

**To detach a target group**  
The following [detach-load-balancer-target-groups](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/detach-load-balancer-target-groups.html) command detaches a target group from your Auto Scaling group when you no longer need it. 

```
aws autoscaling detach-load-balancer-target-groups --auto-scaling-group-name my-asg \
  --target-group-arns "arn:aws:elasticloadbalancing:region:account-id:targetgroup/my-targets/1234567890123456"
```

**To detach a Classic Load Balancer**  
The following [detach-load-balancers](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/autoscaling/detach-load-balancers.html) command detaches a Classic Load Balancer from your Auto Scaling group when you no longer need it.

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