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.

gefragt vor einem Jahr411 Aufrufe
2 Antworten
1
Akzeptierte Antwort

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

profile pictureAWS
EXPERTE
beantwortet vor einem Jahr
profile picture
EXPERTE
überprüft vor 25 Tagen
  • 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?

EXPERTE
beantwortet vor einem Jahr
  • This has literally thousands of fields, which if you're not already familiar with the object, can take days to sort out. Thanks anyway.

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen