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.
Contents
Attach 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-size5
--min-size1
--desired-capacity2
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
"
Describe 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 the attachment status of your load
balancer section in
a previous topic.
Detach 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
"
Attach 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-namemy-launch-config
\ --vpc-zone-identifier "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782
" \ --load-balancer-names "my-load-balancer
" \ --max-size5
--min-size1
--desired-capacity2
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-namesmy-lb
Describe 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 the attachment status of your load
balancer.
Detach 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-namesmy-lb
Add 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-period300
Update 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
-
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-identifiersubnet-41767929
subnet-cb663da2
subnet-8360a9e7
-
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
-
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
\ --subnetssubnet-41767929
subnet-cb663da2
subnet-8360a9e7
For an Auto Scaling group with a Classic Load Balancer in a VPC
-
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-identifiersubnet-41767929
subnet-cb663da2
-
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
-
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
\ --subnetssubnet-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
\ --subnetssubnet-8360a9e7
For an Auto Scaling group with a Classic Load Balancer in EC2-Classic
-
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-zonesus-west-2a
us-west-2b
-
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
-
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-zonesus-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-zonesus-west-2c