· engineering · 2 min read
Paginating in AWS SDK
Easy pagination of api calls using AWS SDK's little known built-in utilities.
Table of Contents
It’s a common task to fetch lists of AWS resources, and managing the pagination of these resources can often seem like a chore. Whether you’re dealing with DynamoDB items, S3 objects, or account listings, each service seems to have its method for handling continuation tokens.
Pagination utilities to the rescue!
Luckily for us, Amazon have taken care of this for us in the JavaScript SDK v3, providing a set of pagination utilities that greatly simplify the task. For example:
For most of the API operations, there is now a paginator pattern implemented, with the prefix ‘paginate’, making the pattern paginate<operation>
- for example:
- S3: List objects with paginateListObjectsV2
- DynamoDb: Query items with paginateQuery
- SSM: List Parameters with paginateDescribeParameters
Using it is easy, the paginators return an AsyncIterator, so we can just loop over it as follows:
The paginationConfig
parameter allows us to specify things like the page size and the startingToken
That’s it - no more boiler plate, and less scope for bugs to creep in!
Further reading
It’s worth reading up on async generator functions to understand how this has been implemented.
There’s similar functionality in the other SDKs too, for example php, python, java, perhaps the Javascript documentation is just lacking.
About James Babington
A cloud architect and engineer with a wealth of experience across AWS, web development, and security, James enjoys writing about the technical challenges and solutions he's encountered, but most of all he loves it when a plan comes together and it all just works.
No comments yet. Be the first to comment!