Created
June 11, 2021 12:51
-
-
Save samjarrett/64008f3fea33724ece19073d6f55643d to your computer and use it in GitHub Desktop.
boto paginators the right way
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
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