For a tool I'm developing, I wanted lambda prices by region. My requirement is that I need lambda type, price and region code. However, obtaining the lambda price by region is not straight forward. Especially if you need region code. If you need the lambda price and the region code, you need to make two separate http requests to AWS.
I thought the code could be useful to others, so I have shared it on my github repository. The remainder of this post will explain what I'm doing in the code to get lambda price for each region.
The code has three main functions:
get_price_by_region
get_region_code
price_to_csv
1: get_price_by_region()
This function make an http request and gets price for each lambda type for a region and save the result as a list.
def get_price_by_region ():
resp = requests.get(PRICE_BY_REGION_URL, headers=HEADERS)
region_price_dict = resp.json()
price_list=[]
for key, val in region_price_dict['regions'].items():
for item_key, item_val in val.items():
price_list.append([key, item_key, item_val['price']])
return (price_list)
2: get_region_code()
This function makes another http request to fetch region code and save the result as a list.
def get_region_code ():
resp = requests.get(REGION_CODE_URL, headers=HEADERS)
region_code_dict = resp.json()
region_list =[]
for k,v in region_code_dict.items():
region_list.append([k,v['code']])
return (region_list)
3: price_to_csv()
The first function's output does not contain region code, this function merges the two lists and constructs a new dataframe using Python's "merge" function. The produced dataframe is saved to a csv file, which I may then utilise to execute other operations if necessary.
def price_to_csv (price_list,code_list):
df_price_list = pd.DataFrame (price_list, columns = ['Region Name', 'Lambda Type', 'Price'])
df_code_list = pd.DataFrame(code_list, columns = ['Region Name', 'RegionCode'])
df_price_by_code = df_price_list.merge(df_code_list)
df_price_by_code.to_csv('./lambda_price_by_region_code.csv',index=False)
When the Python script is executed, the end result is a csv file with the following information about the lambda pricing.
Thanks for reading!
If you enjoyed this article feel free to share it on social media ๐
Say Hello on: Linkedin | Twitter | Polywork
Github: hseera
Top comments (0)