Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List instances in auto scaling group with boto

I want to list all instances that are currently running within a auto scaling group. Can that be accomplished with boto?

There must be some relation between the ASG and the instances as boto has the shutdown_instances method within the boto.ec2.autoscale.group.AutoScalingGroup class.

Any pointers in the right direction is highly appreciated!

like image 295
Sebastian Dahlgren Avatar asked Sep 25 '12 20:09

Sebastian Dahlgren


People also ask

How do I know if an instance is part of Auto Scaling group?

You can query the instance meta-data and compare the instance-id you have received from the previous command to check if the instance is part of autoscaling group.

How do I monitor Auto Scaling in a group?

Open the Amazon EC2 console at console.aws.amazon.com/ec2/. From the navigation pane, select Auto Scaling Groups > (select your group). From the Monitoring tab, select Auto Scaling Metrics > Enable Group Metrics Collection or Display > Auto Scaling.

How many EC2 instances can you have in an Auto Scaling group?

If you specify scaling policies, then Amazon EC2 Auto Scaling can launch or terminate instances as demand on your application increases or decreases. For example, the following Auto Scaling group has a minimum size of one instance, a desired capacity of two instances, and a maximum size of four instances.

Which types of instances would an Auto Scaling group use?

An Auto Scaling group can launch On-Demand Instances, Spot Instances, or both.


1 Answers

Something like this should work:

>>> import boto
>>> autoscale = boto.connect_autoscale()
>>> ec2 = boto.connect_ec2()
>>> group = autoscale.get_all_groups(['mygroupname'])[0]
>>> instance_ids = [i.instance_id for i in group.instances]
>>> reservations = ec2.get_all_instances(instance_ids)
>>> instances = [i for r in reservations for i in r.instances]

The reason we have to collect the Instance ID's and then call EC2 is that AutoScale only stores a small subset of information about the instances. This would result in the variable instances pointing to a list of Instance objects for each instance in the autoscaling group "mygroupname".

like image 172
garnaat Avatar answered Sep 19 '22 11:09

garnaat