I have a python boto3 script that does the ff:
- create a list of tasks with 'RUNNING' status for a service via
ecs_client.list_tasks(cluster=mycluster,serviceName=myservice,desiredStatus='RUNNING')
- do a for loop for the list of tasks above and stop each via
ecs_client.stop_task(cluster=mycluster,task=mytask)
- describe the service to get the runningCount and desiredCount
ecs_client.describe_services(cluster=mycluster,services=[myservice])
- while loop if runningCount < desiredCount -- meaning a task is currently being stopped and has not been replaced yet, so don't stop the next task yet!
while myservice['services'][0]['runningCount'] < myservice['services'][0]['desiredCount']:
If the while loop is not true anymore -- meaning both running and desired counts are equal, stop the next task in the list.
This is the actual flow and I am unable to show the actual code as I'm still employed by my current job and all my code belong to them :)