2012-08-02 70 views
6

我經常在EC2上運行現貨實例(用於Hadoop任務作業,臨時節點等)。其中一些是長期運行的現貨實例。計算EC2現貨實例的運行/累積成本

它很容易計算按需或保留的EC2實例的成本 - 但是,如何計算作爲專有實例運行的特定節點(或多個節點)所產生的成本?

我知道現貨實例的成本會根據市場價格每小時變化 - 那麼是否有任何方法可以計算正在運行的現貨實例的累計總成本?通過API或其他方式?

回答

4

我已經重新編寫網速慢的解決方案與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 
6

好的我在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) 
3

您可以訂閱現場實例數據饋送,以獲得轉儲到S3存儲桶的正在運行的實例的費用。安裝EC2工具集,然後運行:

ec2-create-spot-datafeed-subscription -b bucket-to-dump-in 

注意:你只能有一個數據饋給訂閱整個帳戶。

在你應該開始看到gzip壓縮選項卡分隔的文件顯示在鬥一小時才得知這個樣子:

#Version: 1.0 
#Fields: Timestamp UsageType Operation InstanceID MyBidID MyMaxPrice MarketPrice Charge Version 
2013-05-20 14:21:07 UTC SpotUsage:m1.xlarge RunInstances:S0012 i-1870f27d sir-b398b235 0.219 USD 0.052 USD 0.052 USD 1 
1

我最近開發了計算單個EMR的成本小Python庫羣集或羣集列表(給定一段時間)。

它還考慮了競價型實例和任務節點(可能會在羣集仍在運行時上下移動)。

爲了計算我使用的出價,其中(很多情況下)可能不是您最終爲實例支付的確切價格。 但是,根據您的出價政策,此價格可能足夠準確。

您可以在這裏找到代碼:https://github.com/memosstilvi/emr-cost-calculator