Skip to content

Instantly share code, notes, and snippets.

@xbalaji
Last active September 17, 2019 03:12
Show Gist options
  • Save xbalaji/5687fc0eb81636d2cd9404ed891b471b to your computer and use it in GitHub Desktop.
Save xbalaji/5687fc0eb81636d2cd9404ed891b471b to your computer and use it in GitHub Desktop.
aws cli shell script handling next token
walk_aws_cli()
{
ToCombine="$1"
shift
AwsCliCmd="$@"
DirName="${FUNCNAME[0]}-$$"
mkdir $DirName
LoopCount=0
NextToken=""
while :
do
OutFile="$DirName/aws-cli-out-$(printf %03d $LoopCount).json"
if [[ $NextToken ]]
then
$AwsCliCmd --starting-token $NextToken > $OutFile
else
$AwsCliCmd > $OutFile
fi
NextToken=$(jq '.NextToken // empty' "${OutFile}")
[[ -z $NextToken ]] && break
LoopCount=$(expr $LoopCount + 1)
done
SlurpStr="{ $ToCombine: map(.$ToCombine[]) }"
jq -s "$SlurpStr" $DirName/*json > "Full-$DirName.json"
rm -fr "$DirName"
}
# use it as below
walk_aws_cli Accounts aws organizations list-accounts
# verify using
walk_aws_cli Accounts aws organizations list-accounts --max-items 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment