Retrieving spot price with boto3

0

Hello, I have the following function retrieving the price for an 'on-demand' instance:

    def get_price(self, region, instance, os):
        FLT = '[{{"Field": "operatingSystem", "Value": "{o}", "Type": "TERM_MATCH"}},' \
            '{{"Field": "instanceType", "Value": "{t}", "Type": "TERM_MATCH"}}]'
        f = FLT.format(r=region, t=instance, o=os)
        rrr = boto3.client('pricing')
        data = rrr.get_products(ServiceCode='AmazonEC2', Filters=json.loads(f))
        od = json.loads(data['PriceList'][0])['terms']['OnDemand']
        id1 = list(od)[0]
        id2 = list(od[id1]['priceDimensions'])[0]
        return od[id1]['priceDimensions'][id2]['pricePerUnit']['USD']

However, and when I change ['OnDemand'] to ['spot'] (or ['Spot']) the code tells me that Spot is not a keyword. Is there any way to modify the function to gather the spot price? Thanks.

已提问 1 年前411 查看次数
2 回答
1
已接受的回答

Have you tried retrieving the Spot price history? Does that give you the information you require?

profile pictureAWS
专家
已回答 1 年前
profile picture
专家
已审核 25 天前
  • Yes. The problem was that instead of a nested dictionary, it seems to be storing the data as single lists. It's pretty odd. Thanks.

0

I would print out "data" after calling get_products() so you can see the structure you're trying to navigate. I think PriceList is a list of products, and you're only looking at the first element '[0]' of the list which I guess is an on-demand product?

专家
已回答 1 年前
  • This has literally thousands of fields, which if you're not already familiar with the object, can take days to sort out. Thanks anyway.

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则