Skip to content
Go back

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

Aniket Maurya

A 2% EC2 Spot interruption rate sounds harmless in a spreadsheet. On a 10-job GitHub Actions matrix, that same 2% per-job reclaim rate breaks 18.3% of your pull requests (1 - 0.98^10). Bump the reclaim rate to 5% during a busy weekday window, and 40.1% of your 10-job PRs fail before merge.

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.

Here is how a small per-job Spot reclaim rate compounds across a multi-job workflow:

Jobs in your PR workflow1% Spot reclaim rate2% Spot reclaim rate5% Spot reclaim rate
1 job1.0% PRs fail2.0% PRs fail5.0% PRs fail
5 parallel jobs4.9% PRs fail9.6% PRs fail22.6% PRs fail
10 parallel jobs9.6% PRs fail18.3% PRs fail40.1% PRs fail
20 parallel jobs18.2% PRs fail33.2% PRs fail64.2% PRs fail

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.

What happens during an EC2 Spot interruption

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 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.

Timeline of a 12-minute CI job interrupted by an EC2 Spot reclaim at minute 11, which triggers a full 12-minute rerun for 23 billed minutes total.

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.

Why matrix math turns a 2% Spot rate into 18% broken PRs

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 real-world factors make that number worse than the clean formula:

The rerun bill and the developer-time tax

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:

  1. Phantom flake triage. Most developers do not check AWS EventBridge when a PR turns red. They open the test log, see a truncated trace, and spend 15 to 20 minutes to verify whether their commit broke an unrelated test.
  2. Broken flow state. An extra 12 to 20 minutes of retry wait time pushes a developer onto another branch or another task. Context switches cost far more than $0.02 of EC2 Spot compute.
  3. Eroded trust in CI. Once engineers learn that “red CI usually just needs a retry,” they stop trust-checks on real failures and reflexively hit Re-run on genuine bugs.

Four ways teams mitigate Spot reclaims (and what each fix costs)

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:

  1. Use price-capacity-optimized across wide instance pools. Configure your EC2 Fleet or autoscaler with the price-capacity-optimized allocation strategy across multiple instance families (such as m7a, m8a, c7i, c8i) and multiple Availability Zones. Narrow single-family pools get reclaimed far more often.
  2. Keep every CI job under 10 minutes. Split 30-minute test suites into sharded 5-minute jobs. Shorter jobs have lower reclaim exposure and cost less to retry.
  3. Automate disconnect retries. Wire a webhook or controller to detect 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.
  4. Route release, deploy, and long jobs to On-Demand. Never put a 25-minute production build or database migration on Spot. Keep Spot for short, idempotent PR checks, and accept On-Demand prices (3x to 4x higher than Spot) for critical paths.

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.

When to skip Spot altogether

If you only chose AWS Spot to escape GitHub Actions’ $0.006/min rate, compare the all-in cost against a low-cost managed runner before you build Spot retry machinery.

On 2 vCPU / 8 GB Linux x64, Celesto CI costs $0.00067/min (celesto-memory), with no EC2 Spot 2-minute reclaims, no NAT Gateway or egress fees, and up to 25 GiB of actions/cache storage included per paid repository. For a full side-by-side table across GitHub Actions, Blacksmith, RunsOn, Depot, WarpBuild, Ubicloud, and Celesto CI, see GitHub Actions Runners Compared.

To test whether a managed runner eliminates your Spot flakes, move your longest or most-reclaimed Linux job first:

jobs:
  e2e:
    # 4 vCPU / 16 GB Linux x64 at $0.00133/min, no Spot reclaims
    runs-on: celesto-large
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npm run test:e2e

Install the Celesto CI GitHub App, link a workspace (eligible workspaces include a one-time $5 trial credit), and track your rerun rate for one week. See the Celesto CI quickstart for runner sizes and setup steps.

Frequently asked questions

How much notice does AWS give before it terminates a Spot instance?

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.

Can GitHub Actions automatically retry a job killed by a Spot interruption?

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.

Which EC2 Spot allocation strategy has the fewest interruptions for CI?

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.

Should I run deploy or release jobs on EC2 Spot instances?

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.


Share this post on:

Next Post
GitHub Actions Runners Compared: Celesto CI vs. GitHub, Blacksmith, and RunsOn