AWS: How to get the Spot price of EC2 Instances using Python and Boto3

AWS EC2 spot instances price

2 min read | by Jordi Prats

Running on spot instances can save us a bunch of money on the cloud. It is based on long-term trends in supply and demand. You'll pay the Spot price that's in effect for the time period your instances are running. We can use boto3 to retrieve the current spot price:

To do se we can use the describe_spot_price_history() function that it's going to fetch us the price for a period of time and pick the most recent one.

Instance Type                  Product                        Availability Zone              Spot Price                     Last Update
t3.small                       Linux/UNIX (Amazon VPC)        us-west-2a                     0.006200                       2022-05-26 03:49:44+00:00
t3.small                       Linux/UNIX (Amazon VPC)        us-west-2b                     0.006200                       2022-05-26 03:49:44+00:00
t3.small                       Linux/UNIX (Amazon VPC)        us-west-2c                     0.006200                       2022-05-26 03:49:44+00:00
t3.small                       Linux/UNIX (Amazon VPC)        us-west-2d                     0.006200                       2022-05-26 03:49:44+00:00

Depending on the instance-type we might get a different price depending on the AZ we are running it:

Instance Type                  Product                        Availability Zone              Spot Price                     Last Update
m5a.large                      Linux/UNIX (Amazon VPC)        us-west-2a                     0.039100                       2022-05-26 03:35:51+00:00
m5a.large                      Linux/UNIX (Amazon VPC)        us-west-2b                     0.036900                       2022-05-26 02:43:51+00:00
m5a.large                      Linux/UNIX (Amazon VPC)        us-west-2c                     0.041700                       2022-05-26 05:43:23+00:00
m5a.large                      Linux/UNIX (Amazon VPC)        us-west-2d                     0.033800                       2022-05-25 23:15:15+00:00

So we will have to first find all the AZ using the describe_availability_zones() function and then request each of the prices separately. The code to do so would be:

def current_price(instance_type, product=['Linux/UNIX (Amazon VPC)'], region='us-west-2'):

  ec2_client = boto3.client(service_name='ec2', region_name=region)

  response = ec2_client.describe_availability_zones()

  out_format='{: <30} {: <30} {: <30} {: <30} {}'

  print(out_format.format('Instance Type', 'Product', 'Availability Zone', 'Spot Price', "Last Update"))

  for az in response['AvailabilityZones']:
    for each_product in product:
      spot_response = ec2_client.describe_spot_price_history(
                                                          InstanceTypes=[instance_type],
                                                          ProductDescriptions=[each_product],
                                                          AvailabilityZone=az['ZoneName']
                                                        )
      print(out_format.format(
                              instance_type,
                              each_product,
                              az['ZoneName'],
                              spot_response['SpotPriceHistory'][0]['SpotPrice'],
                              spot_response['SpotPriceHistory'][0]['Timestamp']
                            ))

Posted on 26/05/2022