简体   繁体   中英

How to sort results from `list-tasks` in aws-cli?

I have few tasks running i want to get the latest(task which started last, with last created-at) task arn. I am using this command

aws ecs list-tasks --cluster {cluster} --family {family} --desired-status 'STOPPED'

after exploring a little i got to know about sort_by . I tried

aws ecs list-tasks --cluster {cluster} --family {family} --desired-status 'STOPPED' --query "sort_by(taskArns, &CreatedAt)"

But this gives error

In function sort_by(), invalid type for value: <some_task_arn>, expected one of: ['string', 'number'], received: "null"

Assuming you work from a Linux/Unix-Shell I would suggest sending the output to the sort command. This works by adding

| sort

to your command at the end of the line:

aws ecs list-tasks --cluster {cluster} --family {family} --desired-status 'STOPPED' | sort

I hope this helps.

list-tasks will not give you that information. Have to use both list-tasks and describe-tasks .

I will provide example on my cluster , with RUNNING state tasks. You will have to adjust it to your need .

1. Get the list of tasks

task_arns=$(aws ecs list-tasks --cluster ${cluster[Name]} \
               --desired-status 'RUNNING' \
               --query 'taskArns' --output text)

echo ${task_arns}

Should give a list of Arns of your tasks, eg:

arn:aws:ecs:us-east-1:275795381673:task/0053c603-a6c9-4044-89f5-b0edc8f6de3f arn:aws:ecs:us-east-1:275795381673:task/0b4626ea-0f2b-4c99-9e90-010e8a0c8ad3 arn:aws:ecs:us-east-1:275795381673:task/0d4aa5f2-f547-45ad-b1f8-ed84ef1d678c arn:aws:ecs:us-east-1:275795381673:task/190a320f-4b68-497a-921e-439460447d45 arn:aws:ecs:us-east-1:275795381673:task/c979f4a2-3665-4c56-93c6-e9b88f6b3519

2. Get sorted tasks Arns

aws ecs describe-tasks --cluster ${cluster[Name]} \
    --tasks  ${task_arns} \
    --query "tasks[] | reverse(sort_by(@, &createdAt)) | [].[createdAt,taskArn]" \
    --output table

Should give, eg:

----------------------------------------------------------------------------------------------------
|                                           DescribeTasks                                          |
+----------------+---------------------------------------------------------------------------------+
|  1589888493.15 |  arn:aws:ecs:us-east-1:275795381673:task/c979f4a2-3665-4c56-93c6-e9b88f6b3519   |
|  1589888501.348|  arn:aws:ecs:us-east-1:275795381673:task/190a320f-4b68-497a-921e-439460447d45   |
|  1589888499.438|  arn:aws:ecs:us-east-1:275795381673:task/0d4aa5f2-f547-45ad-b1f8-ed84ef1d678c   |
|  1589888500.312|  arn:aws:ecs:us-east-1:275795381673:task/0b4626ea-0f2b-4c99-9e90-010e8a0c8ad3   |
|  1589888497.701|  arn:aws:ecs:us-east-1:275795381673:task/0053c603-a6c9-4044-89f5-b0edc8f6de3f   |
+----------------+---------------------------------------------------------------------------------+

3. Get last element sorted tasks Arns

aws ecs describe-tasks --cluster ${cluster[Name]} \
    --tasks  ${task_arns} \
    --query "tasks[] | reverse(sort_by(@, &createdAt)) | [-1].[taskArn]" \
    --output text

Should give:

arn:aws:ecs:us-east-1:275795381673:task/0053c603-a6c9-4044-89f5-b0edc8f6de3f

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM