in get-ec2-pricing/app.py [0:0]
def getPrice_from_API(oper_sys, instance_type, region, term_type):
try:
pricing = boto3.client('pricing', region_name='us-east-1')
logger.info("instance: {}".format(instance_type))
searchFilters=[
{
'Type':'TERM_MATCH',
'Field':'tenancy',
'Value':'Shared'
},
{
'Type':'TERM_MATCH',
'Field':'location',
'Value':region_lookup(region)
},
{
'Type':'TERM_MATCH',
'Field':'operatingSystem',
'Value':oper_sys
},
{
'Type':'TERM_MATCH',
'Field':'preInstalledSw',
'Value':'NA'
},
{
'Type':'TERM_MATCH',
'Field':'termType',
'Value': term_type
},
{'Type' :'TERM_MATCH', 'Field':'capacityStatus', 'Value':'Used'},
{
'Type':'TERM_MATCH',
'Field':'instanceType',
'Value': instance_type
}
]
#windows adds an extra license filter
if 'Windows' in oper_sys:
searchFilters.append({"Type":"TERM_MATCH", "Field": "licenseModel", "Value": "No License required"})
response = pricing.get_products(
ServiceCode='AmazonEC2', # required
Filters = searchFilters,
FormatVersion='aws_v1', # optional
NextToken='', # optional
MaxResults=20 # optional
)
if len(response['PriceList']) > 1 :
logger.info("Pricing list has more than one entry, considering first entry")
elif len(response['PriceList']) == 0 :
logger.info("Couldn't query pricing with given filters")
resp_json = json.loads(response['PriceList'][0])
price = 0
for key, value in resp_json['terms'][term_type].items():
logger.info("Reading Price for termType {}, key {}".format(term_type,key))
for dimkey, dimValue in value['priceDimensions'].items():
logger.info("Reading Price for dimension key {}".format(dimkey))
price = dimValue['pricePerUnit']['USD']
return Decimal(price)
except Exception as e:
print(e)
raise e