# Spot Instances for CI: Why Your Tests Fail at Random and What It Really Costs

> Source: <https://dev.to/aniketmaurya/spot-instances-for-ci-why-your-tests-fail-at-random-and-what-it-really-costs-2lee>
> Published: 2026-12-03 11:09:57+00:00

**Recommendation:** Model Spot interruption risk as 1-(1-p)^n across your CI matrix before moving runners to Spot, and use price-capacity-optimized allocation across wide instance pools and multiple Availability Zones to shrink p.
More in the playbook "Running AI in production": <https://wpnews.pro/playbooks/production-ops>

This article was prepared with AI assistance. [Originally published on the Celesto blog](https://celesto.ai/blog/posts/spot-instances-ci-flaky-tests-true-cost/).

A **2% EC2 Spot interruption rate** sounds harmless in a spreadsheet. On a 10-job GitHub Actions matrix with independent interruptions and no retries, a 2% per-job reclaim rate interrupts **18.3% of first attempts** (`1 - 0.98^10`). Bump the reclaim rate to **5%** in the model, and **40.1%** of first attempts have at least one interruption.

Teams put CI runners on EC2 Spot instances because Spot compute looks 60% to 75% cheaper than On-Demand. The hidden catch is that CI jobs cannot checkpoint and move when AWS takes the VM back. Every reclaim looks like a flaky test in GitHub, forces a full rerun from minute zero, and burns both compute and engineer focus.

The table shows the probability of at least one interruption on the first attempt, assuming equal, independent per-job risks.

| Jobs in your PR workflow | 1% Spot reclaim rate | 2% Spot reclaim rate | 5% Spot reclaim rate | 
|---|---|---|---|
| 1 job | 1.0% interrupted | 2.0% interrupted | 5.0% interrupted | 
| 5 parallel jobs | 4.9% interrupted | 9.6% interrupted | 22.6% interrupted | 
| 10 parallel jobs | 9.6% interrupted | **18.3% interrupted** | **40.1% interrupted** | 
| 20 parallel jobs | **18.2% interrupted** | **33.2% interrupted** | **64.2% interrupted** | 

If your CI pipeline has more than a handful of jobs per pull request, Spot interruptions are not a rare edge case. They become a daily tax on your merge queue.

AWS sells spare EC2 capacity at steep discounts with one clear rule: when On-Demand customers need those servers back, AWS reclaims them.

EC2 emits a [Spot Instance interruption notice](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-instance-termination-notices.html) **two minutes before termination**, on a best-effort basis. You can read that notice from the instance metadata service (IMDS) or from an EventBridge rule.

Two minutes works well for a stateless web server behind a load balancer. You stop new requests, finish active HTTP calls in a few seconds, and exit cleanly.

A GitHub Actions job does not work that way.

A running `pytest`, `cargo test`, or Playwright suite holds state across memory, local disk, PostgreSQL containers, and Docker build layers. You cannot serialize that state in 120 seconds and resume it on a second EC2 instance.

When minute 11 arrives, EC2 kills the VM. The `actions/runner` process vanishes without a graceful exit, GitHub times out the runner heartbeat, and the workflow check turns red with a generic message like `The self-hosted runner lost communication with the server`.

Most modern repositories do not run one monolithic job. A typical pull request fans out into a matrix: lint, typecheck, unit tests across two runtimes, integration tests, Docker build, and end-to-end browser checks.

If a PR requires all `n` jobs to pass and each job faces an independent Spot reclaim probability `p`, the chance that at least one job dies mid-run is:

```
P(PR fails due to Spot) = 1 - (1 - p)^n
```

Three factors affect how this model maps to a real workflow:

The direct compute waste is easy to measure. If a 12-minute integration job dies at minute 11 and reruns cleanly for 12 minutes, AWS bills you for **23 minutes** of compute, EBS `gp3` storage, and network traffic for a 12-minute job.

If a developer clicks **Re-run all jobs** instead of **Re-run failed jobs**, you pay for the entire 10-job matrix twice.

The bigger cost never shows up on your AWS invoice. It shows up in engineer attention:

`$0.02` of EC2 Spot compute.
If your CI jobs must stay inside your own AWS account, you can cut Spot pain sharply with four concrete practices. Every one of them adds engineer maintenance or eats into your Spot savings:

`price-capacity-optimized` across wide instance pools.`m7a`, `m8a`, `c7i`, `c8i`) and multiple Availability Zones. Narrow single-family pools get reclaimed far more often.`lost communication with the server` failures and automatically re-trigger only the interrupted job. That keeps developers out of the loop, though you still pay for the dead minutes and extra wait time.
Notice what happens as you layer those fixes together: you spend engineer hours on fleet allocation, fallback logic, and retry automation, and you still pay AWS for NAT Gateways, internet egress, public IPv4 addresses, EBS volumes, and CloudWatch logs. We calculated the full AWS invoice in [Why Self-Hosted GitHub Actions Runners on AWS Cost More Than the EC2 Bill](https://celesto.ai/blog/posts/aws-self-hosted-github-actions-hidden-costs/).

Track interrupted jobs, rerun duration, and time to a passing check by workflow. Move jobs with high retry costs or release dependencies to On-Demand capacity first. Compare the resulting bill and wait time with the same workloads on Spot.

AWS emits a best-effort Spot Instance interruption notice two minutes before termination. You can poll the instance metadata endpoint or subscribe via Amazon EventBridge, but two minutes is rarely enough to finish or checkpoint an active CI build.

Not natively out of the box. GitHub marks the job failed or cancelled when the runner disconnects. You need either manual reruns, a workflow-level retry wrapper, or an external webhook/orchestrator that calls the GitHub API to rerun failed jobs.

AWS recommends `price-capacity-optimized` across a broad list of equivalent instance families and multiple Availability Zones. It chooses pools with both low prices and deep spare capacity, which lowers interruption frequency compared to `lowest-price`.

No. A mid-deploy interruption can leave Terraform locks, partial container pushes, or half-finished database migrations behind. Keep stateful or release-critical workflows on On-Demand instances or a managed runner that does not reclaim VMs mid-job.
