Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
35 changes: 31 additions & 4 deletions bash/lw_aws_inventory.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down