From 4174165cee466214bfc4c770a2e9bdac0866bd6d Mon Sep 17 00:00:00 2001 From: MattAlexWright Date: Mon, 7 Mar 2022 09:46:26 -0500 Subject: [PATCH] Added: aws organizations pull flag, account progress tracking. Fixed: space handling in account names. --- .DS_Store | Bin 0 -> 6148 bytes bash/lw_aws_inventory.sh | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..603159ac412efed756f42d51609fbcd6ffdb3d61 GIT binary patch literal 6148 zcmeH~F^1ZP5Ww=_4llxh+TmDIf);fE17d zGg2Ur@zr`p&!k6@0#aZe3i$V-(495eI^)y95F-FNupGv9%o1er0$G!-lNFlf^kCU) zF@|_O+R2jF)nx1J?XVm^EbnYS#n7y`!wM6c)qsK&kOC70mOUT+{6EtFn*S#)N~M4l z_-6{(a5x?Ie5pKJf4!d9&sp_#qmywt!`n{)6F-VK^f2xhUywD~I$5FVM<8TSkOCi7 F;0K&i5~BbB literal 0 HcmV?d00001 diff --git a/bash/lw_aws_inventory.sh b/bash/lw_aws_inventory.sh index a9fa1f6..aa3998d 100755 --- a/bash/lw_aws_inventory.sh +++ b/bash/lw_aws_inventory.sh @@ -7,14 +7,25 @@ # 1. You can specify multiple accounts by passing a comma seperated list, e.g. "default,qa,test", # there are no spaces between accounts in the list # 2. The script takes a while to run in large accounts with many resources, the final count is an aggregation of all resources found. +# 3. You can use aws organizations to import a list of all of your connected accounts using the o flag. - +# Need to modify the IFS value to account for spaces in names +# Save any existing IFS values in the placeholder +IFSPLACEHOLDER=$IFS AWS_PROFILE=default # Usage: ./lw_aws_inventory.sh -while getopts ":jp:" opt; do +while getopts ":jop:" opt; do case ${opt} in + o ) + # The jq parsing from aws organizations output will yield newline delims + # Need to modify the IFS to account for that + IFS=$'\n' + AWS_PROFILE=$(aws organizations list-accounts | jq .[] | jq .[] | jq .Name) + ;; p ) + # Need to set the IFS to split only on commas + IFS=, AWS_PROFILE=$OPTARG ;; j ) @@ -127,7 +138,7 @@ function calculateInventory { lambdafns=$(getLambdaFunctions $r $profile) LAMBDA_FNS=$(($LAMBDA_FNS + $lambdafns)) - if [ $LAMBDA_FNS -gt 0 ]; then LAMBDA_FNS_EXIST="Yes"; fi + if [ $LAMBDA_FNS -gt 0 ]; then LAMBDA_FNS_EXIST="Yes"; fi done TOTAL=$(($EC2_INSTANCES + $RDS_INSTANCES + $REDSHIFT_CLUSTERS + $ELB_V1 + $ELB_V2 + $NAT_GATEWAYS)) @@ -171,11 +182,27 @@ function jsonoutput { echo "}" } -for PROFILE in $(echo $AWS_PROFILE | sed "s/,/ /g") +# Get the number of accounts to iterate over +PROFCOUNT=0 +for PROFILE in $AWS_PROFILE +do + PROFCOUNT=$(($PROFCOUNT + 1)) +done + +# Start an iterator to track progress +CURRENT=0 +for PROFILE in $AWS_PROFILE do + CURRENT=$(($CURRENT + 1)) + # Need to strip the quotes + PROFILE=$(echo $PROFILE | sed 's/"//g') + echo "Working on" $PROFILE $CURRENT"/"$PROFCOUNT calculateInventory $PROFILE done +# Reset IFS to whatever it was before we started +IFS=$IFSPLACEHOLDER + if [ "$JSON" == "true" ]; then jsonoutput else