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.
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! 🎉
-
December 18 th, 2023
860 Words
I am a huge fan of Computer Interfaces; especially non-textual ones. Spoken words can be a powerful interface to digital services; Amazon Web Services has various services and products available that work with audio. To identify language in spoken words and extract textual information, you can use Amazon Transcribe and analize audio files.
-
December 10 th, 2023
1117 Words
The weekend was nice; I had some fun using the bunq API and configure webhooks for account activities and explained how to use Amazon EventBridge Pipes to process and transform messages from SQS. Now, it’s time to combine both and get bunq events to Amazon EventBridge!
-
December 10 th, 2023
367 Words
When managing continuous events, Amazon EventBridge is a powerful and fully managed service. With Amazon EventBridge Pipes it’s easy to organize, structure, and transform incoming data messages. Using the AWS Cloud Development Kit, this guide will configure an Amazon SQS Queue and use Amazon EventBridge Pipes for data processing and transformation.
-
December 9 th, 2023
1505 Words
The digital banking service bunq has an API for their banking accounts. With support for webhooks, you can easily track any activity within your account! bunq calls them Callbacks. This guide explains how to use the bunq mobile application and some cURL requests to get a JSON request for every activity in your bunq banking accounts.
-
December 3 rd, 2023
369 Words
AWS Step Functions can easily be used to wrap existing AWS services and persist specific use cases. For example, AWS Step Functions can call the Amazon Bedrock API to generate text responses using the AI21 Labs Jurassic-2 models. You can create a Step Function using the AWS Management Console, or the AWS Cloud Development Kit.
-
November 26 th, 2023
290 Words
The current en vogue alternative for Twitter is the invite-only services Bluesky. Based on the open specifications of the foundational concept, the AT Protocol, and the available API client on GitHub, it is not that complex to retrieve details of one of your posts; for example the number of likes.
-
October 31 st, 2023
535 Words
The previous post described how to create an Amazon EventBridge Event Bus with Amazon CloudWatch Log forwarding using the AWS CDK. Publishing events works fine with the AWS Management Console or the AWS CLI, but you can also use Service Integrations for Amazon API Gateway; this works for HTTP and Rest API Gateways.