12

I'm trying to kill a task in ECS via the CLI.

I can fetch the task name by executing:

aws ecs list-tasks --cluster "my-cluster" --service-name "my-service" | jq .taskArns[0]

which outputs:

"arn:aws:ecs:REGION:ACCOUNT-ID:task/TASK-GUID"

the full ARN of the task as a string (I have a global defaulting output to JSON).

I can kill the task by executing:

aws ecs stop-task --cluster "my-cluster" --task "task-arn"

However when I try and combine it:

aws ecs stop-task --cluster "my-cluster" --task $(aws ecs list-tasks --cluster "my-cluster" --service-name "my-service" | jq .taskArns[0])

I get:

An error occurred (InvalidParameterException) when calling the StopTask operation: taskId longer than 36.

I know this is probably bash program output/argument input interpolation but I've looked that up and cannot get to the bottom of it.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Simon
  • 5,158
  • 8
  • 43
  • 65
  • What is the output of `aws ecs list-tasks --cluster "my-cluster" --service-name "my-service" | jq .taskArns[0]` – 123 Jun 19 '17 at 13:52
  • 1
    Does the `ARN` have any special characters? Can you try the command substitution within quotes as follows: `aws ecs stop-task --cluster "my-cluster" --task "$(aws ecs list-tasks --cluster "my-cluster" --service-name "my-service" | jq .taskArns[0])" ` – Inian Jun 19 '17 at 13:54
  • 1
    Also note that the aws cli essentially has jq built in so a better (simpler) way to query your task arn would be with: `aws ecs list-tasks --cluster "my-cluster" --service "my-service" --output text --query taskArns[0]` – nathanpeck Jun 19 '17 at 13:58
  • @Inian alas that didn't work. Example ARN included. – Simon Jun 19 '17 at 14:04
  • @Simon What does `echo "$(aws ecs list-tasks --cluster "my-cluster" --service-name "my-service" | jq .taskArns[0])"` output? – 123 Jun 19 '17 at 14:36
  • @123 it outputs the ARN as a quoted string. Exactly as the original command outputs. – Simon Jun 19 '17 at 14:45
  • @Simon Is it possible the quotes are what are causing a problem? Does `aws ecs stop-task --cluster "my-cluster" --task '"arn:aws:ecs:REGION:ACCOUNT-ID:task/TASK-GUID"'` give the same error? Note the outside single quotes. – 123 Jun 19 '17 at 14:47
  • @Simon, instead of `$()`, can you try backticks: aws ecs stop-task --cluster "my-cluster" --task \`aws ecs list-tasks --cluster "my-cluster" --service-name "my-service" | jq .taskArns[0\]` – helloV Jun 19 '17 at 15:33
  • @helloV That would make zero difference. – 123 Jun 19 '17 at 19:45
  • @nathanpeck when executing with your solution rather than jq it works. Do you want to submit and it as an answer? I guess the piping between aws and jq within the ticks/$() doesn't work... anyone shine light on this? – Simon Jul 03 '17 at 21:45
  • @Simon Submitted as an answer. :) – nathanpeck Jul 10 '17 at 15:03

4 Answers4

19

AWS cli essentially has jq built in so a better (simpler) way to query your task arn would be with:

aws ecs list-tasks --cluster "my-cluster" --service "my-service" --output text --query taskArns[0]
nathanpeck
  • 4,608
  • 1
  • 20
  • 18
4

Maybe that helps someone:

Killing task with unique task definition name:

OLD_TASK_ID=$(aws ecs list-tasks --cluster ${ecsClusterName} --desired-status RUNNING --family ${nameTaskDefinition} | egrep "task/" | sed -E "s/.*task\/(.*)\"/\1/")
aws ecs stop-task --cluster ${ecsClusterName} --task ${OLD_TASK_ID}

Killing multiple tasks (same task definition name but different task ids):

OLD_TASK_IDS=$(aws ecs list-tasks --cluster ${ecsClusterName} --desired-status RUNNING --family ${nameTaskDefinition} | egrep "task/" | sed -E "s/.*task\/(.*)\"/\1/" | sed -z 's/\n/ /g')
IFS=', ' read -r -a array <<< "$OLD_TASK_IDS"
for element in "${array[@]}"
do
    aws ecs stop-task --cluster ${ecsClusterName} --task ${element}
done
Sam_Ste
  • 334
  • 2
  • 16
0

One-liner command to stop tasks in cluster/service

for taskarn in $(aws ecs list-tasks --cluster ${YOUR_CLUSTER} --service ${YOUR_SERVICE} --desired-status RUNNING --output text --query 'taskArns'); do aws ecs stop-task --cluster ${YOUR_CLUSTER} --task $taskarn; done;
nemanjabu
  • 80
  • 3
0

One-liner version of nathanpecks great answer:

aws ecs stop-task --cluster "my-cluster" --task $(aws ecs list-tasks --cluster "my-cluster" --service "my-service" --output text --query taskArns[0])

Stian
  • 1,221
  • 1
  • 19
  • 26