Boto3 - 打印AWS实例类型定价

时间:2018-04-30 16:36:39

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

我正在尝试获得所有OnDemand /共享租赁/非RI实例的pricePerUnit:USD每小时费用;每种类型一个价格。以下代码是我目前的代码:

import boto3
import sys

client = boto3.client('pricing')    
response = client.get_products(
    ServiceCode='AmazonEC2',
    Filters=[
        {'Type': 'TERM_MATCH', 'Field': 'instanceType',     'Value': 'm4.xlarge'                    },
        {'Type' :'TERM_MATCH', 'Field':'operatingSystem',   'Value': 'Windows'                      },
        {'Type' :'TERM_MATCH', 'Field':'location',          'Value': 'US East (N. Virginia)'        },
        {'Type' :'TERM_MATCH', 'Field':'tenancy',           'Value': 'Shared'                       },
        {'Type' :'TERM_MATCH', 'Field':'rateCode',          'Value': 'Q49VKFFWC877GUFC.JRTCKXETXF.6YS6EN2CT7'   }
    ],
    FormatVersion='aws_v1'
)

我正在使用此代码块来检索美元价值:

for key, values in response.items():
    if key == 'PriceList':
        for x in values:
            print(float(x['USD']), 2)

不幸的是我收到了这个错误:

Traceback (most recent call last):
    File "C:\bin\AWSpricing.py", line 31, in <module>
    print(float(x['USD']), 2)
TypeError: string indices must be integers

感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:1)

您的代码没有意义。

响应的内容是多维的。您需要遍历树以进入相应的USD字段:

{u'FormatVersion': u'aws_v1', u'PriceList': [u'{
            "product": {
                "productFamily": "Compute Instance",
                "attributes": {
                    "enhancedNetworkingSupported": "Yes",
                    "memory": "16 GiB",
                    "dedicatedEbsThroughput": "750 Mbps",
                    "vcpu": "4",
                    "capacitystatus": "Used",
                    "locationType": "AWS Region",
                    "storage": "EBS only",
                    "instanceFamily": "General purpose",
                    "operatingSystem": "Windows",
                    "physicalProcessor": "Intel Xeon E5-2676 v3 (Haswell)",
                    "clockSpeed": "2.4  GHz",
                    "ecu": "13",
                    "networkPerformance": "High",
                    "servicename": "Amazon Elastic Compute Cloud",
                    "instanceType": "m4.xlarge",
                    "tenancy": "Shared",
                    "usagetype": "BoxUsage:m4.xlarge",
                    "normalizationSizeFactor": "8",
                    "processorFeatures": "Intel AVX; Intel AVX2; Intel Turbo",
                    "servicecode": "AmazonEC2",
                    "licenseModel": "No License required",
                    "currentGeneration": "Yes",
                    "preInstalledSw": "NA",
                    "location": "US East (N. Virginia)",
                    "processorArchitecture": "64-bit",
                    "operation": "RunInstances:0002"
                },
                "sku": "Q49VKFFWC877GUFC"
            },
            "serviceCode": "AmazonEC2",
            "terms": {
                "OnDemand": {
                    "Q49VKFFWC877GUFC.JRTCKXETXF": {
                        "priceDimensions": {
                            "Q49VKFFWC877GUFC.JRTCKXETXF.6YS6EN2CT7": {
                                "unit": "Hrs",
                                "endRange": "Inf",
                                "description": "$0.384 per On Demand Windows m4.xlarge Instance Hour",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.JRTCKXETXF.6YS6EN2CT7",
                                "beginRange": "0",
                                "pricePerUnit": {
                                    "USD": "0.3840000000"
                                }
                            }
                        },
                        "sku": "Q49VKFFWC877GUFC",
                        "effectiveDate": "2018-04-01T00:00:00Z",
                        "offerTermCode": "JRTCKXETXF",
                        "termAttributes": {}
                    }
                },
                "Reserved": {
                    "Q49VKFFWC877GUFC.4NA7Y494T4": {
                        "priceDimensions": {
                            "Q49VKFFWC877GUFC.4NA7Y494T4.6YS6EN2CT7": {
                                "unit": "Hrs",
                                "endRange": "Inf",
                                "description": "Windows (Amazon VPC), m4.xlarge reserved instance applied",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.4NA7Y494T4.6YS6EN2CT7",
                                "beginRange": "0",
                                "pricePerUnit": {
                                    "USD": "0.3079000000"
                                }
                            }
                        },
                        "sku": "Q49VKFFWC877GUFC",
                        "effectiveDate": "2017-04-30T23:59:59Z",
                        "offerTermCode": "4NA7Y494T4",
                        "termAttributes": {
                            "LeaseContractLength": "1yr",
                            "OfferingClass": "standard",
                            "PurchaseOption": "No Upfront"
                        }
                    },
                    "Q49VKFFWC877GUFC.HU7G6KETJZ": {
                        "priceDimensions": {
                            "Q49VKFFWC877GUFC.HU7G6KETJZ.2TG2D8R56U": {
                                "unit": "Quantity",
                                "description": "Upfront Fee",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.HU7G6KETJZ.2TG2D8R56U",
                                "pricePerUnit": {
                                    "USD": "1323"
                                }
                            },
                            "Q49VKFFWC877GUFC.HU7G6KETJZ.6YS6EN2CT7": {
                                "unit": "Hrs",
                                "endRange": "Inf",
                                "description": "Windows (Amazon VPC), m4.xlarge reserved instance applied",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.HU7G6KETJZ.6YS6EN2CT7",
                                "beginRange": "0",
                                "pricePerUnit": {
                                    "USD": "0.1510000000"
                                }
                            }
                        },
                        "sku": "Q49VKFFWC877GUFC",
                        "effectiveDate": "2017-04-30T23:59:59Z",
                        "offerTermCode": "HU7G6KETJZ",
                        "termAttributes": {
                            "LeaseContractLength": "1yr",
                            "OfferingClass": "standard",
                            "PurchaseOption": "Partial Upfront"
                        }
                    },
                    "Q49VKFFWC877GUFC.6QCMYABX3D": {
                        "priceDimensions": {
                            "Q49VKFFWC877GUFC.6QCMYABX3D.2TG2D8R56U": {
                                "unit": "Quantity",
                                "description": "Upfront Fee",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.6QCMYABX3D.2TG2D8R56U",
                                "pricePerUnit": {
                                    "USD": "2625"
                                }
                            },
                            "Q49VKFFWC877GUFC.6QCMYABX3D.6YS6EN2CT7": {
                                "unit": "Hrs",
                                "endRange": "Inf",
                                "description": "Windows (Amazon VPC), m4.xlarge reserved instance applied",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.6QCMYABX3D.6YS6EN2CT7",
                                "beginRange": "0",
                                "pricePerUnit": {
                                    "USD": "0.0000000000"
                                }
                            }
                        },
                        "sku": "Q49VKFFWC877GUFC",
                        "effectiveDate": "2017-04-30T23:59:59Z",
                        "offerTermCode": "6QCMYABX3D",
                        "termAttributes": {
                            "LeaseContractLength": "1yr",
                            "OfferingClass": "standard",
                            "PurchaseOption": "All Upfront"
                        }
                    },
                    "Q49VKFFWC877GUFC.Z2E3P23VKM": {
                        "priceDimensions": {
                            "Q49VKFFWC877GUFC.Z2E3P23VKM.6YS6EN2CT7": {
                                "unit": "Hrs",
                                "endRange": "Inf",
                                "description": "Windows (Amazon VPC), m4.xlarge reserved instance applied",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.Z2E3P23VKM.6YS6EN2CT7",
                                "beginRange": "0",
                                "pricePerUnit": {
                                    "USD": "0.2834000000"
                                }
                            }
                        },
                        "sku": "Q49VKFFWC877GUFC",
                        "effectiveDate": "2017-04-30T23:59:59Z",
                        "offerTermCode": "Z2E3P23VKM",
                        "termAttributes": {
                            "LeaseContractLength": "3yr",
                            "OfferingClass": "convertible",
                            "PurchaseOption": "No Upfront"
                        }
                    },
                    "Q49VKFFWC877GUFC.MZU6U2429S": {
                        "priceDimensions": {
                            "Q49VKFFWC877GUFC.MZU6U2429S.6YS6EN2CT7": {
                                "unit": "Hrs",
                                "endRange": "Inf",
                                "description": "Windows (Amazon VPC), m4.xlarge reserved instance applied",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.MZU6U2429S.6YS6EN2CT7",
                                "beginRange": "0",
                                "pricePerUnit": {
                                    "USD": "0.0000000000"
                                }
                            },
                            "Q49VKFFWC877GUFC.MZU6U2429S.2TG2D8R56U": {
                                "unit": "Quantity",
                                "description": "Upfront Fee",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.MZU6U2429S.2TG2D8R56U",
                                "pricePerUnit": {
                                    "USD": "7205"
                                }
                            }
                        },
                        "sku": "Q49VKFFWC877GUFC",
                        "effectiveDate": "2017-04-30T23:59:59Z",
                        "offerTermCode": "MZU6U2429S",
                        "termAttributes": {
                            "LeaseContractLength": "3yr",
                            "OfferingClass": "convertible",
                            "PurchaseOption": "All Upfront"
                        }
                    },
                    "Q49VKFFWC877GUFC.BPH4J8HBKS": {
                        "priceDimensions": {
                            "Q49VKFFWC877GUFC.BPH4J8HBKS.6YS6EN2CT7": {
                                "unit": "Hrs",
                                "endRange": "Inf",
                                "description": "Windows (Amazon VPC), m4.xlarge reserved instance applied",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.BPH4J8HBKS.6YS6EN2CT7",
                                "beginRange": "0",
                                "pricePerUnit": {
                                    "USD": "0.2704000000"
                                }
                            }
                        },
                        "sku": "Q49VKFFWC877GUFC",
                        "effectiveDate": "2017-04-30T23:59:59Z",
                        "offerTermCode": "BPH4J8HBKS",
                        "termAttributes": {
                            "LeaseContractLength": "3yr",
                            "OfferingClass": "standard",
                            "PurchaseOption": "No Upfront"
                        }
                    },
                    "Q49VKFFWC877GUFC.NQ3QZPMQV9": {
                        "priceDimensions": {
                            "Q49VKFFWC877GUFC.NQ3QZPMQV9.6YS6EN2CT7": {
                                "unit": "Hrs",
                                "endRange": "Inf",
                                "description": "Windows (Amazon VPC), m4.xlarge reserved instance applied",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.NQ3QZPMQV9.6YS6EN2CT7",
                                "beginRange": "0",
                                "pricePerUnit": {
                                    "USD": "0.0000000000"
                                }
                            },
                            "Q49VKFFWC877GUFC.NQ3QZPMQV9.2TG2D8R56U": {
                                "unit": "Quantity",
                                "description": "Upfront Fee",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.NQ3QZPMQV9.2TG2D8R56U",
                                "pricePerUnit": {
                                    "USD": "6430"
                                }
                            }
                        },
                        "sku": "Q49VKFFWC877GUFC",
                        "effectiveDate": "2017-04-30T23:59:59Z",
                        "offerTermCode": "NQ3QZPMQV9",
                        "termAttributes": {
                            "LeaseContractLength": "3yr",
                            "OfferingClass": "standard",
                            "PurchaseOption": "All Upfront"
                        }
                    },
                    "Q49VKFFWC877GUFC.VJWZNREJX2": {
                        "priceDimensions": {
                            "Q49VKFFWC877GUFC.VJWZNREJX2.2TG2D8R56U": {
                                "unit": "Quantity",
                                "description": "Upfront Fee",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.VJWZNREJX2.2TG2D8R56U",
                                "pricePerUnit": {
                                    "USD": "2777"
                                }
                            },
                            "Q49VKFFWC877GUFC.VJWZNREJX2.6YS6EN2CT7": {
                                "unit": "Hrs",
                                "endRange": "Inf",
                                "description": "Windows (Amazon VPC), m4.xlarge reserved instance applied",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.VJWZNREJX2.6YS6EN2CT7",
                                "beginRange": "0",
                                "pricePerUnit": {
                                    "USD": "0.0000000000"
                                }
                            }
                        },
                        "sku": "Q49VKFFWC877GUFC",
                        "effectiveDate": "2017-10-31T23:59:59Z",
                        "offerTermCode": "VJWZNREJX2",
                        "termAttributes": {
                            "LeaseContractLength": "1yr",
                            "OfferingClass": "convertible",
                            "PurchaseOption": "All Upfront"
                        }
                    },
                    "Q49VKFFWC877GUFC.38NPMPTW36": {
                        "priceDimensions": {
                            "Q49VKFFWC877GUFC.38NPMPTW36.2TG2D8R56U": {
                                "unit": "Quantity",
                                "description": "Upfront Fee",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.38NPMPTW36.2TG2D8R56U",
                                "pricePerUnit": {
                                    "USD": "3419"
                                }
                            },
                            "Q49VKFFWC877GUFC.38NPMPTW36.6YS6EN2CT7": {
                                "unit": "Hrs",
                                "endRange": "Inf",
                                "description": "Windows (Amazon VPC), m4.xlarge reserved instance applied",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.38NPMPTW36.6YS6EN2CT7",
                                "beginRange": "0",
                                "pricePerUnit": {
                                    "USD": "0.1300000000"
                                }
                            }
                        },
                        "sku": "Q49VKFFWC877GUFC",
                        "effectiveDate": "2016-11-30T23:59:59Z",
                        "offerTermCode": "38NPMPTW36",
                        "termAttributes": {
                            "LeaseContractLength": "3yr",
                            "OfferingClass": "standard",
                            "PurchaseOption": "Partial Upfront"
                        }
                    },
                    "Q49VKFFWC877GUFC.R5XV2EPZQZ": {
                        "priceDimensions": {
                            "Q49VKFFWC877GUFC.R5XV2EPZQZ.2TG2D8R56U": {
                                "unit": "Quantity",
                                "description": "Upfront Fee",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.R5XV2EPZQZ.2TG2D8R56U",
                                "pricePerUnit": {
                                    "USD": "3627"
                                }
                            },
                            "Q49VKFFWC877GUFC.R5XV2EPZQZ.6YS6EN2CT7": {
                                "unit": "Hrs",
                                "endRange": "Inf",
                                "description": "Windows (Amazon VPC), m4.xlarge reserved instance applied",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.R5XV2EPZQZ.6YS6EN2CT7",
                                "beginRange": "0",
                                "pricePerUnit": {
                                    "USD": "0.1380000000"
                                }
                            }
                        },
                        "sku": "Q49VKFFWC877GUFC",
                        "effectiveDate": "2017-04-30T23:59:59Z",
                        "offerTermCode": "R5XV2EPZQZ",
                        "termAttributes": {
                            "LeaseContractLength": "3yr",
                            "OfferingClass": "convertible",
                            "PurchaseOption": "Partial Upfront"
                        }
                    },
                    "Q49VKFFWC877GUFC.7NE97W5U4E": {
                        "priceDimensions": {
                            "Q49VKFFWC877GUFC.7NE97W5U4E.6YS6EN2CT7": {
                                "unit": "Hrs",
                                "endRange": "Inf",
                                "description": "Windows (Amazon VPC), m4.xlarge reserved instance applied",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.7NE97W5U4E.6YS6EN2CT7",
                                "beginRange": "0",
                                "pricePerUnit": {
                                    "USD": "0.3265000000"
                                }
                            }
                        },
                        "sku": "Q49VKFFWC877GUFC",
                        "effectiveDate": "2017-10-31T23:59:59Z",
                        "offerTermCode": "7NE97W5U4E",
                        "termAttributes": {
                            "LeaseContractLength": "1yr",
                            "OfferingClass": "convertible",
                            "PurchaseOption": "No Upfront"
                        }
                    },
                    "Q49VKFFWC877GUFC.CUZHX8X6JH": {
                        "priceDimensions": {
                            "Q49VKFFWC877GUFC.CUZHX8X6JH.6YS6EN2CT7": {
                                "unit": "Hrs",
                                "endRange": "Inf",
                                "description": "Windows (Amazon VPC), m4.xlarge reserved instance applied",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.CUZHX8X6JH.6YS6EN2CT7",
                                "beginRange": "0",
                                "pricePerUnit": {
                                    "USD": "0.1599000000"
                                }
                            },
                            "Q49VKFFWC877GUFC.CUZHX8X6JH.2TG2D8R56U": {
                                "unit": "Quantity",
                                "description": "Upfront Fee",
                                "appliesTo": [],
                                "rateCode": "Q49VKFFWC877GUFC.CUZHX8X6JH.2TG2D8R56U",
                                "pricePerUnit": {
                                    "USD": "1400"
                                }
                            }
                        },
                        "sku": "Q49VKFFWC877GUFC",
                        "effectiveDate": "2017-10-31T23:59:59Z",
                        "offerTermCode": "CUZHX8X6JH",
                        "termAttributes": {
                            "LeaseContractLength": "1yr",
                            "OfferingClass": "convertible",
                            "PurchaseOption": "Partial Upfront"
                        }
                    }
                }
            },
            "version": "20180410013226",
            "publicationDate": "2018-04-10T01:32:26Z"
        }'
    ], 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '713af440-4cc4-11e8-82b6-c71b4ccc4334', 'HTTPHeaders': {'x-amzn-requestid': '713af440-4cc4-11e8-82b6-c71b4ccc4334', 'date': 'Mon,
            30 Apr 2018 22: 18: 42 GMT', 'content-length': '10290', 'content-type': 'application/x-amz-json-1.1'
        }
    }
}
相关问题