AWS Pricing API不会根据给定的搜索条件产生价格

时间:2018-07-26 22:02:46

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

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

但是我没有得到组合的结果(美国西部2,r3.2x大,Linux,未安装预软件,租户=共享)

这是我的代码:

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

1 个答案:

答案 0 :(得分:2)

这有效:

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"
        }'
    ]
}