Skip to content

Instantly share code, notes, and snippets.

@wbirkmaier
Forked from marjamis/aws_cli_paginator.sh
Created December 16, 2020 22:10
Show Gist options
  • Save wbirkmaier/109bc22e5e838c3b3f1524ab333ab706 to your computer and use it in GitHub Desktop.
Save wbirkmaier/109bc22e5e838c3b3f1524ab333ab706 to your computer and use it in GitHub Desktop.
A simple sample of running the AWS CLI which will take consideration of pagination to get all results.
#!/bin/bash -xe
# Below command can be replaced to the required CLI, including with custom JSON output, assuming the NextToken is in the same location.
AWS_CLI_COMMAND="aws elasticbeanstalk list-platform-versions --max-records 100 --query={NextToken:NextToken,PlatformARNs:PlatformSummaryList[*].PlatformArn}"
OUTPUT_FILE="./output-$(date +%s)"
function CLI_call() {
if [ ! -v NEXT_TOKEN ]; then
cli_output=$($AWS_CLI_COMMAND)
else
cli_output=$($AWS_CLI_COMMAND --next-token $NEXT_TOKEN)
fi
echo $cli_output >> $OUTPUT_FILE
echo $cli_output | jq -r ".NextToken"
}
while [ "$NEXT_TOKEN" != "null" ]; do
NEXT_TOKEN=$(CLI_call $NEXT_TOKEN)
done
# Not 100% necessary but can be used to cleanup the output from the multiple API calls into a neater JSON formatted output.
cat $OUTPUT_FILE | jq [.PlatformARNs[]] > $OUTPUT_FILE-master.json
rm $OUTPUT_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment