2012-08-02 13 views
6

EC2上でスポットインスタンスを頻繁に実行します(Hadoopタスクジョブ、テンポラリノードなど)。これらは長時間実行されるスポットインスタンスです。EC2スポットインスタンスの稼働/累積コストの計算

オンデマンドまたは予約EC2インスタンスのコストを計算するのはかなり簡単ですが、スポットインスタンスとして実行されている特定のノード(またはノード)のコストをどのように計算すればよいですか?

私は、スポットインスタンスのコストが市場レートに応じて1時間ごとに変化することを認識しています。実行中のスポットインスタンスの累積合計コストを計算する方法はありますか? APIなどを介して?

答えて

4

。 uttimeは必ずtzset!を使用してください:

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

OK 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) 
3

スポットインスタンスのデータフィードを購読して、S3バケットにダンプした実行中のインスタンスの料金を得ることができます。 EC2のツールセットをインストールしてから実行します。

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

注:アカウント全体に対して1つのデータフィードのサブスクリプションを持つことができます。私は最近、単一のEMRのコストを計算する小さなPythonライブラリを開発している

#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

:このような何かを見て、あなたがgzip圧縮されたタブ付き区切りファイルを見始めるバケツに表示されるはずです約1時間で

クラスタ、またはクラスタのリスト(日数が与えられています)。

スポットインスタンスとタスクノードも考慮されます(クラスタが稼動している間は上下に移動する可能性があります)。

コストを計算するために、私は入札価格を使用します。入札価格は、多くの場合、インスタンスに対して支払う最終価格とは異なる場合があります。 しかし、あなたの入札方針によっては、この価格は十分正確です。

あなたがここにコードを見つけることができます:https://github.com/memosstilvi/emr-cost-calculator私はboto3で動作するようにスーマンのソリューションを再書いた