It's a Trap? (EC2 Spot Instances)
Tags: aws, sre, ec2, devops
Published: 2022-08-18 14:15:00

> TLDR: Spot is an opportunity for big savings with no commitment, but costs are unpredictable. Be wary of using Spot too much and driving the price up on yourself (not kidding). Check out the guide on our [website](https://getstrake.com/blog/aws-cost-analysis-amazon-ec2-costs#spot) to analyze your own spot usage.


## Is it a Trap?
All jokes aside, AWS does a great job marketing EC2 Spot Instances. When you do a quick google search, the promise of 'up to 90% savings' is thrown on the table. Who isn't going to click on that?

![Google search for Spot Instances](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d9qno8fxys3m0ygr70dw.png)

The best part? This is <u>technically</u> true. There are times when you can get massive discounts using spot instances. So why would AWS do this?

Take a moment and think about how much time and energy has been spent trying to forecast cloud usage or the server needs at your own data center. To say the least, this is a painful process. To say more, we (read: I) never get it right.

So how can AWS promise international availability for hundreds of services to millions of customers? If you strip AWS' business model down to the core, they are the foremost data center capacity planning experts. However, even the experts need to build some cushion into their plan every once in a while. For EC2, when there is excess capacity, that is sold on the market as Spot Instances.

The thought process from AWS is probably: We already bought it, might as well try to get **some** money for it! 

## How do you use Spot Instances
You submit a 'Spot Fleet Request' when you want to use spot instances. This request contains the following parameters:

1. AMI
2. Target capacity (# of instances)
    - Maintain target capacity? Allow AWS to automatically replace interrupted Spot Instances
    - Set a maximum cost for Spot Instances
3. Network and AZ details
4. Instance type requirements: either specify the instance type or the compute requirements for your workload
5. Select an allocation strategy

Once all these parameters are filled out, you will be given a 'Fleet Strength' score to describe how likely you will find a match.

## How does Spot Compare?
On average, here is what you should expect financially from Spot Instances:
- Average of 70% discount when comparing Spot to On-Demand pricing.
- Average additional 10% discount when comparing Spot with 3yr no-upfront Compute Savings Plans and NO COMMITMENT

I said on average because the fun part about spot instances is how frequently the pricing can change! 

The harsh fact about Spot is there is only so much excess EC2 capacity. The more customers try to use Spot instances, the higher the price goes (classic supply and demand economics). Check out this example from the Spot Instance pricing history tool in the EC2 console that highlights the pricing variance over the last three months. 

![Spot instance price variance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0o97t69k6owqaqbkxnsa.png)

There are two key takeaways from this image:

1. You will **sometimes** get a better discount for Savings Plans than Spot.
2. This price is highly variable. This will throw your team for a loop when you're trying to figure out what your costs will be in the future.

If you're using your [cost and usage report](https://getstrake.com/blog/cost-and-usage-report-setup) for cost analysis, I've written some SQL code to help you identify your spot instance rates over time:

```sql
#Spot existence cost
##Spot existence by usage start date, instance type, region

SELECT
	[lineItem/UsageStartDate],
	[product/instanceType],
	[product/region],
	[lineItem/UnblendedCost]/[lineItem/UsageAmount] as spot_rate
FROM CUR
WHERE
	[lineItem/ProductCode] = 'AmazonEC2'
	and [product/instanceType] <> ""
	and [lineItem/LineItemType] is 'Usage'
	and [lineItem/UsageType] LIKE '%SpotUsage%'
	and [lineItem/Operation] LIKE 'RunInstances:SV%'
GROUP BY
	[product/instanceType],
	[lineItem/UsageStartDate],
	[product/region]
ORDER BY
	[lineItem/UnblendedCost]/[lineItem/UsageAmount];
```

## When to use Spot Instances:
Spot instances are a no-brainer for dev clusters, research projects, and short-term workloads that don't have a tight deadline. You can set a cost target, pick from various instance types, and allow AWS to interrupt (pause) your work when the price gets too high. The work will eventually get done, and you will come in under budget.

The real opportunity is for the engineers who build an architecture that can handle some of their infrastructure running on spot instances. The combination of Spot, On-Demand, and Savings Plans will provide the optimal balance of low-cost and reliable infrastructure. 

If you can do this successfully, your biggest problem will become your own spot usage throwing off the supply/demand equation and increasing your own costs. Think about that one for a bit..

_Content Powered by [Strake](https://getstrake.com)_