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.

asked a year ago398 views
2 Answers
1
Accepted Answer

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

profile pictureAWS
EXPERT
answered a year ago
profile picture
EXPERT
reviewed 12 days ago
  • 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?

EXPERT
answered a year ago
  • This has literally thousands of fields, which if you're not already familiar with the object, can take days to sort out. Thanks anyway.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions