Nov 20, 20264 min read

ECS Fargate Spot: When It Is Safe and How to Set It Up

Fargate Spot runs tasks on spare AWS capacity at a discount, and can reclaim them with a two-minute warning. Which workloads suit it, how to mix on-demand and Spot with a capacity provider strategy in Terraform, and how to handle the interruption signal in code.

Illustration for ECS Fargate Spot: When It Is Safe and How to Set It Up

Fargate bills for vCPU and memory by the second, and Fargate Spot sells the same capacity at a steep discount if you accept that AWS may take it back. For the right workloads that is a large saving for a small change in configuration.

The decisions that matter are which workloads are safe on Spot and how the code survives an interruption. This guide covers both, with a Terraform strategy and an idempotent consumer.

What is Fargate Spot and how does it behave?

AWS describes Fargate capacity providers as FARGATE and FARGATE_SPOT. Spot tasks run on spare capacity at up to a 70 percent discount, and when AWS needs the capacity back it sends the task a SIGTERM and a task state change event, then stops it after the container's stop timeout. For Spot the interruption notice is two minutes, so a task has up to two minutes to finish, though ECS stops the container at the stopTimeout of the task definition, which defaults to 30 seconds and can be raised up to 120.

Interruptions are not constant, but they are not rare either, and they can hit several tasks of a service at once when capacity tightens.

Which workloads are safe on Spot?

Safe: queue consumers with at-least-once delivery, batch jobs that checkpoint, and stateless web services with enough tasks that losing one only reduces capacity briefly. Not safe: a service running a single task with no replacement path, long computations that cannot resume, and anything that holds the only copy of state in memory or on the task's disk.

Services that must stay up can mix both capacity types, so Spot takes the discount on most tasks while a floor of on-demand tasks keeps the service alive when Spot capacity disappears.

How do you set it up?

A capacity provider strategy on the service sets the mix, as documented for the Terraform aws_ecs_service resource. base is the number of tasks that must run on a provider, and weight splits the rest:

resource "aws_ecs_cluster_capacity_providers" "this" { cluster_name = aws_ecs_cluster.this.name capacity_providers = ["FARGATE", "FARGATE_SPOT"] } resource "aws_ecs_service" "worker" { name = "worker" cluster = aws_ecs_cluster.this.id task_definition = aws_ecs_task_definition.worker.arn desired_count = 4 capacity_provider_strategy { capacity_provider = "FARGATE" base = 1 weight = 1 } capacity_provider_strategy { capacity_provider = "FARGATE_SPOT" weight = 3 } }

With four tasks this runs one on-demand task as the base, then splits the remaining three by weight 1 to 3, which puts most of the fleet on Spot. Changing the strategy rolls out as a new deployment of the service.

How do you handle interruptions in code?

Handle SIGTERM as a normal shutdown: stop taking new work, finish or checkpoint what is in flight and exit. For a queue consumer, do not acknowledge messages you did not finish, since SQS returns them after the visibility timeout. That relies on idempotent handlers, as covered in idempotent message consumers, because a message can be processed partly and then run again on another task. Pair the signal handling with graceful shutdown in containers and a stop timeout longer than your longest handler.

Fargate Spot and Encore

Encore

Encore deploys services to Fargate in your own AWS account, and the part of this guide that lives in application code carries over unchanged. Capacity provider choice belongs to the ECS service, so for the discount itself use the strategy above in your own cluster configuration. The code side is Pub/Sub subscribers that tolerate redelivery, which is what an interrupted task needs.

Set up the app

Get started
brew install encoredev/tap/encore &&
encore app create
Copy

Run in your terminal to get started locally.

// jobs/jobs.go package jobs import ( "context" "encore.dev/pubsub" "encore.dev/rlog" "encore.dev/storage/sqldb" ) var db = sqldb.NewDatabase("jobs", sqldb.DatabaseConfig{Migrations: "./migrations"}) type Job struct { ID string `json:"id"` } var Queue = pubsub.NewTopic[*Job]("jobs", pubsub.TopicConfig{ DeliveryGuarantee: pubsub.AtLeastOnce, }) var _ = pubsub.NewSubscription(Queue, "process-job", pubsub.SubscriptionConfig[*Job]{ Handler: func(ctx context.Context, j *Job) error { // Record the job once. A redelivery after an interruption finds the row and does nothing. res, err := db.Exec(ctx, `INSERT INTO processed (job_id) VALUES ($1) ON CONFLICT DO NOTHING`, j.ID) if err != nil { return err } if res.RowsAffected() == 0 { return nil } rlog.Info("processing job", "id", j.ID) // Do the work here. Return an error to retry, which runs it again. return nil }, MaxConcurrency: 5, })

The processed table lives in the service's Postgres database, and each delivery appears in a trace. If the task is reclaimed before the handler returns, the message is not acknowledged and is delivered again, and the processed row stops completed work from running twice. In production, record the row in the same transaction as the work's database writes, or make the work itself keyed on job_id.

A coding agent can write this idempotent handler together with its migration, test the redelivery path locally by publishing the same job twice, and leave the capacity strategy to the Terraform or cluster configuration.

Best fit: Go teams running queue workers on Fargate who want idempotent handlers that survive Spot interruptions and a capacity strategy chosen in their own AWS account.

Frequently asked questions

What is Fargate Spot?

Fargate Spot is a capacity option for ECS tasks that runs them on spare AWS capacity at a discount of up to 70 percent compared with on-demand Fargate. AWS can interrupt Spot tasks when it needs the capacity back, sending a SIGTERM and giving the task two minutes to stop.

Which workloads are safe on Fargate Spot?

Fault-tolerant ones: queue consumers whose messages are retried, batch and data processing that can resume, and stateless services running enough tasks that losing one is harmless. Single-task services and anything that holds state locally are poor fits.

How do you mix on-demand and Spot capacity in ECS?

Use a capacity provider strategy on the service. Set a base number of tasks on FARGATE for a guaranteed floor, then split the rest by weight between FARGATE and FARGATE_SPOT, for example 1 to 3, so most tasks run on Spot while a few always run on on-demand capacity.

How do you handle a Fargate Spot interruption?

Handle SIGTERM: stop accepting new work, finish or checkpoint what is in flight and exit before the stop timeout. For queue consumers, leave unfinished messages unacknowledged so they return to the queue, and keep handlers idempotent so a redelivered message is harmless.

Deploy to your cloud on AWS and Google Cloud
without managing infrastructure

Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders