List instances in auto scale group using boto

I want to list all the instances that are currently running in the auto-scaling group. Could this be done with boto?

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

Any pointers in the right direction are highly appreciated!

+7
source share
1 answer

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 need to collect the instance ID and then call EC2 is because AutoScale only stores a small subset of the instance information. This will cause the variable instances to point to a list of instance objects for each instance in the autosave group "mygroupname".

+10
source

All Articles