aws,

Restart ECS tasks with --force-new-deployment

Jul 14, 2022 · 1 min read · Post a comment

If you want to update your currently running ECS tasks and launch new ones, there is a cool option --force-new-deployment which will start a new deployment of the service and recycle all the tasks. It’s very useful if you need to refresh the containers and use some updated configurations.

Prerequisites

  • ECS
  • AWS CLI
  • sudo privileges

Force new deployment for a service in a specific cluster

It will stop all the running tasks and launch new ones within a service running in a specific cluster.

aws ecs update-service --cluster {ClusterName} --service {ServiceName} --force-new-deployment

Example:

aws ecs update-service --cluster dev --service devcoops --force-new-deployment

Force new deployment for all services in all clusters

There is no example in the AWS documentation for this requirement. To achieve it, I’ve created a bash script that will list all running Clusters, put them in a list and get all the services from that cluster, so we can force a new service deployment and restart the tasks within the service.

!/usr/bin/env bash

clusters=( $(aws ecs list-clusters | jq -r '."clusterArns" | .[]' | sed 's:.*/::' | awk '{print}' ORS=' ') )

for c in "${clusters[@]}"; do
  aws ecs update-service --cluster $c --service $(echo "`aws ecs list-services --cluster $c | jq -r '."serviceArns" | .[]' | sed 's:.*/::' | awk '{print}' ORS=' '`") --force-new-deployment
done

Conclusion

Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.