Cmag
Cmag

Reputation: 15750

map AWS instances/volumes to snapshots with boto

I'm writing a python script to check all my running instances for ebs snapshots...

rsv = self.botoEC2.get_all_instances()
for r in rsv:
  ins = r.instances[0]
  blockDevice = self.getInstanceAttribute(ins,"blockDeviceMapping")
  print blockDevice

def getInstanceAttribute(self,instance,attribute):
    return instance.get_attribute(attribute)

output:

{u'blockDeviceMapping': {u'/dev/sda1': <boto.ec2.blockdevicemapping.BlockDeviceType object at 0x10d5faed0>}}

I am new to boto. Am I on the right path?

Do I need to get the blockDeviceMapping first, then call the volume functions?

How do I get the actual volume id, so i can check it for existing snapshots?

Upvotes: 0

Views: 1629

Answers (1)

garnaat
garnaat

Reputation: 45856

It sounds like you want to loop through all of your running instances and then loop through all of the EBS volumes attached to those instances. Is that right? If so, I would do something like this:

import boto

ec2 = boto.connect_ec2()
reservations = ec2.get_all_instances(filters={'instance-state-name': 'running'})
volumes = []
for r in reservations:
    for i in r.instances:
        volumes.extend(ec2.get_all_volumes(filters={'attachment.instance-id': i.id}))

At that point, volumes would contain a list of all Volume objects that are attached to running EC2 instances.

Upvotes: 3

Related Questions