【问题标题】:AWS Pricing API not yielding prices for the given search criteriaAWS 定价 API 没有为给定的搜索条件生成价格
【发布时间】:2019-01-03 23:57:10
【问题描述】:

我正在使用 AWS boto3 定价 api 来获取实例的价格。

但我没有得到组合的结果(us west 2,r3.2x large,Linux,未安装预软件,tenancy =shared)

这是我的代码:

pricing = boto3.client('pricing', region_name='us-east-1')
hourlyTermCode = 'JRTCKXETXF'
rateCode = '6YS6EN2CT7'
token = ''
while True:
    paginator = pricing.get_paginator('get_products')
    pages = paginator.paginate(
        ServiceCode='AmazonEC2',
        Filters=[
            {'Type': 'TERM_MATCH', 'Field': 'operatingSystem', 'Value': 'Linux'},
            {'Type': 'TERM_MATCH', 'Field': 'location', 'Value': 'US West (Oregon)'}

        ],

        PaginationConfig={
            'StartingToken':token
        }
    )

    for response in pages:
        for price in response['PriceList']:
            resp = json.loads(price)
            product = resp['product']  # ['attributes']['']
            sku = product['sku']

            if product['productFamily'] == 'Compute Instance':
                if str(product['attributes']['instanceType']) == str(amazon_instance_type) :
                    if str(product['attributes']['operatingSystem']) == 'Linux':
                        if str(product['attributes']['preInstalledSw']) == 'NA':
                            if str(product['attributes']['tenancy']) == 'Shared':
                                sku_key = resp['terms']['OnDemand'].get(sku)
                                if sku_key:
                                    price = sku_key[sku + '.' + hourlyTermCode + '.' + rateCode]['pricePerUnit']['USD']
                                    print 'here 7'
                                    print price

        try:
            token = response['NextToken']
        except KeyError:
            pass

【问题讨论】:

  • 我建议你采取让代码先返回something的策略。减少限制。添加一些“else”分支来打印匹配的值,以供检查。打印出整个响应或价格并进行目视检查。您可能正在使用不适用于 us-west-2 的每小时期限代码和/或费率代码。
  • 嗨@Michael-sqlbot:是的,我试过了,对于我在报价文件中找到的匹配密钥,api 以某种方式给出了匹配 sku 的密钥错误。

标签: python-2.7 amazon-web-services amazon-ec2 pagination boto3


【解决方案1】:

这行得通:

import json
import boto3

client = boto3.client('pricing', region_name='us-east-1')

response = client.get_products(
    ServiceCode='AmazonEC2',
    Filters=[
        {'Type': 'TERM_MATCH', 'Field': 'operatingSystem', 'Value': 'Linux'},
        {'Type': 'TERM_MATCH', 'Field': 'location', 'Value': 'US West (Oregon)'},
        {'Type': 'TERM_MATCH', 'Field': 'instanceType', 'Value': 'r3.2xlarge'},
        {'Type': 'TERM_MATCH', 'Field': 'tenancy', 'Value': 'Shared'},
        {'Type': 'TERM_MATCH', 'Field': 'preInstalledSw', 'Value': 'NA'}
    ]
)

for pricelist_json in response['PriceList']:
    pricelist = json.loads(pricelist_json)
    product = pricelist['product']

    if product['productFamily'] == 'Compute Instance':
        print pricelist['terms']['OnDemand'].values()[0]['priceDimensions'].values()[0][u'pricePerUnit']['USD']

它基于以下输出:

{u'FormatVersion': u'aws_v1', u'PriceList': [u'{
            "product": {
                "productFamily": "Compute Instance",
                "attributes": {
                    "enhancedNetworkingSupported": "Yes",
                    "memory": "61 GiB",
                    "vcpu": "8",
                    "capacitystatus": "Used",
                    "locationType": "AWS Region",
                    "storage": "1 x 160 SSD",
                    "instanceFamily": "Memory optimized",
                    "operatingSystem": "Linux",
                    "physicalProcessor": "Intel Xeon E5-2670 v2 (Ivy Bridge)",
                    "clockSpeed": "2.5 GHz",
                    "ecu": "26",
                    "networkPerformance": "High",
                    "servicename": "Amazon Elastic Compute Cloud",
                    "instanceType": "r3.2xlarge",
                    "tenancy": "Shared",
                    "usagetype": "USW2-BoxUsage:r3.2xlarge",
                    "normalizationSizeFactor": "16",
                    "processorFeatures": "Intel AVX; Intel Turbo",
                    "servicecode": "AmazonEC2",
                    "licenseModel": "No License required",
                    "currentGeneration": "No",
                    "preInstalledSw": "NA",
                    "location": "US West (Oregon)",
                    "processorArchitecture": "64-bit",
                    "operation": "RunInstances"
                },
                "sku": "GMTWE5CTY4FEUYDN"
            },
            "serviceCode": "AmazonEC2",
            "terms": {
                "OnDemand": {
                    "GMTWE5CTY4FEUYDN.JRTCKXETXF": {
                        "priceDimensions": {
                            "GMTWE5CTY4FEUYDN.JRTCKXETXF.6YS6EN2CT7": {
                                "unit": "Hrs",
                                "endRange": "Inf",
                                "description": "$0.665 per On Demand Linux r3.2xlarge Instance Hour",
                                "appliesTo": [],
                                "rateCode": "GMTWE5CTY4FEUYDN.JRTCKXETXF.6YS6EN2CT7",
                                "beginRange": "0",
                                "pricePerUnit": {
                                    "USD": "0.6650000000"
                                }
                            }
                        },
                        "sku": "GMTWE5CTY4FEUYDN",
                        "effectiveDate": "2018-07-01T00:00:00Z",
                        "offerTermCode": "JRTCKXETXF",
                        "termAttributes": {}
                    }
                },
                ...
            },
            "version": "20180726190848",
            "publicationDate": "2018-07-26T19:08:48Z"
        }'
    ]
}

【讨论】:

  • 非常感谢。该解决方案非常完美,消除了对 sku 或报价代码或费率代码的依赖。但是,为什么我们不需要分页呢?它的用例是什么?为什么它不适用于这种情况?
  • 附加过滤器意味着返回的结果很少(一个?)。因此,不需要分页。如果您的过滤器较少,则会返回更多结果,因此可能需要分页。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-29
  • 1970-01-01
  • 2015-12-14
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
  • 2022-07-14
相关资源
最近更新 更多