Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get all EC2 Instance Types in All Availability Zones
#!/bin/bash
echo "Getting list of Availability Zones"
all_regions=$(aws ec2 describe-regions --output text --query 'Regions[*].[RegionName]' | sort)
all_az=()
while read -r region; do
az_per_region=$(aws ec2 describe-availability-zones --region $region --query 'AvailabilityZones[*].[ZoneName]' --output text | sort)
while read -r az; do
all_az+=($az)
done <<< "$az_per_region"
done <<< "$all_regions"
counter=1
num_az=${#all_az[@]}
for az in "${all_az[@]}"
do
echo "Checking Availability Zone $az ($counter/$num_az)"
region=$(echo $az | rev | cut -c 2- | rev)
raw=$(aws ec2 describe-reserved-instances-offerings --filters "Name=availability-zone,Values=$az" --region $region)
instance_types=$(echo $raw | jq '.ReservedInstancesOfferings[] | .InstanceType' | sort -u)
while read -r instance_type; do
echo "$region;$az;$instance_type" >> instance-types.csv
done <<< "$instance_types"
counter=$((counter+1))
done
@TryTryAgain

This comment has been minimized.

Copy link

@TryTryAgain TryTryAgain commented Dec 3, 2019

How long does this typically take you to run?

@feed1stblood

This comment has been minimized.

Copy link

@feed1stblood feed1stblood commented Dec 11, 2019

I found this runs very long time now. The reason this that reserved offers has many different options (up front, different durations) for each instance. Now there is another api "describe-instance-type-offerings". I changed

raw=$(aws ec2 describe-reserved-instances-offerings --filters "Name=availability-zone,Values=$az"  --region $region)
instance_types=$(echo $raw | jq '.ReservedInstancesOfferings[] | .InstanceType' | sort -u)

To

raw=$(aws ec2 describe-instance-type-offerings --filters "Name=location,Values=$az" --location-type availability-zone --region $region)
instance_types=$(echo $raw | jq '.InstanceTypeOfferings[] | .InstanceType' | sort -u

And it completed in around 1 minutes.

@TryTryAgain

This comment has been minimized.

Copy link

@TryTryAgain TryTryAgain commented Dec 12, 2019

Awesome, thanks so much for providing that updated information!

@pmoosh

This comment has been minimized.

Copy link

@pmoosh pmoosh commented Jan 4, 2020

the describe-instance-type-offerings API requires an updated aws cli installation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment