Reputation: 27
I'm trying to get all instances that are not attached to an autoscaling group. All instances from an autoscaling group have a tag aws:autoscaling:groupName. I can get those instances with this:
reservations = ec.describe_instances(
Filters=[
# Instances in autoscaling group
{'Name': 'tag-key', 'Values': ['aws:autoscaling:groupName']},
# Instances should be in running mode
{'Name': 'instance-state-name', 'Values': ['running']}
]
).get(
'Reservations', []
)
instances = sum(
[
[i for i in r['Instances']]
for r in reservations
], [])
print "Number of the instances in autoscaling: %d" % len(instances)
But what I really need to get are instances that are not in autoscaling. As there is an inverse search in AWS Console, I'm hoping there is in Python as well.
Upvotes: 1
Views: 422
Reputation: 108
Following code can do it for you. There is no direct inverse search option in boto till date. Hope someday, someone will add that option :)
https://github.com/boto/boto3/issues/173
reservations = ec.describe_instances(
Filters=[
# Instances should be in running mode
{'Name': 'instance-state-name', 'Values': ['running']}
]
)
Count=0
for Instances in reservations['Reservations']:
for Instance in Instances['Instances']:
if "aws:autoscaling:groupName" not in str(Instance):
# your logic on servers without AutoScaling Group goes here
Count=Count+1
print "Number of the instances Without autoscaling: ", Count
Upvotes: 1