Last active
August 7, 2024 10:32
-
-
Save huevos-y-bacon/1c1d1afa58bf3ddb32ac9e1946ca7aef to your computer and use it in GitHub Desktop.
AWS EC2 - Get supported EC2 instance types in the current account and region. If you only need to check for specific types, then specify them in types.txt in the script directory, otherwise it will process all available instance types in the region. Tested on MacOS.
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 | |
# Get supported EC2 instance types in the current account and region. If you | |
# only need to check for specific types, then specify them in types.txt in | |
# the script directory, otherwise it will process all available instance types | |
# in the region. Tested on MacOS. | |
# Prereqs: awscli, jq | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | |
TMPTYPES=/tmp/types | |
PREFERREDTYPES=${DIR}/types.txt | |
REGION=$AWS_REGION | |
ACCOUNTID=$(aws sts get-caller-identity --query Account --out text) | |
[[ -z ${ACCOUNTID} ]] && { echo "Could not determine account ID"; exit 1; } | |
OUTPUT=${DIR}/supported_instance_types_${ACCOUNTID}_${REGION}.txt | |
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') | |
echo -e "${TIMESTAMP}" > ${OUTPUT} | |
get_types(){ | |
aws ec2 describe-instance-type-offerings \ | |
--location-type availability-zone \ | |
--filters Name=instance-type,Values=${type} \ | |
--region ${REGION} \ | |
--output text \ | |
> ${TMPTYPES}.${type}.txt | |
} | |
check_types(){ | |
if [[ $(wc -l ${TMPTYPES}.${type}.txt | awk '{print $1}') == 0 ]] | |
then | |
echo -e "\n${type} not supported in ANY AZ" | tee -a ${OUTPUT} | |
elif [[ $(wc -l ${TMPTYPES}.${type}.txt | awk '{print $1}') < 3 ]] | |
then | |
echo -e "\n${type} only available in these AZs:" | tee -a ${OUTPUT} | |
for l in $(cat ${TMPTYPES}.${type}.txt | awk '{print $3}') | |
do echo " ${l}" | tee -a ${OUTPUT} | |
done | |
fi | |
} | |
prep_types(){ | |
if [[ -f ${PREFERREDTYPES} ]] | |
then | |
echo -e "\nProcessing instance types specified in ${PREFERREDTYPES} ..." | tee -a ${OUTPUT} | |
cat ${PREFERREDTYPES} > ${TMPTYPES} | |
else | |
echo -e "\nProcessing all available instance types ..." | tee -a ${OUTPUT} | |
>${TMPTYPES} | |
for t in $(aws ec2 describe-instance-types \ | |
--query 'InstanceTypes[].InstanceType' \ | |
--out text); do echo ${t} >> ${TMPTYPES}; done | |
fi | |
} | |
get_az_ids(){ | |
echo -e "\nAvailability Zone Name to ID mapping:\n" | tee -a ${OUTPUT} | |
aws ec2 describe-availability-zones \ | |
| jq '[.AvailabilityZones[] | {"ZoneName": .ZoneName, "ZoneId":.ZoneId}]' | tee -a ${OUTPUT} | |
} | |
prep_types | |
LISTTYPES=$(cat ${TMPTYPES} | sort) | |
for type in ${LISTTYPES}; | |
do | |
get_types && check_types | |
rm -f ${TMPTYPES}.${type}.txt | |
done | |
get_az_ids | |
echo -e "\nOutput: ${OUTPUT}\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment