AWS for DevOps — Spot Instances
14.4 — Spot Instances
Spot Instances use unused EC2 capacity at up to 90% discount. The trade-off: AWS can reclaim them with a 2-minute warning.
Real-World Analogy
Spot Instances are like standby airline tickets. You get a seat at a massive discount, but if the flight fills up with full-fare passengers, you get bumped with 2 minutes notice. Perfect if you’re flexible; terrible if you absolutely must be on that flight.
Good Workloads for Spot
| Workload | Why It Works |
|---|---|
| CI/CD build agents | Builds can restart; short-lived |
| Batch data processing | Jobs are idempotent; can resume |
| Auto Scaling groups (mixed) | Other instances absorb traffic |
| Dev/test environments | Interruption is acceptable |
| Machine learning training | Checkpointing supports resume |
| Containers (ECS/EKS) | Orchestrator reschedules tasks |
Bad Workloads for Spot
| Workload | Why It’s Risky |
|---|---|
| Single-instance databases | Interruption = downtime |
| Stateful applications without checkpointing | Data loss risk |
| Long-running jobs that can’t be restarted | Wasted compute |
| Primary production web servers | User-facing interruptions |
Using Spot with Auto Scaling Groups
# Create a mixed instances policy (On-Demand + Spot)
$ aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name production-asg \
--mixed-instances-policy '{
"LaunchTemplate": {
"LaunchTemplateSpecification": {
"LaunchTemplateId": "lt-abc123",
"Version": "$Latest"
},
"Overrides": [
{"InstanceType": "m5.large"},
{"InstanceType": "m5a.large"},
{"InstanceType": "m4.large"},
{"InstanceType": "m5d.large"}
]
},
"InstancesDistribution": {
"OnDemandBaseCapacity": 2,
"OnDemandPercentageAboveBaseCapacity": 20,
"SpotAllocationStrategy": "capacity-optimized"
}
}' \
--min-size 4 \
--max-size 20 \
--desired-capacity 10 \
--vpc-zone-identifier "subnet-abc123,subnet-def456"
This creates:
- 2 On-Demand instances (base capacity — always running)
- Of the remaining 8: 20% On-Demand + 80% Spot
- Result: 4 On-Demand + 6 Spot = 10 total
Spot Allocation Strategies
| Strategy | How It Works | Best For |
|---|---|---|
| capacity-optimized | Picks pools with most capacity | Lowest interruption rate (recommended) |
| lowest-price | Picks cheapest pools | Maximum savings (higher interruption) |
| diversified | Spreads across all pools | Balanced approach |
Handling Spot Interruptions
# Check for interruption notice (from within the instance)
$ curl -s http://169.254.169.254/latest/meta-data/spot/instance-action
{"action": "terminate", "time": "2026-03-30T12:00:00Z"}
Best practices for handling interruptions:
- Use multiple instance types (4+ types)
- Spread across multiple AZs
- Use
capacity-optimizedallocation strategy - Implement graceful shutdown handlers
- Design applications to be stateless
Spot with ECS Fargate
# Use Fargate Spot for non-critical tasks
# In your ECS service, set the capacity provider strategy:
$ aws ecs create-service \
--cluster production \
--service-name batch-processor \
--task-definition batch:1 \
--capacity-provider-strategy '[
{"capacityProvider": "FARGATE", "weight": 1, "base": 1},
{"capacityProvider": "FARGATE_SPOT", "weight": 4}
]' \
--desired-count 5
# Result: 1 Fargate (base) + 4 Fargate Spot
Common Mistakes
| Mistake | Why It Hurts | Fix |
|---|---|---|
| Single instance type | Higher interruption risk | Use 4+ instance types |
| 100% Spot for production | Service disruption | Keep On-Demand base capacity |
| No graceful shutdown | Data loss on interruption | Handle SIGTERM signal |
| Not using capacity-optimized | More interruptions | Switch from lowest-price |
Quick Reference
| Task | Command |
|---|---|
| Check Spot price history | aws ec2 describe-spot-price-history --instance-types m5.large --product-descriptions "Linux/UNIX" |
| Request Spot Instance | aws ec2 request-spot-instances --instance-count 1 --type one-time --launch-specification '...' |
| Create mixed ASG | aws autoscaling create-auto-scaling-group --mixed-instances-policy '...' |
| Check interruption notice | curl http://169.254.169.254/latest/meta-data/spot/instance-action |
Next → Tagging & Right-Sizing