How do you restart all Tasks of a Service?

67,664

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

I'm expanding on @user326608's answer above (thanks for the insight!).

This will restart ALL TASKS FOR ALL SERVICES FOR A CLUSTER by stopping all of its tasks. Each service will then automatically launch X number of new tasks, where X is the service's desired task count.

#!/bin/bash

index=0
taskArn=$(aws ecs list-tasks --cluster ${CLUSTER_NAME} --query "taskArns[${index}]" --output text)

until [ "$taskArn" = "None" ]
do 
  aws ecs stop-task --cluster ${CLUSTER_NAME} --task $taskArn
  ((index++))
  taskArn=$(aws ecs list-tasks --cluster ${CLUSTER_NAME} --query "taskArns[${index}]" --output text)
done

Solution 5

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.

Share:
67,664

Related videos on Youtube

Dennkster
Author by

Dennkster

Updated on September 18, 2022

Comments

  • Dennkster
    Dennkster over 1 year

    We have a task that loads some configuration files from an external data source. After the settings are uploaded we would like to be able to restart all the tasks in a service so that the settings propagate to all instances.

    What's the best way to restart all services?

    We have a 'workaround' that involves setting the 'number of tasks' to 0 and then back up, but this is definitely not how it's supposed to be done and has downtime.

    • Matt
      Matt almost 9 years
      Does this document from Amazon explain the workaround that you are currently using?
  • geerlingguy
    geerlingguy almost 7 years
    I needed to do this via the AWS Console, and this is the easiest way—you can manage the whole process manually if you need to. Helpful when you need to quickly relaunch all the tasks and don't have something more robust set up for the process — in the UI, go to the Task definition, create a new revision, update the service, then after a little time all the Tasks are relaunched!
  • Josh Vickery
    Josh Vickery about 6 years
    They've added a checkbox to the service update "Force new deployment" which lets you skip step 1 in your process.
  • ecbrodie
    ecbrodie almost 6 years
    The comment about the "Forces new deployment" option was the Accepted Answer for myself.
  • nroose
    nroose over 5 years
    That second one seems to do something other than start new instances.
  • sudo soul
    sudo soul almost 5 years
    Note: If you want to restart tasks for a single service, simply force a new deployment as @Ben Whaley has described.
  • Zulhilmi Zainudin
    Zulhilmi Zainudin almost 4 years
    As of 24th June 2020, the command should be aws ecs update-service --force-new-deployment --cluster CLUSTER_NAME --service SERVICE_NAME
  • Ben Whaley
    Ben Whaley over 3 years
    Thanks for the note! Amended the answer accordingly.