AWS SDK: Pagination Pattern for JavaScript SDK v3

October 25th, 2023 305 Words

When using the AWS Software Development Kit, pagination in API responses is a common and sometimes annoying to deal with. Thanks to a shared interfaced in @aws-sdk clients, can you can establish a common pattern to interact with AWS paginated API actions using the JavaScript SDK v3. Retrieving all AWS Accounts in your AWS Organization is a neat example use case for this.

Retrieve list of AWS Accounts

The available ListAccounts API responds with 10 items per page and you must use the commong NextToken property to define your position within the list. This is usually the same API functionality available in all AWS SDK clients.

You can retrieve the first ten AWS accounts with this:

import {
    OrganizationsClient, 
    ListAccountsCommand,
} from '@aws-sdk/client-organizations'

const client = new OrganizationsClient(
    { region: 'eu-central-1' }
)

const command = new ListAccountsCommand(
    { NextToken: 'STRING_VALUE' })

const data = await client.send(command);

console.log(data)

Of course, you just need to wrap these lines in a loop and maintain a list of all accounts and add the next ten AWS accounts to this list until the API stops responding with items.

Pagination for AWS JavaScript v3 SDK

Lucky us, the available current version 3 of the AWS JavaScript SDK has a Paginator Pattern already included.

For the example above, the ListAccountsCommand object has as a paginated pendant available; this is called paginateListAccounts and supports a custom configuration for automated pagination handling:

import {
    Account,
    OrganizationsClient,
    paginateListAccounts
} from '@aws-sdk/client-organizations'

const client = new OrganizationsClient(
    { region: 'eu-central-1' }
)

const paginator = paginateListAccounts(
    { client, pageSize: 10 }, {}
)

const accounts: Account[] = []

for await (const page of paginator) {
    accounts.push(...page.Accounts!)
}

console.log(accounts)

Thanks to the simple Iterator Pattern available in the AWS Software Development Kit v3 for JavaScript scripts and helper functions like this are way more readable! 🎉