首页 > 解决方案 > boto3 定价为相同类型的实例返回多个值

问题描述

我正在尝试使用以下代码来获取我所在地区的实例价格:

import boto3
import json

my_session = boto3.session.Session()
region = boto3.session.Session().region_name
print "region : ",region

pricing_client = boto3.client("pricing")

pricingValues = pricing_client.get_products(ServiceCode='AmazonEC2',Filters=[{'Type': 'TERM_MATCH','Field': 'instanceType','Value': 'm4.large'},{'Type': 'TERM_MATCH','Field': 'location','Value': 'Asia Pacific (Mumbai)'},{'Type': 'TERM_MATCH','Field': 'operatingSystem','Value': 'Linux'},{'Type': 'TERM_MATCH','Field': 'preInstalledSw','Value': 'NA'},{'Type': 'TERM_MATCH','Field': 'tenancy','Value': 'Dedicated'}])

for priceVal in pricingValues["PriceList"]:
    priceValInJson=json.loads(priceVal)
    if("OnDemand" in priceValInJson["terms"] and len(priceValInJson["terms"]["OnDemand"]) > 0):
        for onDemandValues in priceValInJson["terms"]["OnDemand"].keys():
            for priceDimensionValues in priceValInJson["terms"]["OnDemand"][onDemandValues]["priceDimensions"]:
                print "USDValue : ",priceValInJson["terms"]["OnDemand"][onDemandValues]["priceDimensions"][priceDimensionValues]["pricePerUnit"]," : ", priceValInJson["product"]["attributes"]["capacitystatus"]," : ", priceValInJson["product"]["attributes"]["usagetype"]

上述代码的输出是:

region :  ap-south-1
USDValue :  {u'USD': u'0.0000000000'}  :  AllocatedCapacityReservation  :  APS3-DedicatedRes:m4.large
USDValue :  {u'USD': u'0.1155000000'}  :  Used  :  APS3-DedicatedUsage:m4.large
USDValue :  {u'USD': u'0.1155000000'}  :  UnusedCapacityReservation  :  APS3-UnusedDed:m4.large

我想要做什么

我正在尝试获取实例类型的价格值,以便我可以使用 boto3 实例组以一半的价格出价。

我的观察

除 SKU 和输出中显示的参数外,所有参数均匹配。其中一个有一个 Reserved 字段,我猜它也是用于已保留的实例。

>>> json.loads(pricingValues["PriceList"][1])["terms"].keys()
[u'Reserved', u'OnDemand']

我的困惑是什么

我总是得到 3 个价格值。无论我选择哪种实例类型都是如此。我想了解这些是什么以及为什么报告的价格之一是 0.0 美元。

标签: pythonpython-2.7amazon-ec2boto3

解决方案


我找不到关于这些值的任何文档,但我的猜测是:

  • Used:按需使用实例的成本
  • UnusedCapacityReservation:未使用时预留实例的成本(您仍需付费)
  • AllocatedCapacityReservation:实例用作预留实例时的成本(已付费,因此无需成本)

这些只是我的猜测。


推荐阅读