Python Boto3 Paginating Error: 'PageIterator' object is not subscriptable

Viewed 2336

I'm trying to get a complete list of instances available in a region. The code will iterate over a number of pages but stops short with an error:

Traceback (most recent call last):
  File "list_available_instance_offerings.py", line 29, in <module>
    marker = page_iterator['Marker']
TypeError: 'PageIterator' object is not subscriptable

How can I iterate over all of the pages without erroring prematurely?

Here's my script:

import sys
import boto3

ec2 = boto3.client("ec2")
marker = None
while True:
    paginator = ec2.get_paginator('describe_instance_type_offerings')
    page_iterator = paginator.paginate(
        LocationType='availability-zone',Filters=[{'Name': 'location', 'Values':['us-east-1a']}],
        PaginationConfig={
            'PageSize': 50,
            'StartingToken': marker})
    for page in page_iterator:
        offerings = page['InstanceTypeOfferings']
        for offer in offerings:
            print(offer['InstanceType'])
    try:
        marker = page_iterator['Marker']
    except KeyError:
        sys.exit()
2 Answers

There is no such property as Marker. I belive that you are after NextToken from page. In this case, it should be:

    try:
        marker = page['NextToken']
    except KeyError:
        sys.exit()

When using a boto3 paginator, you don't need to worry about the Marker. The purpose of the paginator is to manage that for you.

client = boto3.client('route53')
paginator = client.get_paginator('list_health_checks')

response_iterator = paginator.paginate(
    PaginationConfig={
        'PageSize': 10
    }
)

for page in response_iterator:
    for healthcheck in page['HealthChecks']:
        print(healthcheck["Id"])

If you have 87 health checks, this will list 8 pages of 10 and 1 page of 7.

You can also use MaxItems if you want to limit the output. For example, if you have MaxItems=80 you will get 8 pages of 10.

Related