Examples for working with Elastic Load Balancing with the AWS Command Line Interface (AWS CLI) - Amazon EC2 Auto Scaling

Examples for working with Elastic Load Balancing with the AWS Command Line Interface (AWS CLI)

Use the AWS CLI to attach and detach load balancers, add Elastic Load Balancing health checks, and update Availability Zones.

Attaching a load balancer target group

The following create-auto-scaling-group 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:123456789012:targetgroup/my-targets/1234567890123456" \ --max-size 5 --min-size 1 --desired-capacity 2

The following attach-load-balancer-target-groups 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:123456789012:targetgroup/my-targets/1234567890123456"

Describing load balancer target groups

To view the target groups associated with an Auto Scaling group, use the describe-load-balancer-target-groups command. The following example lists the target groups for my-asg.

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

For an explanation of the State field in the output, see the Understand load balancer status section in a previous topic.

Detaching a load balancer target group

The following detach-load-balancer-target-groups 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:123456789012:targetgroup/my-targets/1234567890123456"

Attaching a Classic Load Balancer

The following create-auto-scaling-group 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" \ --max-size 5 --min-size 1 --desired-capacity 2

The following attach-load-balancers 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

Describing Classic Load Balancers

To view the Classic Load Balancers associated with an Auto Scaling group, use the describe-load-balancers command. The following example lists the Classic Load Balancers for my-asg.

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

For an explanation of the State field in the output, see Understand load balancer status.

Detaching a Classic Load Balancer

The following detach-load-balancers 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

Adding Elastic Load Balancing health checks

To add Elastic Load Balancing health checks to an Auto Scaling group, run the following update-auto-scaling-group command and specify ELB as the value for the --health-check-type option.

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

To update the health check grace period, use the --health-check-grace-period option. New instances often need time for a brief warm-up before they can pass a health check. If the grace period doesn't provide enough warm-up time, the instances might not appear ready to serve traffic. Amazon EC2 Auto Scaling might consider those instances unhealthy and replace them. For more information, see Health check grace period.

The following update-auto-scaling-group command adds Elastic Load Balancing health checks and specifies a grace period of 300 seconds.

aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-lb-asg \ --health-check-type ELB --health-check-grace-period 300

Updating Availability Zones

The commands that you use depend on whether your load balancer is an Application Load Balancer or Network Load Balancer, a Classic Load Balancer in a VPC, or a Classic Load Balancer in EC2-Classic. You can update the subnets and Availability Zones for your load balancer only if your load balancer supports it. For more information, see Limitations.

For an Auto Scaling group with an Application Load Balancer or Network Load Balancer

  1. Specify the subnets that are used for the Auto Scaling group using the following update-auto-scaling-group command.

    aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg \ --vpc-zone-identifier subnet-41767929 subnet-cb663da2 subnet-8360a9e7
  2. Verify that the instances in the new subnets are ready to accept traffic from the load balancer using the following describe-auto-scaling-groups command.

    aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name my-asg
  3. Specify the subnets that are used for your Application Load Balancer or Network Load Balancer using the following set-subnets command.

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

For an Auto Scaling group with a Classic Load Balancer in a VPC

  1. Specify the subnets that are used for the Auto Scaling group using the following update-auto-scaling-group command.

    aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg \ --vpc-zone-identifier subnet-41767929 subnet-cb663da2
  2. Verify that the instances in the new subnets are ready to accept traffic from the load balancer using the following describe-auto-scaling-groups command.

    aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name my-asg
  3. Enable the new subnet for your Classic Load Balancer using the following attach-load-balancer-to-subnets command.

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

    To disable a subnet, run the following detach-load-balancer-from-subnets command.

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

For an Auto Scaling group with a Classic Load Balancer in EC2-Classic

  1. Specify the Availability Zones that are used for the Auto Scaling group using the following update-auto-scaling-group command.

    aws autoscaling update-auto-scaling-group --auto-scaling-group-name my-asg \ --availability-zones us-west-2a us-west-2b
  2. Verify that the instances in the new Availability Zones are ready to accept traffic from the load balancer using the following describe-auto-scaling-groups command.

    aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name my-asg
  3. Enable the new Availability Zone for your Classic Load Balancer using the following enable-availability-zones-for-load-balancer command.

    aws elb enable-availability-zones-for-load-balancer --load-balancer-name my-lb \ --availability-zones us-west-2b

    To disable an Availability Zone, run the following disable-availability-zones-for-load-balancer command.

    aws elb disable-availability-zones-for-load-balancer --load-balancer-name my-lb \ --availability-zones us-west-2c