Sub topic 15
Mixing Fargate and Fargate Spot
This chapter assumes you can write a Fargate task definition and create an ECS service with the AWS CLI. You should also know what desired count means. It covers capacity providers and how to use Fargate Spot without risking the whole service.
Capacity providers
A capacity provider tells ECS where to get compute for a task. Fargate clusters have two built-in providers:
FARGATEruns tasks on regular on-demand capacity.FARGATE_SPOTruns tasks on spare AWS capacity at a lower price. AWS can reclaim that capacity with a two-minute warning.
A service picks providers through a capacity provider strategy. A strategy replaces --launch-type. You can't set both on the same service.
The strategy fields
Each entry in a strategy has up to three fields:
| Field | Meaning |
|---|---|
| `capacityProvider` | The provider name, such as `FARGATE` or `FARGATE_SPOT`. |
| `base` | The minimum number of tasks to run on this provider. Only one provider in a strategy can have a base. |
| `weight` | The relative share of tasks placed on this provider after the base is met. |
ECS fills the base first. It then splits the remaining tasks in proportion to the weights.
A worked example
The following command creates a service with four tasks:
aws ecs create-service \
--cluster demo \
--service-name worker \
--task-definition web-app \
--desired-count 4 \
--capacity-provider-strategy capacityProvider=FARGATE,base=1,weight=1 capacityProvider=FARGATE_SPOT,weight=3 \
--network-configuration "awsvpcConfiguration={subnets=[subnet-aaa111,subnet-bbb222],securityGroups=[sg-0123456789abcdef0],assignPublicIp=DISABLED}"One task goes to FARGATE because of the base. The remaining three are split 1:3, which gives about 0.75 tasks to FARGATE and 2.25 to FARGATE_SPOT. ECS rounds these, so you end up with two tasks on FARGATE and two on FARGATE_SPOT, or one and three, depending on rounding. As the desired count grows, the split approaches 1:3.
What happens on interruption
When AWS reclaims Spot capacity, ECS sends SIGTERM to the task and then SIGKILL after the stop timeout. The default stopTimeout is 30 seconds. The service scheduler then starts a replacement task to restore the desired count. The base task on FARGATE keeps serving traffic in the meantime. That is the reason for the base.
Common errors
- Setting
--launch-type FARGATEtogether with a strategy. The call fails, so choose one. - Giving a base to both providers. ECS accepts a base on only one.
- Using Spot for a service with a single task. An interruption takes the service down until the replacement starts.
- Forgetting that the cluster must have the providers associated. Fargate clusters created recently include them. Older clusters may need
aws ecs put-cluster-capacity-providers.
Exercise
A service has desired count 10 and the strategy FARGATE base=2 weight=1, FARGATE_SPOT weight=1. Roughly how many tasks run on each provider, and why is the base useful?
Show answer
The base places 2 tasks on FARGATE. The other 8 are split 1:1, so 4 go to each provider. That gives 6 on FARGATE and 4 on FARGATE_SPOT. The base guarantees that at least 2 tasks stay on on-demand capacity if Spot capacity is reclaimed.