amazon-web-servicesamazon-ecsaws-fargate

Schedule AWS Fargate to restart tasks every night


Our service has a memory leak issue, which obviously should ideally be corrected, but as a temporary solution we could reboot our Fargate tasks once a day, preferably at night.

I'm aware of ScheduledFargateTask, which allows triggering Fargate tasks at certain moments, but that's not quite what is needed. Rather, we need a continuous task, which gets stopped and therefore Fargate launches a new one in its place.

Is there a straightforward way to tell Fargate to restart tasks?


Solution

  • It sounds like you have an ECS Service, deployed to Fargate, and you need to trigger an ECS task replacement in that Service once a day.

    The easiest way to do this, would be to create a scheduled Lambda function, that uses the AWS SDK to trigger ECS to replace the running task. The actual API call you need to make for this would either be ECS Update Service with forceNewDeployment enabled which will cause the ECS service to do a rolling redployment of the ECS Task. Or, if a few minutes of down-time is acceptable, you could simply call Stop Task on the currently running task, and the ECS service will replace it with a new task after a minute or two.