简体   繁体   English

Boto3 get_products 定价为某些实例类型返回 0.00 美元

[英]Boto3 get_products pricing returning 0.00 USD for some instance types

I'm trying to fetch GPU instance prices for the EU (Ireland) region (eu-west-1).我正在尝试获取欧盟(爱尔兰)区域 (eu-west-1) 的 GPU 实例价格。 However, some of the instance types have price 0.00, even though the website ( https://aws.amazon.com/ec2/pricing/on-demand/ ) shows them as available and definitely not 0.00.但是,一些实例类型的价格为 0.00,即使网站 ( https://aws.amazon.com/ec2/pricing/on-demand/ ) 显示它们是可用的并且绝对不是 0.00。

I'm using the code below:我正在使用下面的代码:

session = boto3.Session(profile_name='nimbo')
instance_types = list(sorted(ec2_instance_types('eu-west-1')))
# Filter by p and g instance types (gpus)
instance_types = [inst for inst in instance_types if inst[0] in ["p", "g"]]

# Use AWS Pricing API at US-East-1
pricing = session.client('pricing', region_name='us-east-1')


for instance_type in instance_types:
    response = pricing.get_products(
        ServiceCode='AmazonEC2',
        MaxResults=1,
        FormatVersion='aws_v1',
        Filters=[
            { 'Type': 'TERM_MATCH', 'Field': 'instanceType', 'Value': instance_type },
            { 'Type': 'TERM_MATCH', 'Field': 'location', 'Value': 'EU (Ireland)' },
            { 'Type': 'TERM_MATCH', 'Field': 'operatingSystem', 'Value': 'Linux' },
        ]
    )

    inst = json.loads(response["PriceList"][0])
    inst = inst['terms']['OnDemand']
    inst = list(inst.values())[0]
    inst = list(inst["priceDimensions"].values())[0]
    inst = inst['pricePerUnit']
    currency = list(inst.keys())[0]
    price = float(inst[currency])
    print(instance_type+": "+"%0.3f %s"%(price, currency))

This returns:这将返回:

g2.2xlarge: 0.907 USD
g2.8xlarge: 0.000 USD
g3.16xlarge: 0.000 USD
g3.4xlarge: 1.331 USD
g3.8xlarge: 2.420 USD
g3s.xlarge: 0.796 USD
g4ad.16xlarge: 0.000 USD
g4ad.4xlarge: 0.000 USD
g4ad.8xlarge: 2.130 USD
g4dn.12xlarge: 4.645 USD
g4dn.16xlarge: 0.000 USD
g4dn.2xlarge: 0.838 USD
g4dn.4xlarge: 1.342 USD
g4dn.8xlarge: 0.000 USD
g4dn.metal: 8.724 USD
g4dn.xlarge: 0.625 USD
p2.16xlarge: 0.000 USD
p2.8xlarge: 7.776 USD
p2.xlarge: 0.000 USD
p3.16xlarge: 0.000 USD
p3.2xlarge: 3.305 USD
p3.8xlarge: 14.082 USD
p3dn.24xlarge: 0.000 USD
p4d.24xlarge: 0.000 USD

Any one has any idea what I'm missing here?有人知道我在这里缺少什么吗?

There are many dimensions to pricing of an Amazon EC2 instance. Amazon EC2 实例的定价有很多方面。

Your code is only looking at the first element returned ( MaxResults=1 ), out of a result set of over 100 records.您的代码仅查看包含 100 多条记录的结果集中返回的第一个元素 ( MaxResults=1 )。

When I ran your code, it was displaying:当我运行您的代码时,它显示:

  • $0.907 per Dedicated Unused Reservation Linux with SQL Web g2.2xlarge Instance Hour
  • $0.00 per Reservation Linux g2.8xlarge Instance Hour

I presume you want On-Demand pricing without any Reservations in place.我假设您希望按需定价而无需任何预订。 In this case, you would need additional filters:在这种情况下,您将需要额外的过滤器:

        { 'Type': 'TERM_MATCH', 'Field': 'capacitystatus', 'Value': 'Used' },
        { 'Type': 'TERM_MATCH', 'Field': 'preInstalledSw', 'Value': 'NA' },
        { 'Type': 'TERM_MATCH', 'Field': 'tenancy', 'Value': 'shared' },

(Thanks to Use boto3 to get current price for given EC2 instance type for this configuration!) (感谢使用 boto3 获取此配置的给定 EC2 实例类型的当前价格!)

The result is then:结果是:

g2.2xlarge: 0.702 USD
g2.8xlarge: 2.808 USD

I suggest that you remove the MaxResults setting so that you can see when more results are returned.我建议您删除MaxResults设置,以便您可以查看何时返回更多结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM