Created
July 2, 2017 07:05
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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
line 8: if [ -z $NEXT_TOKEN ]; then