Skip to content

Instantly share code, notes, and snippets.

@kszarek
Last active June 19, 2019 15:18
Show Gist options
  • Save kszarek/57c2aec45d4c20f61d5f73a2d7178c56 to your computer and use it in GitHub Desktop.
Save kszarek/57c2aec45d4c20f61d5f73a2d7178c56 to your computer and use it in GitHub Desktop.
Bash script to get ec2 and ebs count from multiple AWS accounts
#!/usr/bin/env bash
AWS_REGION=eu-west-1
envs='staging development dt-staging dt-production qa accounting team-bravo'
aws() {
docker run --rm -it \
--mount type=bind,source="$HOME"/.aws,destination=/root/.aws,readonly \
--mount type=bind,source="$(pwd)",destination=/data,consistency=cached \
-e AWS_PROFILE \
-e AWS_REGION \
kszarek/aws "$@"
}
get_ec2_count(){
ENV_NAME="$1"
AWS_PROFILE=$ENV_NAME \
count=$(aws ec2 describe-instances --output json | grep -c InstanceId)
echo "ec2 count for $ENV_NAME: $count"
}
get_ebs_size_count(){
ENV_NAME="$1"
AWS_PROFILE=$ENV_NAME \
count=$(aws ec2 describe-volumes --query 'Volumes[*].{Size:Size}' --output text|awk '{s+=$1} END {printf "%.0f\n", s}')
echo "ebs size for $ENV_NAME: $count"
}
get_rds_count(){
AWS_PROFILE="$1" \
count=$(aws rds describe-db-instances --output json|jq '.DBInstances[].DBInstanceIdentifier'|wc -l)
echo "rds count for $AWS_PROFILE: $count"
}
for env in $envs
do
get_ec2_count "$env"
done
for env in $envs
do
get_ebs_size_count "$env"
done
for env in $envs
do
get_rds_count "$env"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment