如何使用boto查看EMR现货实例历史价格

How to check EMR spot instance price history with boto

我想使用现货定价以编程方式创建 EMR 集群以节省一些成本。为此,我正在尝试使用 boto3 从 AWS 检索 EMR 现货实例定价,但我从 Boto3 知道的唯一 API 可用的是使用 ec2 客户端的 decribe_spot_price_history 调用 - https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_spot_price_history

EC2 的价格不是 指示 EMR 的定价,如此处所示 - https://aws.amazon.com/emr/pricing/。这些值几乎是 EMR 的两倍。

有什么方法可以让我看到类似于 EC2 的 EMR 的现货价格历史记录?我检查了 https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/emr.html 和 AWS 在线文档的其他几页,但没有发现任何内容。

这是我用来检查可用于对 EMR 实例出价的大概定价的代码片段。

max_bid_price = 0.140
min_bid_price = max_bid_price
az_choice = ''
response = ec2.describe_spot_price_history(
    Filters=[{
        'Name': 'availability-zone',
        'Values': ['us-east-1a', 'us-east-1c', 'us-east-1d']
        },
        {
            'Name': 'product-description',
            'Values': ['Linux/UNIX (Amazon VPC)']
        }],
    InstanceTypes=['r5.2xlarge'],
    EndTime=datetime.now(),
    StartTime=datetime.now()
)
# TODO: Add more Subnets in other AZ's if picking from our existing 3 is an issue
# 'us-east-1b', 'us-east-1e', 'us-east-1f'
for spot_price_history in response['SpotPriceHistory']:
    print(spot_price_history)
    if float(spot_price_history['SpotPrice']) <= min_bid_price:
        min_bid_price = float(spot_price_history['SpotPrice'])
        az_choice = spot_price_history['AvailabilityZone']

上述方法失败了,因为 EC2 现货实例的价格略高于亚马逊对 EMR 按需实例按正常小时收费的价格。 (例如,这种规模的集群的按需成本仅为 0.126 美元/小时,但 EC2 的按需成本为 0.504 美元/小时,现货实例的价格为 about 0.20 美元/小时)。

评论中已经提到,没有所谓的 EMR 现货定价。现货定价适用于 EC2 实例。您可以查看 this AWS spot advisor 页面,找出哪些实例类别的中断率较低,并据此进行选择。

自 2017 年以来,AWS 更改了现货定价算法,“价格根据供需的长期趋势逐渐调整”,因此您可能不需要看历史现货价格。有关详细信息,请参见 here

如今,使用该实例的最后价格 (+ delta) 很可能没问题。这可以使用以下代码片段实现:

def get_bid_price(instancetype, aws_region):
    instance_types = [instancetype]
    start = datetime.now() - timedelta(days=1)

    ec2_client = boto3.client('ec2', aws_region)
    price_dict = ec2_client.describe_spot_price_history(StartTime=start,
                                                        InstanceTypes=instance_types,
                                                        ProductDescriptions=['Linux/UNIX (Amazon VPC)']
                                                        )
    if len(price_dict.get('SpotPriceHistory')) > 0:
        PriceHistory = namedtuple('PriceHistory', 'price timestamp')
        price_list = [PriceHistory(round(float(item.get('SpotPrice')), 3), item.get('Timestamp'))
                      for item in price_dict.get('SpotPriceHistory')]
        price_list.sort(key=lambda tup: tup.timestamp, reverse=True)

        # Maybe add 10 cents to the last spot price
        bid_price = round(float(price_list[0][0] + .01), 3)
        return bid_price
    else:
        raise ValueError('Invalid instance type: {} provided. '
                         'Please provide correct instance type.'.format(instancetype))