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!
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.
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.
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.
An Auto Scaling group can launch On-Demand Instances, Spot Instances, or both.
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With