Search code examples
amazon-ecsautoscalingaws-fargate

Auto-Scaling removes running task in ECS service (FARGATE)


I am running an ecs service using Fargate on AWS. Each task there completes a single operation and dies (fetching a message from a SQS queue and decode/encode a video file). Now I designed an autoscaling policy like below,

  1. If SQS queue size is more than 5 increment desired count to 1 (repeat every 60 seconds).
  2. If SQS queue size is less than 2 decrement desired count to 1 (repeat every 60 seconds).

But what AWS is doing is than when queue size gets down below 2, it kills out running tasks leaving the corresponding operation "broken". I don't want AWS to kill the running tasks (because they will automatically die within sometime when the command completes) but just to set the desired count to 0 so that the tasks doesn't get "respawned". So literally I want my tasks to be unstoppable during auto-scaling.

How I can achieve this in ECS service and aws_ecs_autoscaling_target. Please note that I am using terraform to provision the service.

Thanks in advance.


Solution

  • I had to solve this issue in a different approach. I had to create a small Lambda function which gets triggered by the cloudwatch alarm and starts a Fargate task using StartTask. This workflow suited well here rather than using autoscaling policy.