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.

posta un anno fa411 visualizzazioni
2 Risposte
1
Risposta accettata

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

profile pictureAWS
ESPERTO
con risposta un anno fa
profile picture
ESPERTO
verificato 25 giorni fa
  • 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?

ESPERTO
con risposta un anno fa
  • This has literally thousands of fields, which if you're not already familiar with the object, can take days to sort out. Thanks anyway.

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande