1

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?

bluethundr
  • 1,005
  • 17
  • 68
  • 141
  • the EC2 Client object has no `get_all_volumes` method – gold_cy Feb 15 '20 at 23:05
  • Check the documentation. There should be something like describe_ebs_volumes. – Asdfg Feb 15 '20 at 23:08
  • Yeah thanks. The method is actually `describe_volumes()`. I found it and it works. Thanks for playing! :) – bluethundr Feb 15 '20 at 23:10
  • 1
    your first call should have worked technically looking at the [docs](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.ServiceResource.volumes) – gold_cy Feb 15 '20 at 23:11

1 Answers1

3

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
Azize
  • 4,006
  • 2
  • 22
  • 38