Sub topic 13
Pagination and waiters
This chapter covers two features that make CLI commands behave well against large or slow services. It assumes you can run aws commands with a working profile and Region, and that you can read JSON output and use --query. Pagination controls how many results come back from a list call. Waiters pause until a resource reaches a state.
How pagination works
Many AWS list operations return only part of the data per request. The service sends one page of results and a token that points to the next page. The AWS CLI hides most of this. By default it calls the API repeatedly until it has collected every page, then prints the combined result.
That default is convenient but can be slow on a bucket with millions of objects. Two options let you control it:
--max-itemslimits the total number of items the CLI returns.--page-sizesets how many items the CLI asks for in each underlying API call.
aws s3api list-objects-v2 --bucket amzn-s3-demo-bucket --max-items 50 --page-size 10This command returns at most 50 items, fetched in pages of 10. That is about five API calls. A smaller page size can help when a single large page times out. It does not change what you get back.
Continuing after a limit
When --max-items stops the output early, the CLI includes a NextToken field in the JSON. Pass its value to --starting-token to resume where the last call ended.
aws s3api list-objects-v2 --bucket amzn-s3-demo-bucket --max-items 50
aws s3api list-objects-v2 --bucket amzn-s3-demo-bucket --max-items 50 --starting-token "TOKEN_FROM_PREVIOUS_OUTPUT"Replace the placeholder with the real token from the first call's output. Keep every other option the same, or the token may not be valid.
If you want everything on one page, add --no-paginate. The CLI then makes a single call and returns only the first page. This is useful for a quick look, but it is a common source of missing results in scripts.
Waiters
Many operations are asynchronous. A call such as run-instances returns right away, while the instance is still starting. A waiter polls a describe operation until the resource reaches the state you need, then exits.
aws ec2 wait instance-running --instance-ids i-0123456789abcdef0The command prints nothing on success and returns exit code 0. It checks at fixed intervals. If the state is not reached after the maximum number of attempts, it fails with exit code 255 and an error message. For EC2 instance waiters, the CLI checks every 15 seconds, up to 40 times.
You can list the waiters for a service with the built-in help:
aws ec2 wait helpBecause the exit code reflects the result, waiters work well in scripts. A command after && runs only when the wait succeeds.
Common mistakes
- Expecting
--max-itemsto cut the time of every call. The CLI may still read all pages up to the limit. - Using
--no-paginatein a script and missing results on later pages. - Ignoring the exit code of a waiter and continuing with a resource that is not ready.
- Passing a stale
--starting-tokenafter changing other options.
Exercise
You run aws s3api list-objects-v2 --bucket amzn-s3-demo-bucket --max-items 100 and the output ends with a NextToken. How do you get the next 100 items, and what happens if a waiter you run later times out?
Show answer
Run the same command again and add --starting-token with the NextToken value from the first output. For the waiter, it exits with code 255 and an error after it exhausts its attempts, so a script using && will not run the following command.