Skip to content

Instantly share code, notes, and snippets.

@justmiles
Last active July 9, 2020 20:43
Show Gist options
  • Save justmiles/969a92102e458e99eee6952ee88acfcb to your computer and use it in GitHub Desktop.
Save justmiles/969a92102e458e99eee6952ee88acfcb to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# aws-paginated
# usage: aws-paginated <any aws cli command>
# requirements: aws-cli, jq
#
function aws-paginated() {
AWS_CLI_COMMAND="${@}"
OUTPUT=$(mktemp)
function _invoke() {
TMP_OUTPUT=$(mktemp)
if [ -z "$NEXT_TOKEN" ]; then
eval "aws $AWS_CLI_COMMAND --output json" >$TMP_OUTPUT
else
eval "aws $AWS_CLI_COMMAND --output json --next-token '$NEXT_TOKEN'" >$TMP_OUTPUT
fi
cat $TMP_OUTPUT | jq -c 'del(.NextToken)' >>$OUTPUT
jq -r ".NextToken" $TMP_OUTPUT
}
NEXT_TOKEN=$(_invoke $NEXT_TOKEN)
while [ ! -z "$NEXT_TOKEN" ]; do
NEXT_TOKEN=$(_invoke $NEXT_TOKEN)
done
cat $OUTPUT | jq -s '{PaginatedResults: .}'
rm -rf $OUTPUT
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment