J Balrog J Balrog - 3 months ago 44
Python Question

Trouble sending boto3 filtered data to a list

I've generated a working boto3 instance filter using anaconda python 2.7. I need to assign the IP addresses and instance names to a list for further processing.

Code looks like this:

for instance in instances:
print(instance.id, instance.instance_type, instance.key_name, instance.private_ip_address, instance.public_dns_name ,instance.tags[0].get("Value"))

tagged = ec2.instances.filter(Filters=[{'Name': 'tag:My', 'Values': ['tag']}])

for instance in tagged:
print(name, instance.id, instance.private_ip_address)

object_list={}
object_list['requests']=[]

for instance in tagged:
tagged.collections.OrderedDict()
tagged['ip-address'] = instance.private_ip_address
tagged['machine'] = name
tagged['roles'] = ['tagged',instance.id,name]
object_list['requests'].append(tagged)


This is the error:

AttributeError: 'ec2.instancesCollection' object has no attribute 'collections'


Thoughts?

Answer

this doesn't exist but you have a cryptic error because you are reusing your tagged variable which is an instance of ec2.instancesCollection

 tagged.collections.OrderedDict()

(and would do nothing anyway since it's not affected)

you (probably) want/wanted:

tagged = collections.OrderedDict()

but that doesn't make sense since you're already iterating on tagged.

You have to declare another variable

for instance in tagged:
    t = collections.OrderedDict()
    t['ip-address'] = instance.private_ip_address
    t['machine'] = name
    t['roles'] = ['tagged',instance.id,name]
    object_list['requests'].append(t)