Created
April 19, 2017 16:12
-
-
Save lukeplausin/a3670cd8f115a783a822aa0094015781 to your computer and use it in GitHub Desktop.
Example for depaginating boto3 results from AWS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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