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)
4 回答
我重写了Suman与boto3合作的解决方案 . 确保在tz设置中使用utctime!:
好的,我在Boto库中找到了一种方法 . 这段代码并不完美 - Boto似乎没有返回确切的时间范围,但它确实在一定范围内或多或少地获得了历史现货价格 . 以下代码似乎运行良好 . 如果有人可以改进它,那将是伟大的 .
您可以订阅spot实例数据源,以便为转储到S3存储桶的正在运行的实例收取费用 . 安装ec2工具集,然后运行:
注意:您的整个帐户只能有一个数据Feed订阅 .
在大约一个小时内,你应该开始看到gzipped标签分隔文件显示在桶中,看起来像这样:
我最近开发了一个小型python库,用于计算单个EMR集群或集群列表的成本(给定一段时间) .
它还考虑了Spot实例和Task节点(在集群仍在运行时可能会上下移动) .
为了计算成本,我使用了出价,这在很多情况下可能不是您最终为实例支付的确切价格 . 但是,根据您的出价政策,此价格可以足够准确 .
你可以在这里找到代码:https://github.com/memosstilvi/emr-cost-calculator