0

I am query AWS using boto ec2 in python. Firstly I find all reserved instances by get_all_reserved_instances then I am also able to find total count of each instance_type by instance_count. I am trying to calculate total number of reserved instances under tags.

Eg. We have two tags group and name. Then I want to show total number of reserved instances of particular type (Eg. i2.xlarge) under group tag. How to do this, I did not find this in AWS console also ?

Vikas Saini
  • 163
  • 3

1 Answers1

0

Each AWS instance item boto returns has tags and instance_type, that you can use for querying.

With boto, you can do something like below - I am using get_all_instances as I don't have any reserved instances under my account - but I believe the outcome should be similar in your as well if you replace it with get_all_reserved_instances -

import boto
conn=boto.connect_ec2()
R=conn.get_all_instances()
i=[i for r in R for i in r.instances]
for item in i:
  if item.instance_type=='i2.xlarge' and item.tags.get('Name')=='demo':
    ## Your logic here
Daniel t.
  • 9,291
  • 1
  • 33
  • 36
  • If we iterate result return by get_all_instances(), its like, for instance_set in R: for instance in instance_set.instances: ##logic and we can print its name,tag etc. But we iterate result return by get_all_reserved_instances() like this, for instance in R: ##logic and it does not has any attribute like .tags or .instances. when i print instance in get_all_reserved_instances() , it print result like, ReservedInstance:ah9l09kj-6s1a-4122-9lp7-237jks8901ka ReservedInstance:jhaks0923j-8c0a-1275-0l29-8dhdkl98df34 How to identify that which reserve instance belong to which tag ? – Vikas Saini Jan 05 '16 at 05:14