【问题标题】:How to get the list of instances for AWS EMR?如何获取 AWS EMR 的实例列表?
【发布时间】:2018-05-20 19:59:28
【问题描述】:

为什么 EC2 的列表与 EMR 列表不同?

EC2:https://aws.amazon.com/ec2/spot/pricing/

电子病历:https://aws.amazon.com/emr/pricing/

为什么并非所有类型的 EC2 实例都可用于 EMR?如何获得这份特别名单?

【问题讨论】:

  • 您的问题是什么?是如何获取在给定 EMR 集群中运行的 EC2 实例列表,还是 EMR 定价与 EC2 定价不同的原因?
  • ...或者是如何获取与 EMR 一起使用的实例类型列表?请编辑您的问题以阐明您在寻找什么。

标签: amazon-web-services amazon-ec2 boto amazon-emr


【解决方案1】:

如果您的问题与亚马逊控制台无关
(那么它肯定会被关闭为off-topic):

作为一种编程解决方案,您看起来像这样:(使用 python boto3

import boto3
client = boto3.client('emr')
for instance in client.list_instances():
  print("Instance[%s] %s"%(instance.id, instance.name))

【讨论】:

  • 创建集群后使用listInstances()。在创建集群之前,我需要一个 InstanceType 列表。
【解决方案2】:

这是我使用的,虽然我不能 100% 确定它是准确的(因为我找不到支持我的某些选择的文档(-BoxUsage 等))。

值得查看来自 AWS 的响应,以了解定价客户端响应中不同字段的不同值。

使用以下获取响应列表:

   default_profile = boto3.session.Session(profile_name='default')
    # Only us-east-1 has the pricing API
    # - https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/pricing.html
    pricing_client = default_profile.client('pricing', region_name='us-east-1')
    service_name = 'ElasticMapReduce'
    product_filters = [
        {'Type': 'TERM_MATCH', 'Field': 'location', 'Value': aws_region_name}
    ]
    response = pricing_client.get_products(
        ServiceCode=service_name,
        Filters=product_filters,
        MaxResults=100
    )
    response_list.append(response)

    num_prices = 100
    while 'NextToken' in response:
       # re-query to get next page

获得响应列表后,您可以过滤掉实际的实例信息:

  emr_prices = {}
  for response in response_list:
      for price_info_str in response['PriceList']:
          price_obj = json.loads(price_info_str)
          attributes = price_obj['product']['attributes']

          # Skip pricing info that doesn't specify a (EC2) instance type
          if 'instanceType' not in attributes:
              continue
          inst_type = attributes['instanceType']

          # AFAIK, Only usagetype attributes that contain the string '-BoxUsage' are the ones that contain the prices that we would use (empirical research)
          # Other examples of values are <REGION-CODE>-M3BoxUsage, <REGION-CODE>-M5BoxUsage, <REGION-CODE>-M7BoxUsage (no clue what that means.. )
          if '-BoxUsage' not in attributes['usagetype']:
              continue

          if 'OnDemand' not in price_obj['terms']:
              continue

          on_demand_info = price_obj['terms']['OnDemand']
          price_dim = list(list(on_demand_info.values())[0]['priceDimensions'].values())[0]
          emr_price = Decimal(price_dim['pricePerUnit']['USD'])

          emr_prices[inst_type] = emr_price

实际上,从 boto3 文档中弄清楚这一点很简单。特别是get_products 文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-30
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多