85 words
1 minute
Helper function for pagination using AWS boto3

Helper function for pagination using AWS boto3#

I noticed that a lot of my boto3 code in s3-credentials looked like this:

paginator = iam.get_paginator("list_user_policies")
for response in paginator.paginate(UserName=username):
for policy_name in response["PolicyNames"]:
print(policy_name)

This was enough verbosity that I was hesitating on implementing pagination properly for some method calls.

I came up with this helper function to use instead:

def paginate(service, method, list_key, **kwargs):
paginator = service.get_paginator(method)
for response in paginator.paginate(**kwargs):
yield from response[list_key]

Now the above becomes:

for policy_name in paginate(iam, "list_user_policies", "PolicyNames", UserName=username):
print(policy_name)

Here’s the issue and the refactoring commit.

Helper function for pagination using AWS boto3
https://mranv.pages.dev/posts/helper-function-for-pagination-using-aws-boto3/
Author
Anubhav Gain
Published at
2024-06-07
License
CC BY-NC-SA 4.0