Last active
August 12, 2016 20:41
-
-
Save mrchristine/23e81c0fe30bcc6c1275a27925986afe to your computer and use it in GitHub Desktop.
AWS Spot Pricing History
This file contains hidden or 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
| #!/bin/bash | |
| # catch ctrl+c handler | |
| trap ctrl_c_cleanup INT | |
| function ctrl_c_cleanup() { | |
| echo "** Interrupt handler caught" | |
| rm -rf spot_prices_*.json | |
| } | |
| function usage() { | |
| # us-west-1 us-west-2 us-east-1 eu-central-1 eu-west-1 | |
| REGIONS=`aws ec2 describe-regions | jq '.Regions[] | .RegionName'` | |
| echo "Use the -k option to keep the files for further processing" | |
| echo -e "Please select a region below" | |
| echo -e "Region Listing:\n$REGIONS" | |
| } | |
| # Default region is us-west-2 | |
| REGION="us-west-2" | |
| # Format is YEAR-MONTH-DAY | |
| START_DATE=`date -d '7 days ago' +%Y-%m-%d` | |
| END_DATE=`date +%Y-%m-%d` | |
| while getopts "hns:e:r:" opt; do | |
| case $opt in | |
| s) | |
| START_DATE=${OPTARG} | |
| ;; | |
| e) | |
| END_DATE=${OPTARG} | |
| ;; | |
| r) | |
| REGION=${OPTARG} | |
| ;; | |
| n) | |
| now=true | |
| ;; | |
| k) | |
| keep=true | |
| ;; | |
| h | *) | |
| usage | |
| exit | |
| ;; | |
| \?) | |
| echo -e "Running EC2 Spot Instance History\n" | |
| ;; | |
| esac | |
| done | |
| shift $((OPTIND-1)) | |
| if [[ $now ]] ; then | |
| START_DATE=`date -d '-2 hours' -u +"%Y-%m-%dT%H:%M:%SZ"` | |
| END_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"` | |
| fi | |
| # Find the list of availability zones | |
| ZONES=`aws ec2 describe-availability-zones --region $REGION | jq '.AvailabilityZones[] | .ZoneName'` | |
| for z in $ZONES | |
| do | |
| # Remove quotes from string using bash inline string operations | |
| z=${z//\"} | |
| echo "Running describe spot history for ${z}" | |
| aws ec2 describe-spot-price-history --region $REGION \ | |
| --availability-zone $z \ | |
| --instance-types r3.2xlarge \ | |
| --product-description "Linux/UNIX (Amazon VPC)" \ | |
| --start-time $START_DATE \ | |
| --end-time $END_DATE | jq -c .SpotPriceHistory[] > spot_prices_$START_DATE_$END_DATE_$z.json | |
| done | |
| # Add average cost / week | |
| for f in `ls spot_prices*` | |
| do | |
| echo -e "File Region: $f" | |
| echo -e "Avg spot price over date range: " | |
| cat $f | jq .SpotPrice | tr -d "\"" | python -c "import sys; sl = [float(l) for l in sys.stdin]; print sum(sl)/len(sl)" | |
| echo -e "Max Spot Price over date range: " | |
| cat $f | jq .SpotPrice | tr -d "\"" | sort -n -r | head -n 1 | |
| head -n 2 $f | |
| echo | |
| done | |
| if [[ ! $keep ]] ; then | |
| rm -rf spot_prices_*.json | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment