Ideally using ECS Fargate you would have a different tag (i.e. unique version) for each image change, so that when you updated the CloudFormation template the cluster would redeploy automatically with the new image. If you're using the same version number e.g. for a SNAPSHOT version of the image and want to force the cluster to start using the new image, as the other answer to this question noted you can stop the cluster task(s), and when they restart they will automatically pull the latest image from the repository.
If you want to manually stop the task using the CLI, you'll need to first query the cluster for the task ARN, as explained by an answer to another question:
aws ecs list-tasks --cluster my-cluster --service my-service --output text --query taskArns[0]
(Note that this command as written only returns the ARN of the first task.)
Save this in a taskArn
variable in Bash, and then use it with aws ecs stop-task
to stop the task, specifying the task ARN using --task
:
aws ecs stop-task --cluster my-cluster --task $taskArn --output text --query task.lastStatus
I added the --output text
and --query task.lastStatus
to the end to prevent pages of JSON cruft being shown. You may find other information to glean from it.
You can put it all together in a Bash script. Assuming you put $clusterName
, $serviceName
, etc. in variables, it might look like this:
taskArn=$(aws ecs list-tasks --profile $awsProfile --cluster $clusterName --service $serviceName --output text --query taskArns[0])
taskLastStatus=$(aws ecs stop-task --profile $awsProfile --cluster $clusterName --task $taskArn --output text --query task.lastStatus)
echo $clusterName $serviceName $taskArn $taskLastStatus
Note that the specifying --profile
is optional if you're using the default configured profile.