J Balrog J Balrog -4 years ago 216
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)


for instance in tagged:
tagged['ip-address'] = instance.private_ip_address
tagged['machine'] = name
tagged['roles'] = ['tagged',instance.id,name]

This is the error:

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


Answer Source

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


(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]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download