Created
November 9, 2017 09:20
-
-
Save padilo/773343c56595071417a6883cd0f50139 to your computer and use it in GitHub Desktop.
Check the uses of an AMI in all configured AWS accounts
This file contains 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
#!/usr/bin/env bash | |
profiles="$((egrep '^\[ *[a-zA-Z0-9_-]+ *\]$' ~/.aws/credentials 2>/dev/null; grep '\[profile' ~/.aws/config 2>/dev/null | sed 's|\[profile ||g') | tr -d '[]' | sort | uniq)" | |
ami=$1 | |
total_usages=0 | |
ec2_usages() { | |
usages=$(AWS_DEFAULT_PROFILE=$profile aws --output=json ec2 describe-instances | grep ImageId | sed -n 's|.*"\(ami-[^"]*\).*|\1|p' | grep $ami | wc -l | tr -d " ") | |
echo "$profile has $usages usages on ec2" | |
total_usages=$(($total_usages+$usages)) | |
} | |
launchconfig_usages() { | |
usages=$(AWS_DEFAULT_PROFILE=$profile aws --output=json autoscaling describe-launch-configurations | grep ImageId | sed -n 's|.*"\(ami-[^"]*\).*|\1|p' | grep $ami | wc -l | tr -d " ") | |
echo "$profile has $usages usages on launchconfigurations" | |
total_usages=$(($total_usages+$usages)) | |
} | |
for profile in $profiles; do | |
ec2_usages | |
launchconfig_usages | |
done | |
echo "" | |
echo "Total usages: $total_usages" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment