Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create -e option for skipping AWS profile use #10

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
30 changes: 20 additions & 10 deletions lw_aws_inventory.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,30 @@
# Requirements: awscli, jq

# You can specify a profile with the -p flag, or get JSON output with the -j flag.
# You can specify to use environment variables and not specify a profile with -e flag.
# Note that the script takes a while to run in large accounts with many resources.


AWS_PROFILE=default

# Usage: ./lw_aws_inventory.sh
while getopts ":jp:" opt; do
while getopts ":jep:" opt; do
case ${opt} in
p )
AWS_PROFILE=$OPTARG
;;
j )
JSON="true"
;;
e )
SKIP_PROFILE="true"
;;
\? )
echo "Usage: ./lw_aws_inventory.sh [-p profile] [-j]" 1>&2
echo "Usage: ./lw_aws_inventory.sh [-p profile] [-e] [-j]" 1>&2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to be a bit more verbose with this echo. Some info on what -e & -j CLI args do.

exit 1
;;
: )
echo "Usage: ./lw_aws_inventory.sh [-p profile] [-j]" 1>&2
echo "Usage: ./lw_aws_inventory.sh [-p profile] [-e] [-j]" 1>&2
exit 1
;;
esac
Expand All @@ -37,38 +41,44 @@ ELB_V1=0
ELB_V2=0
NAT_GATEWAYS=0

if [ "x$SKIP_PROFILE" = "xtrue" ]; then
AWS_PROFILE_STR=""
else
AWS_PROFILE_STR="--profile $AWS_PROFILE"
fi

function getRegions {
aws --profile $AWS_PROFILE ec2 describe-regions --output json | jq -r '.[] | .[] | .RegionName'
aws $AWS_PROFILE_STR ec2 describe-regions --output json | jq -r '.[] | .[] | .RegionName'
}

function getInstances {
region=$1
aws --profile $AWS_PROFILE ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId]' --region $r --output json --no-paginate | jq 'flatten | length'
aws $AWS_PROFILE_STR ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId]' --region $r --output json --no-paginate | jq 'flatten | length'
}

function getRDSInstances {
region=$1
aws --profile $AWS_PROFILE rds describe-db-instances --region $r --output json --no-paginate | jq '.DBInstances | length'
aws $AWS_PROFILE_STR rds describe-db-instances --region $r --output json --no-paginate | jq '.DBInstances | length'
}

function getRedshift {
region=$1
aws --profile $AWS_PROFILE redshift describe-clusters --region $r --output json --no-paginate | jq '.Clusters | length'
aws $AWS_PROFILE_STR redshift describe-clusters --region $r --output json --no-paginate | jq '.Clusters | length'
}

function getElbv1 {
region=$1
aws --profile $AWS_PROFILE elb describe-load-balancers --region $r --output json --no-paginate | jq '.LoadBalancerDescriptions | length'
aws $AWS_PROFILE_STR elb describe-load-balancers --region $r --output json --no-paginate | jq '.LoadBalancerDescriptions | length'
}

function getElbv2 {
region=$1
aws --profile $AWS_PROFILE elbv2 describe-load-balancers --region $r --output json --no-paginate | jq '.LoadBalancers | length'
aws $AWS_PROFILE_STR elbv2 describe-load-balancers --region $r --output json --no-paginate | jq '.LoadBalancers | length'
}

function getNatGateways {
region=$1
aws --profile $AWS_PROFILE ec2 describe-nat-gateways --region $r --output json --no-paginate | jq '.NatGateways | length'
aws $AWS_PROFILE_STR ec2 describe-nat-gateways --region $r --output json --no-paginate | jq '.NatGateways | length'
}

for r in $(getRegions); do
Expand Down