Sunday, February 27, 2022

[SOLVED] Stopping task on AWS ECS via CLI (program output as argument input bash)

Issue

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.


Solution

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]


Answered By - nathanpeck
Answer Checked By - Marie Seifert (WPSolving Admin)