Sub topic 9
Handling Spot interruptions
This chapter assumes you know how to launch EC2 instances, use the AWS CLI, and understand that Spot Instances run on spare EC2 capacity at a discount. Here you will learn what a Spot interruption is, how the two-minute notice works, and how to build a workload that survives it.
What an interruption is
EC2 can reclaim Spot capacity when it needs the capacity back. Your instance then stops or terminates, depending on the request settings. You pay the Spot discount, and in return you accept that the instance can disappear.
The interruption behavior is part of the Spot request. The default is to terminate. Persistent requests can also stop or hibernate the instance, and a persistent request launches a replacement after an interruption. The one-time request in the example below is not resubmitted.
aws ec2 run-instances \
--image-id ami-0123456789abcdef0 \
--instance-type c5.large \
--count 1 \
--instance-market-options '{"MarketType":"spot","SpotOptions":{"SpotInstanceType":"one-time"}}'The AMI ID is a placeholder. Replace it with an AMI from your Region.
The two-minute notice
AWS sends a notice about two minutes before it interrupts a Spot Instance. The notice is a best-effort warning, and you should treat two minutes as the most time you will get. Two channels carry it:
- Instance metadata: the path
spot/instance-actionreturns a JSON document with the action and the time. Before a notice, it returns HTTP 404. - Amazon EventBridge: an event named "EC2 Spot Instance Interruption Warning" lets other services react, such as a Lambda function that drains a queue.
Poll the metadata path from the instance. This example gets a session token first, as IMDSv2 requires.
TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" \
-H "X-aws-ec2-metadata-token-ttl-seconds: 300")
curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \
http://169.254.169.254/latest/meta-data/spot/instance-actionA 404 response means no interruption is pending. When a notice arrives, the body looks like {"action": "terminate", "time": "2026-11-26T18:30:00Z"}. In a real agent, run the check in a loop every few seconds and start your shutdown steps as soon as the path returns a body.
What to do in two minutes
Two minutes is enough for short, well-prepared steps:
- Stop taking new work, for example by deregistering from the load balancer.
- Save progress to durable storage such as Amazon S3 or a database. Local instance storage is lost.
- Return unfinished jobs to the queue so another worker can pick them up.
- Exit cleanly.
Design for interruption before it happens. Keep workers stateless, make jobs idempotent (safe to run twice), and checkpoint long jobs regularly instead of only at the notice. Also use the Auto Scaling group's capacity rebalancing feature, which can launch a replacement when EC2 signals that a Spot Instance is at elevated risk of interruption.
Reducing how often it happens
Interruptions depend on how much spare capacity exists in a given instance type and Availability Zone. You cannot predict them, but you can make each one cheaper. Spread requests across several instance types and zones, so one capacity pool running short does not take down the whole fleet. The next chapter covers diversification and allocation strategies in detail.
Exercise
Your Spot worker polls spot/instance-action every five seconds and gets 404 each time. What does that mean, and what should the worker do when the response changes?
Show answer
A 404 means no interruption notice has been issued, so the worker keeps running normally. When the path returns a JSON body with an action and time, the worker should stop accepting new work, checkpoint its state to durable storage such as S3, return unfinished jobs to the queue, and exit within the roughly two minutes available.
Exercise
Why is a local checkpoint file on the instance's own disk not enough to survive a Spot interruption?
Show answer
When the instance is terminated, its local storage is lost. The checkpoint must go to durable storage such as S3 or a database so a replacement instance can resume from it.