How to use Boto3 pagination
<p>BACKGROUND:</p>
<p>The AWS operation to list IAM users returns a max of 50 by default.</p>
<p>Reading the docs (links) below I ran following code and returned a complete set data by setting the &quot;MaxItems&quot; to 1000.</p>
<pre><code>paginator = client.get_paginator('list_users')
response_iterator = paginator.paginate(
 PaginationConfig={
     'MaxItems': 1000,
     'PageSize': 123})
for page in response_iterator:
    u = page['Users']
    for user in u:
        print(user['UserName'])
</code></pre>
<p><a href="http://boto3.readthedocs.io/en/latest/guide/paginators.html" rel="noreferrer">http://boto3.readthedocs.io/en/latest/guide/paginators.html</a>
<a href="https://boto3.readthedocs.io/en/latest/reference/services/iam.html#IAM.Paginator.ListUsers" rel="noreferrer">https://boto3.readthedocs.io/en/latest/reference/services/iam.html#IAM.Paginator.ListUsers</a></p>
<p>QUESTION:</p>
<p>If the &quot;<strong>MaxItems</strong>&quot; was set to 10, for example, what would be the best method to loop through the results?</p>
<p>I tested with the following but it only loops 2 iterations before 'IsTruncated' == False and results in &quot;KeyError: 'Marker'&quot;. Not sure why this is happening because I know there are over 200 results.</p>
<pre><code>marker = None

while True:
    paginator = client.get_paginator('list_users')
    response_iterator = paginator.paginate( 
        PaginationConfig={
            'MaxItems': 10,
            'StartingToken': marker})
    #print(response_iterator)
    for page in response_iterator:
        u = page['Users']
        for user in u:
            print(user['UserName'])
            print(page['IsTruncated'])
            marker = page['Marker']
            print(marker)
        else:
            break
</code></pre>