Skip to content

Instantly share code, notes, and snippets.

@mtulio
Created August 24, 2018 06:23
Show Gist options
  • Save mtulio/1066c3ca6d400899ef905f274bede119 to your computer and use it in GitHub Desktop.
Save mtulio/1066c3ca6d400899ef905f274bede119 to your computer and use it in GitHub Desktop.
AWS CLI - Check the cheaper spot on an given Region and Instance Type and return the AZ name
#!/bin/bash
# Return cheapper AZ for an given instance type and region
# Eg: $0 us-east-1 r3.8xlarge [all]
# AWS Region [Eg. us-east-1 ]
R=$1
# AWS Instance Type [Eg. r3.8xlarge ]
I=$2
FILTER=|jq '.|=sort_by(.price)'[0].az
test -z $R && ( echo "Region not found."; exit 1)
test -z $I && ( echo "Instance type not found."; exit 1)
RES=$(aws --region=$R \
ec2 describe-spot-price-history --instance-types $I \
--start-time=$(date +%s) \
--product-descriptions="Linux/UNIX" \
--query 'SpotPriceHistory[*].{az:AvailabilityZone, price:SpotPrice}')
if [[ "$3" == 'all' ]]; then
echo $RES |jq '.|=sort_by(.az)'
exit 0
fi
echo $RES |jq '.|=sort_by(.price)'[0].az
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment