How do you restart all Tasks of a Service?

Solution 1:

Using the AWS CLI tool:

aws ecs update-service --force-new-deployment --service my-service --cluster cluster-name

Solution 2:

What you're wanting to do is essentially the same as redeploying the Service.

To redeploy the service without downtime:

  1. Register a new Task Definition based on the current Task Definition (with the same details)
  2. Call UpdateService, associating the existing Service with the new Task Definition.

This should launch new Tasks for the new Task Definition and then kill the old Tasks for the old Task Definition, effectively restarting the tasks without downtime.

See: UpdateService

Solution 3:

this worked for me:

aws ecs list-tasks --cluster <cluster_name> | jq -r ".taskArns[]" | awk '{print "aws ecs stop-task --cluster <cluster_name> --task \""$0"\""}' | sh

the tasks then recreate on the same instances.

if you need new instances, then use this:

aws ecs list-services --cluster <cluster_name> | jq -r ".serviceArns[]" | awk '{print "aws ecs update-service --cluster <cluster_name> --force-new-deployment  --service \""$0"\""}' | sh

Solution 4:

Task as building block of ECS can be stopped by StopTask call. Service is composed of underlying tasks which may be stopped with same API call. Only missing part here is foreach around results from ListTasks call with defined family parameter. I wrote simple Lambda function which can help you with this.