Issue
I'm trying to fetch GPU instance prices for the EU (Ireland) region (eu-west-1). However, some of the instance types have price 0.00, even though the website (https://aws.amazon.com/ec2/pricing/on-demand/) shows them as available and definitely not 0.00.
I'm using the code below:
session = boto3.Session(profile_name='nimbo')
instance_types = list(sorted(ec2_instance_types('eu-west-1')))
# Filter by p and g instance types (gpus)
instance_types = [inst for inst in instance_types if inst[0] in ["p", "g"]]
# Use AWS Pricing API at US-East-1
pricing = session.client('pricing', region_name='us-east-1')
for instance_type in instance_types:
response = pricing.get_products(
ServiceCode='AmazonEC2',
MaxResults=1,
FormatVersion='aws_v1',
Filters=[
{ 'Type': 'TERM_MATCH', 'Field': 'instanceType', 'Value': instance_type },
{ 'Type': 'TERM_MATCH', 'Field': 'location', 'Value': 'EU (Ireland)' },
{ 'Type': 'TERM_MATCH', 'Field': 'operatingSystem', 'Value': 'Linux' },
]
)
inst = json.loads(response["PriceList"][0])
inst = inst['terms']['OnDemand']
inst = list(inst.values())[0]
inst = list(inst["priceDimensions"].values())[0]
inst = inst['pricePerUnit']
currency = list(inst.keys())[0]
price = float(inst[currency])
print(instance_type+": "+"%0.3f %s"%(price, currency))
This returns:
g2.2xlarge: 0.907 USD
g2.8xlarge: 0.000 USD
g3.16xlarge: 0.000 USD
g3.4xlarge: 1.331 USD
g3.8xlarge: 2.420 USD
g3s.xlarge: 0.796 USD
g4ad.16xlarge: 0.000 USD
g4ad.4xlarge: 0.000 USD
g4ad.8xlarge: 2.130 USD
g4dn.12xlarge: 4.645 USD
g4dn.16xlarge: 0.000 USD
g4dn.2xlarge: 0.838 USD
g4dn.4xlarge: 1.342 USD
g4dn.8xlarge: 0.000 USD
g4dn.metal: 8.724 USD
g4dn.xlarge: 0.625 USD
p2.16xlarge: 0.000 USD
p2.8xlarge: 7.776 USD
p2.xlarge: 0.000 USD
p3.16xlarge: 0.000 USD
p3.2xlarge: 3.305 USD
p3.8xlarge: 14.082 USD
p3dn.24xlarge: 0.000 USD
p4d.24xlarge: 0.000 USD
Any one has any idea what I'm missing here?
Solution
There are many dimensions to pricing of an Amazon EC2 instance.
Your code is only looking at the first element returned (MaxResults=1
), out of a result set of over 100 records.
When I ran your code, it was displaying:
$0.907 per Dedicated Unused Reservation Linux with SQL Web g2.2xlarge Instance Hour
$0.00 per Reservation Linux g2.8xlarge Instance Hour
I presume you want On-Demand pricing without any Reservations in place. In this case, you would need additional filters:
{ 'Type': 'TERM_MATCH', 'Field': 'capacitystatus', 'Value': 'Used' },
{ 'Type': 'TERM_MATCH', 'Field': 'preInstalledSw', 'Value': 'NA' },
{ 'Type': 'TERM_MATCH', 'Field': 'tenancy', 'Value': 'shared' },
(Thanks to Use boto3 to get current price for given EC2 instance type for this configuration!)
The result is then:
g2.2xlarge: 0.702 USD
g2.8xlarge: 2.808 USD
I suggest that you remove the MaxResults
setting so that you can see when more results are returned.
Answered By - John Rotenstein