How to get a list of EBS volume in EC2 using Python

3.4k Views Asked by At

I am trying to get a list of EBS volumes in EC2 using python.

Here is my code:

import boto3
import objectpath
aws_account = 'company-lab'
region = 'us-east-1'
session = boto3.Session(profile_name=aws_account, region_name=region)
ec2 = session.client("ec2")
instance_list = ec2.describe_instances()
for reservation in instance_list["Reservations"]:
    for instance in reservation.get("Instances", []):
        tree = objectpath.Tree(instance)
        block_devices = set(tree.execute('$..BlockDeviceMappings[\'Ebs\'][\'VolumeId\']'))
        block_devices = list(block_devices)
        for volume_id in block_devices:
            volume = ec2.Volume(volume_id)

When I try that I get back the following error:

Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
  File "C:\Users\tdun0002\AppData\Local\Programs\Python\Python38-32\lib\site-packages\botocore\client.py", line 573, in __getattr__
    raise AttributeError(
AttributeError: 'EC2' object has no attribute 'Volume'

I am trying to use the boto3 EC2 Volume attribute. I would like to get a list of EBS volumes and their sizes for any given EC2 instance. How can I do that?

1

There are 1 best solutions below

3
On BEST ANSWER

"I would like to get a list of EBS volumes and their sizes for any given EC2 instance."

Here is code using the resource method:

import boto3

ec2_resource = boto3.resource('ec2')

for instance in ec2_resource.instances.all():
    for volume in instance.volumes.all():
        print(instance.id, volume.id, volume.volume_type, volume.size)

And using the client method:

import boto3

ec2_client = boto3.client('ec2')

response = ec2_client.describe_instances()

for reservation in response['Reservations']:
    for instance in reservation['Instances']:
        volumes = ec2_client.describe_volumes(
            Filters=[{'Name':'attachment.instance-id','Values':[instance['InstanceId']]}]
        )
        for disk in volumes['Volumes']:
            print(instance['InstanceId'], disk['VolumeId'], disk['VolumeType'], disk['Size'])

However, this results in multiple API calls (one DescribeInstances() and then one DescribeVolumes() for each instance).

This version just uses a single call to DescribeVolumes() and sorts by InstanceId:

import boto3

ec2_resource = boto3.resource('ec2')

volumes = [(v.attachments[0]['InstanceId'], v.id, v.size)
           for v in ec2_resource.volumes.filter(Filters=[{'Name':'attachment.status','Values':['attached']}])]

for volume in sorted(volumes):
    print(volume[0], volume[1], volume[2])

Here is the equivalent code using the client method:

import boto3

ec2_client = boto3.client('ec2')

response = ec2_client.describe_volumes(Filters=[{'Name':'attachment.status','Values':['attached']}])

volumes = [(v['Attachments'][0]['InstanceId'], v['VolumeId'], v['Size']) for v in response['Volumes']]

for volume in sorted(volumes):
    print(volume[0], volume[1], volume[2])

In addition to the license granted under the terms of service of this site the contents of this post are licensed under MIT-0.