【发布时间】:2019-08-03 00:02:45
【问题描述】:
我正在尝试使用以下代码来获取我所在地区的实例价格:
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 美元。
【问题讨论】:
标签: python python-2.7 amazon-ec2 boto3