I'm writing a code to get EC2 On-Demand prices for a particular OS in a determined region (eg. 'Ubuntu Pro' and 'us-east-1') but I only get 661 instances out of the 705 available on the pricing list. Is there a way that I can retrieve all the instances available for said region and OS - with the correct price?
Pricing list:
Database:
Python Code:
import boto3
import json
import os
from database import insert_into_table
def ec2_prices(region, op_system, database_name, table_name):
desired_region = region
pricing_region = 'us-east-1'
ec2 = boto3.client('ec2', region_name=desired_region)
pricing_client = boto3.client('pricing', region_name=pricing_region)
response = ec2.describe_instance_type_offerings(LocationType='region')
instance_types = [instance_type['InstanceType'] for instance_type in response['InstanceTypeOfferings']]
for instance_type in instance_types:
response = pricing_client.get_products(
ServiceCode='AmazonEC2',
Filters=[
{'Type': 'TERM_MATCH', 'Field': 'instanceType', 'Value': instance_type},
{'Type': 'TERM_MATCH', 'Field': 'operatingSystem', 'Value': op_system},
],
)
for product in response['PriceList']:
product_dict = json.loads(product)
attributes = product_dict.get('product').get('attributes')
instance_region = attributes.get('regionCode')
instance_region_name = attributes.get('location')
instance_memory = attributes.get('memory')[:-3]
instance_vcpu = attributes.get('vcpu')
instance_os = attributes.get('operatingSystem')
tenancy = attributes.get('tenancy')
if instance_region == desired_region and tenancy == 'Shared':
terms = product_dict.get('terms', {}).get('OnDemand', {})
for term_key, term_value in terms.items():
price_dimensions = term_value['priceDimensions']
for _, price_dimension_value in price_dimensions.items():
price = price_dimension_value['pricePerUnit']
description = price_dimension_value['description']
value = float(price.get('USD', 0))
if value > 0 and f'{op_system} {instance_type}' in description:
insert_into_table(
database_name,
table_name,
instance_type,
value,
instance_region,
instance_vcpu,
instance_memory,
instance_os
)
There are some tags (tenancy
and description
) that are used for retrieving the right price for the instance.
Hello!
It gets 761 instances - the problem is with the pricing, it's probably missing. If I delete the
tenancy
flag, it gets 701 instances, but those don't have the correct price.Apparently the API can't retrieve all the instance prices available on the Pricing List, but I'm not entirely sure.