3

I'm trying to write a script thats stops all services in an existing cluster. To accomplish this, ECS services must be scaled down to desired state of 0 tasks So far, I managed to do this for a single service via a simple shell script example:

 aws ecs update-service --cluster ${ecsClusterName} --service ${nameService} --task-definition ${taskDefinition} --desired-count 0

the problem I'm facing: how can I list all task's definitions:version linked to a giving service in a cluster and set the desired state to 0 in a loop

kenlukas
  • 105
rbsd
  • 31

1 Answers1

3
  1. List services:

    aws ecs list-services --cluster ${ecsClusterName}
    
  2. Write services in a file:

    aws ecs list-services --cluster ${ecsClusterName} | awk -F'"' '{print $2}' | rev | awk -F'/' '{print $1}' | rev | grep -v serviceArns > services.txt
    
  3. Scale down:

    for service in $(cat services.txt); do aws ecs update-service --cluster ${ecsClusterName} --service "$service" --desired-count 0 --no-cli-pager > /dev/null; done