Sub topic 8
Spot Instances: spare capacity at a discount
This chapter assumes you know what an EC2 instance, an instance family, a Region, and an Availability Zone are. You should also be able to run an AWS CLI command. It covers what Spot Instances are, how you request them, and what can go wrong. The next chapter covers handling interruptions in detail.
What a Spot Instance is
AWS keeps a large pool of EC2 capacity. Some of it is not in use at any given moment. AWS sells that spare capacity as Spot Instances, usually at a large discount compared with On-Demand pricing. AWS documents savings of up to 90%, and the actual discount varies by instance type and Availability Zone.
The trade-off is that AWS can take the capacity back. When AWS needs it for On-Demand or reserved customers, it interrupts your Spot Instance. You get a two-minute warning first.
| Model | Price vs On-Demand | Commitment | Main risk |
|---|---|---|---|
| On-Demand | Baseline | None | Highest cost |
| Reserved Instances and Savings Plans | Lower | One or three years | Paying for unused commitment |
| Spot | Lowest | None | Interruption |
Spot needs no long-term commitment. You pay for the risk of interruption, not for a contract.
How pricing works
Each instance type in each Availability Zone is its own Spot capacity pool. Each pool has its own current Spot price. The price changes gradually based on long-term supply and demand. You no longer bid against other users. You pay the current Spot price for the time the instance runs, and you can optionally set a maximum price. The default maximum is the On-Demand price, and leaving it there is the usual advice.
Capacity is the main variable. A pool can run out, and then AWS cannot start new Spot Instances in it or must reclaim running ones.
Requesting a Spot Instance
The simplest way is to add market options to run-instances. The AMI ID below is a placeholder, so replace it with one from your Region.
aws ec2 run-instances \
--image-id ami-0123456789abcdef0 \
--instance-type c5.large \
--count 1 \
--instance-market-options '{"MarketType":"spot","SpotOptions":{"SpotInstanceType":"one-time"}}'one-time means that if the instance is interrupted, the request ends. It is not resubmitted. A persistent request would be resubmitted to try again.
For fleets, you normally don't launch Spot Instances one by one. An Auto Scaling group or EC2 Fleet requests capacity across several pools for you. Later chapters show how to configure that.
Common errors
- Using Spot for work that cannot be interrupted. A single-instance database or a long job with no checkpoints loses work when AWS reclaims the instance.
- Choosing one instance type in one zone. All your capacity sits in one pool, so one shortage can remove all of it. Spreading across pools reduces this risk.
- Skipping interruption handling. If your software ignores the two-minute notice, it does not drain connections, save state, or deregister from a load balancer.
- Lowering the maximum price to save money. A low cap does not make the instance cheaper while it runs. It only makes interruption and unfulfilled requests more likely.
- Expecting a fixed discount. The discount differs by pool and changes over time. Check current prices before you plan costs.
Where Spot fits
Spot suits workloads that are fault tolerant, flexible about timing, or made of many small, independent units. Examples are batch processing, CI builds, data analysis, rendering, and stateless web tiers behind a load balancer. Steady, always-on workloads with state fit commitments better.
Exercise
A team runs a nightly image-processing job split into thousands of independent tasks, and any failed task can be retried. Is Spot a good fit, and why? Name one thing they should do to reduce the effect of capacity shortages.
Show answer
Yes. The tasks are independent and retryable, so an interruption only repeats a small amount of work. They should diversify across several instance types and Availability Zones so that a shortage in one capacity pool does not stop the whole job.