How do I get the pricing data for EC2 in the Python SDK? (New to python)

0

I'm new to python so please bear with me. I'm following the layout of the data in this link:

https://aws.amazon.com/blogs/aws/aws-price-list-api-update-new-query-and-metadata-functions/

and this is the relevant code:

  for price in responsee['PriceList']:
                price_dict = json.loads(price)
                product = price_dict['product']
                attributes = product['attributes']
                terms = price_dict['terms']
                on_demand_terms = terms['OnDemand']
                for dimension in on_demand_terms:
                    pricedimension = dimension['priceDimension']
                    for y in pricedimension:
                        if vcpu == attributes['vcpu']:
                            finalprice = y['pricePerUnit']['USD']
                            print(finalprice)

and the error message I generally get is this:

    pricedimension = dimension['priceDimension'] 
                     ~~~~~~~~~^^^^^^^^^^^^^^^^^^ 
TypeError: string indices must be integers, not 'str

I've tried everything I could think of but I can't figure out how to get to "priceperUnit". that's basically what I'm trying to do. I don't know how to get thru the last layers in the data.

'terms': {'OnDemand': {'24GRA8RB2KZ9NPCS.JRTCKXETXF': {'effectiveDate': '2017-09-01T00:00:00Z',
                                                        'offerTermCode': 'JRTCKXETXF',
                                                        'priceDimensions': {'24GRA8RB2KZ9NPCS.JRTCKXETXF.6YS6EN2CT7':

just to be clear, what I'm trying to get through with "dimension" is the key after OnDemand that starts with "24GRA". it is apparently a string and is not being registered as a dictionary, it is not fetching the other values when I try to use the dictionary route. also, is this really grabbing all the products for ec2? the list seems a little short. there are hundreds of instances for ec2, but this seems to only be grabbing one price? I apologize as I am new to python. I've been doing this for about a year now. Please advise.

1 Answer
0

To get the key right after 'OnDemand' beginning with '24GR', you can do:

for price in responsee['PriceList']: price_dict = json.loads(price) product = price_dict['product'] attributes = product['attributes'] terms = price_dict['terms']

	key = terms['OnDemand']
	key = str(key)
	key = key.split(str("'"),1)[1]
	key = key.split(str("'"),1)[0]
answered a year ago

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