7

私はよく EC2 でスポット インスタンスを実行します (Hadoop タスク ジョブ、一時ノードなど)。これらのいくつかは、長時間実行されるスポット インスタンスです。

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

スポット インスタンスのコストが市場レートに応じて 1 時間ごとに変化することは承知しています。実行中のスポット インスタンスの累積合計コストを計算する方法はありますか? API経由か、それ以外か?

4

4 に答える 4

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)
于 2012-08-02T21:51:58.217 に答える
4

boto3で動作するSumanのソリューションを書き直しました。必ず tz セットで utctime を使用してください!:

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
于 2015-10-08T02:52:40.330 に答える
3

スポット インスタンス データ フィードをサブスクライブして、S3 バケットにダンプされた実行中のインスタンスの料金を取得できます。ec2 ツールセットをインストールしてから、次を実行します。

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

注: アカウント全体で 1 つのデータ フィード サブスクリプションのみを持つことができます。

約 1 時間で、次のような 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
于 2013-05-20T17:14:37.173 に答える
1

私は最近、単一の EMR クラスターのコスト、またはクラスターのリスト (指定された日数) を計算する小さな Python ライブラリを開発しました。

スポット インスタンスとタスク ノードも考慮されます (クラスターの実行中にアップとダウンする可能性があります)。

コストを計算するために入札価格を使用しますが、これは (多くの場合) インスタンスに対して最終的に支払う正確な価格ではない可能性があります。ただし、入札ポリシーによっては、この価格が十分に正確な場合があります。

コードはこちらにあります: https://github.com/memosstilvi/emr-cost-calculator

于 2015-05-27T09:59:56.697 に答える