【问题标题】:How to check EMR spot instance price history with boto如何使用 boto 查看 EMR 现货实例价格历史记录
【发布时间】:2020-01-18 11:10:18
【问题描述】:

我想使用现货定价以编程方式创建 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 在线的其他几页文档,但一无所获。

这是一个代码 sn-p,我用它来检查可用于对 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 现货实例的价格略高于 Amazon 对 EMR 按需实例的正常小时费用收取的费用。 (例如,这种规模的集群按需仅需 0.126 美元/小时,但 EC2 按需为 0.504 美元/小时,而 Spot 实例的价格 0.20 美元/小时)。

【问题讨论】:

  • 当您使用 EMR 时,它将按 EMR 价格 + 每个 EC2 价格收费。 EMR 没有现货价格,您可以获取每个节点的现货价格,然后添加 EMR 按需价格。这就是总价。

标签: python-3.x amazon-web-services boto3 amazon-emr


【解决方案1】:

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

自 2017 年以来,AWS 改变了现货定价算法,“价格根据长期供需趋势逐步调整”,因此您可能无需查看按历史现货价格计算。更多详情请见here

现在,使用该实例的最后价格(+ delta)很可能会很好。这可以使用以下代码sn-p来实现:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 2022-10-19
    • 2012-09-17
    • 1970-01-01
    • 2016-07-04
    相关资源
    最近更新 更多