Had a similar requirement as above, but also wanted to filter on running instances and view the results per region (sorted).
Using some of the awesome responses from @Vladimir-Kovpak, and @ Anthony Neace, I ended up with this, may help someone (those guys helped me)
for region in $(aws ec2 describe-regions --query 'Regions[].RegionName | sort(@)' --output text); \
do echo $region; \
aws ec2 describe-instances \
--region "${region}" \
--filter Name=instance-state-name,Values=running \
--query 'Reservations[].Instances[].{InstanceType:InstanceType}' \
| jq -r '.[].InstanceType' \
| sort \
| uniq -c \
| sort -r;
done
... resulting in output similar to below...
ap-northeast-1
ap-northeast-2
ap-south-1
ap-southeast-1
5 m3.medium
4 m4.xlarge
3 t2.micro
2 t2.large
2 t2.medium
2 m3.large
1 t2.xlarge
1 t2.small
1 m4.large
ap-southeast-2
5 t2.medium
4 m4.xlarge
3 t2.micro
2 t2.large
2 m3.medium
2 m3.large
1 t2.xlarge
1 t2.small
1 m4.large
ca-central-1
eu-central-1
eu-west-1
eu-west-2
eu-west-3
sa-east-1
us-east-1
us-east-2
us-west-1
us-west-2
Also, to save having to paste this in each time, I added it to my aws alias file. Run on the cli with:
aws all
Example below. (create at $HOME/.aws/cli/alias)
See the aws cli alias github repo for details (and more great time saving aliases)
# Add to your existing, or create this file at $HOME/.aws/cli/alias
[toplevel]
# Run, with 'aws who'
who = sts get-caller-identity
# Run, with 'aws all'
all = !
! g() {
for region in $(aws ec2 describe-regions --query 'Regions[].RegionName | sort(@)' --output text); \
do echo $region; \
aws ec2 describe-instances \
--region "${region}" \
--filter Name=instance-state-name,Values=running \
--query 'Reservations[].Instances[].{InstanceType:InstanceType}' \
| jq -r '.[].InstanceType' \
| sort \
| uniq -c \
| sort -r;
done
}; g