Skip to content

Instantly share code, notes, and snippets.

@marjamis
Created July 2, 2017 07:05
Show Gist options
  • Save marjamis/abf7ec76f8c4f720294425977323e84f to your computer and use it in GitHub Desktop.
Save marjamis/abf7ec76f8c4f720294425977323e84f 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
@iamtirth
Copy link

line 8: if [ -z $NEXT_TOKEN ]; then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment