Search code examples
pythonamazon-ec2botovolumesmounted-volumes

Python Boto List Storage Devices Attached to Instance


I'm using Python 2.7 and Boto.latest.

I'm creating a dynamic inventory script and I would like to list the storage devices that are attached to each instance

Example:

ID: i-3rblah
Storage:
-  /dev/sda 
- /dev/sdb
- /dev/sdc

Tried:

for reservation in reservations:
  for i in reservation.instances:
    volumes = conn.get_all_volumes(filters={'attachment.instance-id': i.id})
    print i.__dict__
    print volumes.__dict__ 

So I have some volume info, but not what the VolumeID is mapped to.


Solution

  • I think you want the BlockDeviceMapping for the instance. Based on your example above the following should find the block_device_mapping for the instance which is a dictionary. Each key in the dictionary is a device name and the value is a BlockDeviceType object which contain information about the block device associated with that device name.

    for reservation in reservations:
        for instance in reservation.instances:
            bdm = instance.block_device_mapping
            for device in bdm:
                print('Device: {}'.format(device)
                bdt = bdm[device]
                print('\tVolumeID: {}'.format(bdt.volume_id))
                print('\tVolume Status: {}'.format(bd.volume_status))
    

    This should print something like:

    Device: /dev/sda1
        VolumeID: vol-1d011806
        Volume Size: attached
    

    There are other fields in the BlockDeviceType object. You should be able to find more info about that in the Boto docs.