Fargate Spot fallback to On-Demand on Amazon ECS: setup guide
ECS does not replace missing Fargate Spot capacity with On-Demand. A capacity provider strategy with both providers only splits tasks by weight; when Spot has no capacity the Spot share simply does not start, and the service event says "unable to place a task. Reason: Capacity is unavailable at this time". This guide sets up a fallback that fills the gap.
Option 1: On-Demand twin (recommended)
- Keep your service on
FARGATE_SPOTas it is, for exampleweb. - Create a second service in the same cluster from the same task definition, with capacity provider
FARGATE, desired count 0, and the same load balancer target group, subnets and security groups. Call it for exampleweb-ondemand. At 0 tasks it costs nothing. - Tag the twin:
aws ecs tag-resource --resource-arn <twin service ARN> \ --tags key=fallback-managed,value=true key=fallback-for,value=web
- Connect the account here. Within five minutes
webshows "On-Demand twin" under Protection.
When web is short of Spot capacity for longer than your stuck time, the twin's desired count is raised to the number of missing tasks.
When Spot runs them again, the twin is lowered step by step back to 0. Only the twin is ever updated, and only its desired count; your Spot
service, its deployments and its auto scaling are not touched. Do not attach auto scaling to the twin, and keep its task definition in step with
the Spot service in your deploy pipeline.
Option 2: strategy switch (no twin)
Tag the Spot service itself:
aws ecs tag-resource --resource-arn <service ARN> \ --tags key=fallback-managed,value=true key=fallback-mode,value=strategy
When it is stuck, its capacity provider strategy is switched to FARGATE with a new deployment, and switched back after your
probe time to see whether Spot has returned. If not, it falls back again and waits twice as long. This replaces every task of the service on each
switch (a normal rolling deployment), so use it for services where that is fine. The cluster must have the FARGATE capacity provider
associated.
Alert-only
Any other service with a FARGATE_SPOT strategy is watched without tags: you get an alert when it runs short and when it recovers.
With the alert-only CloudFormation template the role is read-only.
What the role can do
Read: ecs:ListClusters, ListServices, DescribeServices, ListTasks, DescribeTasks, ListTagsForResource.
Write: ecs:UpdateService, limited by an IAM condition to services tagged fallback-managed=true. It has no access to task
definitions, environment variables, secrets, logs or tags. Read the template.
Start in dry run
New workspaces are in dry run: nothing is changed, and the audit log and alerts show what would have been done. Turn on automatic fallback in Settings when you are happy with what you see.