Cmag
Cmag

Reputation: 15750

boto aws pulling down list of instances

Folks, I am trying to retrieve not only instance ids of my running machines, but also the aliased names which I've added to them in the aws console.

Is this the proper way to do this? I am not getting back anything interesting....

import boto
botoEC2 = boto.connect_ec2('asdf','asdfasdfasdfasdf')
rsv = botoEC2.get_all_instances()
tags = botoEC2.get_all_tags()
print tags
dir (tags)
print tags
print tags.status
print tags.pop
print tags.count
print tags.tagSet
print tags.requestId
print tags.index
print tags.
print tags.requestId
print tags.index
print tags.key_marker

print tags

output: [Tag:ec2tag, Tag:Name, Tag:Name, Tag:Name, Tag:Name, Tag:Name, Tag:Name, Tag:Name, Tag:Name, Tag:Name, Tag:Name, Tag:Name, Tag:ec2tag, Tag:Name, Tag:Name, Tag:Name, Tag:Name, Tag:Name]

Thanks!

Upvotes: 3

Views: 10427

Answers (1)

aychedee
aychedee

Reputation: 25569

You can fetch all the tags

import boto
conn = boto.connect_ec2('asdf','asdfasdfasdfasdf')

tags = conn.get_all_tags()
for tag in tags:
    print tag.name, tag.value

Or you can get the tags associated with just an instance

reservation = conn.get_all_instances()[0]
# Yeah I don't know why they have these stupid reservation objects either...
instance = reservation.instances[0]
print instance.tags
# prints a dictionary of the tags {'Name': 'Given name'}

UPDATE Apr 2014: Get all instances is going to change it's behaviour in the near future. Funnily enough it is going to start returning a list of EC2 instances. You should use get_all_reservations now to avoid code breakage during the next major version update.

Upvotes: 6

Related Questions