【发布时间】: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