We rely heavily on spot instances in our infrastructure, with most of our production workloads running on them. Spot instances are significantly cheaper than on-demand instances, but their prices are not fixed—they can change every minute. Therefore, we must monitor spot prices to ensure we’re not paying more than the on-demand rates.


Understanding AWS Spot Pricing

AWS Spot Instances operate on a supply-and-demand model where prices fluctuate based on the available EC2 capacity and current demand for compute resources. Here’s how the pricing mechanism works:

Supply and Demand

  • Spot prices are determined by the available unused EC2 capacity in each Availability Zone.
  • When demand increases or available capacity decreases, spot prices tend to rise.
  • When demand decreases or capacity becomes more available, prices typically fall.
  • Prices can vary significantly across different AWS regions and Availability Zones.

Price Determination

  • AWS uses a market-driven pricing model.
  • Prices are updated every 5 minutes.
  • Each instance type in each Availability Zone has its own spot price.
  • Prices can range from as low as 20% of the on-demand price to potentially reaching the on-demand price.

Interruption Mechanism

  • AWS can reclaim spot instances with a 2-minute notification when:
    • The spot price exceeds your maximum bid price.
    • EC2 capacity is needed for on-demand instances.
  • Your instance will be interrupted (stopped or terminated) when AWS needs the capacity back.

How We Use Spot Instances

We run nearly all our workloads in Kubernetes. All workloads are set up with multiple replicas to ensure high availability.

This setup allows us to leverage spot instances effectively. By running multiple replicas of the same workload on different spot instances and instance types, we ensure resilience.

To manage spot instances efficiently, we use Spotinst Ocean. Spotinst Ocean is a managed service that can automatically scale our workloads based on spot prices and instance types.

We select the instance types we want to use, and Spotinst Ocean dynamically scales our workloads based on spot price fluctuations and the performance of the selected instances.

This approach works well, but occasionally, significant changes in spot pricing require us to revisit and adjust the instance types we use—considering both pricing and performance.


Fetching Spot Prices

When we need to select new instance types or review the pricing of our current ones, we must fetch spot prices for the relevant instance types.

Typically, we do this manually by checking the AWS Management Console or using various online services that might not always be up-to-date. This manual process is time-consuming.

To address this, I wrote a small script to fetch spot prices directly from the AWS CLI. This approach simplifies the process and makes the data available instantly from the command line.


Prerequisites

You need to configure AWS CLI with the necessary permissions to fetch spot prices.

The script utilizes the aws ec2 describe-spot-price-history command to retrieve spot pricing data. You’ll also need:

  • bash for running the script
  • jq for parsing JSON output
  • A font that supports emojis (recommended for better output display)

Usage Instructions

Usage: fetch-aws-spotprice [-d] <region> <instance-type>
Options:
  -d    Enable debug output
Example: fetch-aws-spotprice -d us-east-1 t3.micro

Example Output

Fetch AWS spot prices

Get the script

The script is available on GitHub


Further Reading

For more detailed information about AWS Spot Instances, refer to these official AWS resources:

  1. AWS Spot Instances Documentation – Comprehensive guide on spot instances.
  2. Spot Best Practices – Recommendations for using spot instances effectively.
  3. Spot Instance Pricing History – View and analyze historical pricing data.
  4. Spot Instance Interruptions – Detailed explanation of interruption behavior.
  5. AWS Spot Instance Advisor – Tool for finding optimal spot instances for your workload.

For pricing-specific information and current rates, visit: