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.

demandé il y a un an411 vues
2 réponses
1
Réponse acceptée

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

profile pictureAWS
EXPERT
répondu il y a un an
profile picture
EXPERT
vérifié il y a 25 jours
  • 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
répondu il y a un an
  • This has literally thousands of fields, which if you're not already familiar with the object, can take days to sort out. Thanks anyway.

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions