List all EBS volumes in an AWS Account

1.5k Views Asked by At

I am trying to get a list of all the EBS volumes in an AWS account. I'm using Python 3 and boto3 version 1.10.34.

I found this post with some suggestions but neither of them work.

I'm setting the ec2_client like this: import boto3 session = boto3.Session(profile_name=aws_account,region_name='us-east-1') ec2_client = session.client('ec2')

If I try volumes = ec2_client.volumes.all() I get back AttributeError: 'EC2' object has no attribute 'volumes'.

If I try volumes = ec2_client.get_all_volumes()I get back AttributeError: 'EC2' object has no attribute 'get_all_volumes'.

How can I do this correctly?

1

There are 1 best solutions below

1
Azize On

I suggest you use paginator when you need to describe "all" volumes.
If you have more than 1000 volumes, describe_volumes() will not describe all volumes, just the first 1000.

Let me quote the reference documentation below.

Some AWS operations return results that are incomplete and require subsequent requests in order to attain the entire result set. The process of sending subsequent requests to continue where a previous request left off is called pagination. For example, the list_objects operation of Amazon S3 returns up to 1000 objects at a time, and you must send subsequent requests with the appropriate Marker in order to retrieve the next page of results.

Paginators are a feature of boto3 that act as an abstraction over the process of iterating over an entire result set of a truncated API operation.

Reference: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/paginators.html

See an snippet below:

def get_all_volumes(session):
    volumes = []

    ec2 = session.client("ec2")
    # Get all Volumes using paginator
    paginator = ec2.get_paginator("describe_volumes")
    page_iterator = paginator.paginate()
    for page in page_iterator:
        volumes.extend(page["Volumes"])
    return volumes