Amazon Web Services (AWS) - List Elastic Block Storage (EBS) Volumes using Python boto3

by
Jeremy Canfield |
Updated: April 01 2024
| Amazon Web Services (AWS) articles
This assumes you are familar with the basic configurations needed to connect to Amazon Web Services (AWS) using Python boto3. If not, check out my article Python (Scripting) - Getting Started with Amazon Web Services (AWS) boto3.
Here is the minimal boilterplate code without error handling to list your Elastic Block Storage (EBS) Volumes.
#!/usr/bin/python3
import boto3
client = boto3.client('ec2')
response = client.describe_volumes()
print(response)
Here is a more practical example, with try/except/else error handling.
#!/usr/bin/python3
import boto3
import sys
try:
client = boto3.client('ec2')
except Exception as exception:
print(exception)
sys.exit(1)
try:
response = client.describe_volumes()
except Exception as exception:
print(exception)
try:
response['Volumes']
except KeyError as keyerror:
print(keyerror)
else:
for volume in response['Volumes']:
for key in volume:
print(f"{key} = {volume[key]}")
Something like this should be returned
AvailabilityZone = us-east-1a
CreateTime = 2024-03-14 01:25:44.141000+00:00
Encrypted = False
Size = 8
SnapshotId = snap-0596d2ead8bde978f
State = in-use
VolumeId = vol-0fb7a8d03ce5eb4de
Iops = 3000
VolumeType = gp3
MultiAttachEnabled = False
Throughput = 125
Did you find this article helpful?
If so, consider buying me a coffee over at