Skip to content

Instantly share code, notes, and snippets.

@lukeplausin
Created April 19, 2017 16:12
Show Gist options
  • Save lukeplausin/a3670cd8f115a783a822aa0094015781 to your computer and use it in GitHub Desktop.
Save lukeplausin/a3670cd8f115a783a822aa0094015781 to your computer and use it in GitHub Desktop.
Example for depaginating boto3 results from AWS.
import boto3
def depaginate(function, resource_key, **kwargs):
# Will depaginate results made to an aws client
response = function(**kwargs)
results = response[resource_key]
while (response.get("NextToken", None) is not None):
response = function(NextToken=response.get("NextToken"), **kwargs)
results = results + response[resource_key]
return results
# I created this because the boto3 client for AWS organizations has no paginators
client = boto3.client("organizations")
parent_obj = client.list_roots()['Roots'][0]
child_accounts = depaginate(
function=client.list_accounts_for_parent,
resource_key='Accounts',
ParentId=parent_obj["Id"]
)
Sign up for free
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment