Examples for working with Elastic Load Balancing using the Amazon CLI
Use the Amazon Command Line Interface (Amazon 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 Amazon CLI commands that perform common tasks for Amazon EC2 Auto Scaling.
Important
Contents
Attach your target group or Classic Load Balancer
Use the following create-auto-scaling-group
Replace the sample values for --auto-scaling-group-name,
--vpc-zone-identifier, --min-size, and
--max-size. For the --launch-template option, replace
and
my-launch-template with the name and version of a
launch template for your Auto Scaling group. For the 1--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-namemy-asg\ --launch-template LaunchTemplateName=my-launch-template,Version='1' \ --vpc-zone-identifier "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782" \ --min-size1--max-size5\ --traffic-sources "Identifier=arn:aws-cn:elasticloadbalancing:region:account-id:targetgroup/my-targets/12345678EXAMPLE1"
Use the attach-traffic-sources
The following command adds another target group to the same group.
aws autoscaling attach-traffic-sources --auto-scaling-group-namemy-asg\ --traffic-sources "Identifier=arn:aws-cn: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
with the
name of a Classic Load Balancer. For the my-classic-load-balancer--type option, specify a value of
.elb
--traffic-sources "Identifier=my-classic-load-balancer" --type elb
Describe your target groups or Classic Load Balancers
To describe the load balancers or target groups attached to your Auto Scaling group, use
the following describe-traffic-sources with the name of your group. my-asg
aws autoscaling describe-traffic-sources --auto-scaling-group-namemy-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-cn:elasticloadbalancing:region:account-id:targetgroup/my-targets/12345678EXAMPLE1",
"State": "InService",
"Type": "elbv2"
},
{
"Identifier": "arn:aws-cn: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.
Add Elastic Load Balancing health checks
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 as the value for the
ELB--health-check-type option. Replace
with the name of your
group.my-asg
aws autoscaling update-auto-scaling-group --auto-scaling-group-namemy-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-period300
For more information, see Health checks for instances in an Auto Scaling group.
Change your Availability Zones
Changing your Availability Zones has some limitations that you should be aware of. For more information, see Add an Availability Zone.
To change the Availability Zones for an Application Load Balancer or Network Load Balancer
-
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
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 with the name of your group.my-asgaws autoscaling update-auto-scaling-group --auto-scaling-group-namemy-asg\ --vpc-zone-identifier "subnet-41767929,subnet-cb663da2,subnet-8360a9e7" -
Use the following describe-auto-scaling-groups
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 with the name of your group.my-asgaws autoscaling describe-auto-scaling-groups --auto-scaling-group-namemy-asg -
Use the following set-subnets
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 with the ARN of your load balancer.my-lb-arnaws elbv2 set-subnets --load-balancer-arnmy-lb-arn\ --subnetssubnet-41767929subnet-cb663da2subnet-8360a9e7
To change the Availability Zones for a Classic Load Balancer
-
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
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 with the name of your group.my-asgaws autoscaling update-auto-scaling-group --auto-scaling-group-namemy-asg\ --vpc-zone-identifier "subnet-41767929,subnet-cb663da2" -
Use the following describe-auto-scaling-groups
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 with the name of your group.my-asgaws autoscaling describe-auto-scaling-groups --auto-scaling-group-namemy-asg -
Use the following attach-load-balancer-to-subnets
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 with the name of your load balancer.my-lbaws elb attach-load-balancer-to-subnets --load-balancer-namemy-lb\ --subnetssubnet-cb663da2To disable an Availability Zone, use the following detach-load-balancer-from-subnets
command. Replace the sample subnet ID with the ID of the subnet for the Availability Zone to disable. Replace with the name of your load balancer.my-lbaws elb detach-load-balancer-from-subnets --load-balancer-namemy-lb\ --subnetssubnet-8360a9e7
Detach your target group or Classic Load Balancer
The following detach-traffic-sources
For the --auto-scaling-group-name option, replace
with the name of your group.
For the my-asg--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-namemy-asg\ --traffic-sources "Identifier=arn:aws-cn: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
with the
name of a Classic Load Balancer. For the my-classic-load-balancer--type option, specify a value of
.elb
--traffic-sources "Identifier=my-classic-load-balancer" --type elb
Remove Elastic Load Balancing health checks
To remove Elastic Load Balancing health checks from your Auto Scaling group, use the following update-auto-scaling-group as the value for the
EC2--health-check-type option. Replace
with the name of your group. my-asg
aws autoscaling update-auto-scaling-group --auto-scaling-group-namemy-asg\ --health-check-type "EC2"
For more information, see Health checks for instances in an Auto Scaling group.
Legacy commands
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.
To attach your target group
The following create-auto-scaling-group
aws autoscaling create-auto-scaling-group --auto-scaling-group-namemy-asg\ --launch-template LaunchTemplateName=my-launch-template,Version='1' \ --vpc-zone-identifier "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782" \ --target-group-arns "arn:aws-cn:elasticloadbalancing:region:account-id:targetgroup/my-targets/1234567890123456" \ --min-size1--max-size5
The following attach-load-balancer-target-groups
aws autoscaling attach-load-balancer-target-groups --auto-scaling-group-namemy-asg\ --target-group-arns "arn:aws-cn:elasticloadbalancing:region:account-id:targetgroup/my-targets/1234567890123456"
To attach your Classic Load Balancer
The following create-auto-scaling-group
aws autoscaling create-auto-scaling-group --auto-scaling-group-namemy-asg\ --launch-configuration-namemy-launch-config\ --vpc-zone-identifier "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782" \ --load-balancer-names "my-load-balancer" \ --min-size1--max-size5
The following attach-load-balancers
aws autoscaling attach-load-balancers --auto-scaling-group-namemy-asg\ --load-balancer-namesmy-lb
To describe target groups
To describe the target groups associated with an Auto Scaling group, use the
describe-load-balancer-target-groupsmy-asg.
aws autoscaling describe-load-balancer-target-groups --auto-scaling-group-namemy-asg
To describe Classic Load Balancers
To describe the Classic Load Balancers associated with an Auto Scaling group, use the describe-load-balancersmy-asg.
aws autoscaling describe-load-balancers --auto-scaling-group-namemy-asg
To detach a target group
The following detach-load-balancer-target-groups
aws autoscaling detach-load-balancer-target-groups --auto-scaling-group-namemy-asg\ --target-group-arns "arn:aws-cn:elasticloadbalancing:region:account-id:targetgroup/my-targets/1234567890123456"
To detach a Classic Load Balancer
The following detach-load-balancers
aws autoscaling detach-load-balancers --auto-scaling-group-namemy-asg\ --load-balancer-namesmy-lb