Search code examples
pythonamazon-web-servicesamazon-ec2botoboto3

AWS EBS Volume - Python - Find all fields info as shown in AWS EC2 EBS Volume Console


I'm trying to create a Python script to generate a csv format file against all the available AWS EBS volumes and showing all the field values as I see them in AWS EC2 EBS Volume Console.

[arun@Andrews-MBP-2 ~/aks/always-latest-ws-sunny/anisble] $ cat ~/aws-vol-info.py 
import boto3

#define the connection
ec2 = boto3.resource('ec2', region_name="us-west-2")

volumes = ec2.volumes.all()

for vol in volumes:
    print "Created(" + str(vol.create_time) + "),VolumeState(" + str(vol.state) + "),VolumeID(" + str(vol.id) + "),VolumeSize(" + str(vol.size) + ") " + vol.type
[arun@Andrews-MBP-2 ~/aks/always-latest-ws-sunny/anisble] $ 

This script is giving me the following error message. Reason: The above script works if I don't use vol.type field. It didn't work as volumes variable never got that into it's value when it ran ec2.volumes.all().

[arun@Andrews-MBP-2 ~/aks/always-latest-ws-sunny/anisble] $ python ~/aws-vol-info.py 
Traceback (most recent call last):
  File "/Users/arun/aws-vol-info.py", line 9, in <module>
    print "Created(" + str(vol.create_time) + "),VolumeState(" + str(vol.state) + "),VolumeID(" + str(vol.id) + "),VolumeSize(" + str(vol.size) + ") " + vol.type
AttributeError: 'ec2.Volume' object has no attribute 'type'
[arun@Andrews-MBP-2 ~/aks/always-latest-ws-sunny/anisble] $ 

What library/function should I use / change in the above script using which I can show all the fields or some more meaningful fields of an EBS volume (what I see in AWS EC2 EBS Volume console), see picture below for the available fields in AWS Console.

enter image description here

I found this other script (#2) online on Github, which seems like it can print some more fields, but it's giving another error listed below. I successfully ran python -m pip install --user aws or python -m pip install aws or pip install aws, or ran the script (which included lines only after #Import classes from aws package line, inside the aws folder per his repository (after cloning it) but still getting the error.

import boto.ec2
class Volumes:
    def __init__(self):
        ''' Volumes Constructor '''

    def list_volumes(conn):
        ''' List Volumes '''
        # get all volumes
        vols = conn.get_all_volumes()

        # if volumes found
        if vols:
            #loop through volumes
            for v in vols:
                print 'Volume Id:', v.id
                print 'Volume Status:', v.status
                print 'Volume Size:', v.size
                print 'Zone:', v.zone
                print 'Volume Type:', v.type
                print 'Encrypted:', v.encrypted

                #print attachment set object
                attachmentData = v.attach_data
                print 'Instance Id:', attachmentData.instance_id
                print 'Attached Time:', attachmentData.attach_time
                print 'Device:', attachmentData.device
                print '**********************************'

#Import classes from aws package
from aws import Connection
from aws import EC2Instance
from aws import Volumes
#import aws

connInst = Connection()
conn = connInst.ec2Connection()

#instantiate Volumes and list volumes
volumeInst = Volumes()
volumeInst.list_volumes(conn)

Script# 2 error is:

Traceback (most recent call last):
  File "/Users/arun/aws-vol-info2.py", line 30, in <module>
    from aws import Connection
ImportError: cannot import name Connection

If I comment the lines in script# 2 which says from aws ... .. and just use/uncomment import aws, then I'm getting this:

Traceback (most recent call last):
  File "/Users/arun/aws-vol-info2.py", line 35, in <module>
    connInst = Connection()
NameError: name 'Connection' is not defined

Solution

  • I think you're looking for vol.volume_type. You can see a full list of attributes in ec2.Volume here: http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#volume