计算EC2现货实例的运行/累计成本

Sum*_*man 7 amazon-ec2

我经常在EC2上运行现场实例(用于Hadoop任务作业,临时节点等)其中一些是长期运行的现场实例.

它很容易计算按需或保留的EC2实例的成本 - 但是如何计算作为专色实例运行的特定节点(或节点)的成本?

我知道现场实例的成本每小时都会根据市场价格而变化 - 那么有没有办法计算运行现货实例的累计总成本?通过API或其他方式?

Sum*_*man 6

好的,我在Boto库中找到了一种方法.这段代码并不完美 - Boto似乎没有返回确切的时间范围,但它确实在一定范围内或多或少地获得了历史现货价格.以下代码似乎运行良好.如果有人可以改进它,那将是伟大的.

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)
Run Code Online (Sandbox Code Playgroud)


amo*_*ohr 4

我重写了 Suman 的解决方案以与 boto3 一起使用。确保将 utctime 与 tz 集一起使用!:

def get_spot_instance_pricing(ec2, instance_type, start_time, end_time, zone):
    result = ec2.describe_spot_price_history(InstanceTypes=[instance_type], StartTime=start_time, EndTime=end_time, AvailabilityZone=zone)
    assert 'NextToken' not in result or result['NextToken'] == ''

    total_cost = 0.0

    total_seconds = (end_time - start_time).total_seconds()
    total_hours = total_seconds / (60*60)
    computed_seconds = 0

    last_time = end_time
    for price in result["SpotPriceHistory"]:
        price["SpotPrice"] = float(price["SpotPrice"])

        available_seconds = (last_time - price["Timestamp"]).total_seconds()
        remaining_seconds = total_seconds - computed_seconds
        used_seconds = min(available_seconds, remaining_seconds)

        total_cost += (price["SpotPrice"] / (60 * 60)) * used_seconds
        computed_seconds += used_seconds

        last_time = price["Timestamp"]

    # Difference b/w first and last returned times
    avg_hourly_cost = total_cost / total_hours
    return avg_hourly_cost, total_cost, total_hours
Run Code Online (Sandbox Code Playgroud)