Created
August 2, 2012 16:13
-
-
Save srs81/3238282 to your computer and use it in GitHub Desktop.
Find cumulative+average cost for EC2 spot instances at time ranges
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
import boto, datetime, time | |
# Enter your AWS credentials | |
aws_key = "YOUR_AWS_KEY" | |
aws_secret = "YOUR_AWS_SECRET" | |
# Details of instance & time range you want to find spot prices for | |
instanceType = 'm1.xlarge' | |
startTime = '2012-07-01T21:14:45.000Z' | |
endTime = '2012-07-30T23:14:45.000Z' | |
aZ = 'us-east-1c' | |
# Some other variables | |
maxCost = 0.0 | |
minTime = float("inf") | |
maxTime = 0.0 | |
totalPrice = 0.0 | |
oldTimee = 0.0 | |
# Connect to EC2 | |
conn = boto.connect_ec2(aws_key, aws_secret) | |
# Get prices for instance, AZ and time range | |
prices = conn.get_spot_price_history(instance_type=instanceType, | |
start_time=startTime, end_time=endTime, availability_zone=aZ) | |
# Output the prices | |
print "Historic prices" | |
for price in prices: | |
timee = time.mktime(datetime.datetime.strptime(price.timestamp, | |
"%Y-%m-%dT%H:%M:%S.000Z" ).timetuple()) | |
print "\t" + price.timestamp + " => " + str(price.price) | |
# Get max and min time from results | |
if timee < minTime: | |
minTime = timee | |
if timee > maxTime: | |
maxTime = timee | |
# Get the max cost | |
if price.price > maxCost: | |
maxCost = price.price | |
# Calculate total price | |
if not (oldTimee == 0): | |
totalPrice += (price.price * abs(timee - oldTimee)) / 3600 | |
oldTimee = timee | |
# Difference b/w first and last returned times | |
timeDiff = maxTime - minTime | |
# Output aggregate, average and max results | |
print "For: one %s in %s" % (instanceType, aZ) | |
print "From: %s to %s" % (startTime, endTime) | |
print "\tTotal cost = $" + str(totalPrice) | |
print "\tMax hourly cost = $" + str(maxCost) | |
print "\tAvg hourly cost = $" + str(totalPrice * 3600/ timeDiff) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment