Skip to content

Instantly share code, notes, and snippets.

@samjarrett
Created June 11, 2021 12:51
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samjarrett/64008f3fea33724ece19073d6f55643d to your computer and use it in GitHub Desktop.
Save samjarrett/64008f3fea33724ece19073d6f55643d to your computer and use it in GitHub Desktop.
boto paginators the right way
def paginate(method, **kwargs):
client = method.__self__
paginator = client.get_paginator(method.__name__)
for page in paginator.paginate(**kwargs).result_key_iters():
for result in page:
yield result
# usage:
import boto3
ec2 = boto3.client("ec2")
instances = paginate(ec2.describe_instances, Filters=[...])
for instance in instances:
# ....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment