Skip to content

Instantly share code, notes, and snippets.

@iaon
Forked from serithemage/instance-types.sh
Last active June 2, 2023 18:42
Show Gist options
  • Save iaon/199f812060d3fc0e26a6b297ebeac356 to your computer and use it in GitHub Desktop.
Save iaon/199f812060d3fc0e26a6b297ebeac356 to your computer and use it in GitHub Desktop.
Get all EC2 Instance Types in All Availability Zones (Output as table instance_type vs AZ)
#!/bin/bash
# Getting list of Availability Zones from target region
# all
#target_regions=$(aws ec2 describe-regions --output text --query 'Regions[*].[RegionName]' | sort)
# default region
target_regions=$(aws configure get region)
echo "Target regions:"
echo $target_regions
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 <<< "$target_regions"
counter=1
num_az=${#all_az[@]}
echo > instance-types.csv
JCMD="| join --header -e"-" -o auto -a1 -a2 ALL-instance-types.csv -"
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-instance-type-offerings --filters "Name=location,Values=$az" --location-type availability-zone --region $region)
instance_types=$(echo $raw | jq '.InstanceTypeOfferings[] | .InstanceType' | sort -u)
# echo -n $az > instance-types-$az.csv
while read -r instance_type; do
echo $instance_type >> instance-types-$az.tmp
done <<< "$instance_types"
counter=$((counter+1))
echo instance_type $az > instance-types-$az.csv
sort instance-types-$az.tmp >> instance-types-$az.csv; rm instance-types-$az.tmp
JCMD="${JCMD}| join --header -e"-" -o auto -a1 -a2 instance-types-${az}.csv -"
done
echo instance_type > ALL-instance-types.csv
cat instance-types-*.csv | sort | uniq >> ALL-instance-types.csv
sed -i 's/"$/" \+/' instance-types-*.csv
bash -c "echo ${JCMD}" > instance-types.csv
echo See instance-types.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment