List instances in auto scaling group with boto

Sebastian Dahlgren picture Sebastian Dahlgren · Sep 25, 2012 · Viewed 9k times · Source

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!

Answer

garnaat picture garnaat · Sep 26, 2012

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".