Last active
September 17, 2019 03:12
-
-
Save xbalaji/5687fc0eb81636d2cd9404ed891b471b to your computer and use it in GitHub Desktop.
aws cli shell script handling next token
This file contains hidden or 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
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