Search code examples
amazon-web-servicesamazon-ecsaws-cli

How to get the main container only in the ECS describe-tasks query?


I need to get a list of running ECS tasks with their image names/tags.

Trying in 2 steps:

  1. Extracting task ARNs
ARNS=$(aws ecs list-tasks --cluster $CLUSTER_NAME \
  --desired-status 'RUNNING' --query 'taskArns' \
  --output json --profile $PROFILE)
  1. Describing tasks
aws ecs describe-tasks --cluster $CLUSTER_NAME --tasks $ARNS \
  --profile $PROFILE --output table \
  --query "tasks[] | [].[startedAt,containers[0].image]"

The problem is I have multiple sidecar containers in each task, and their order is inconsistent, so containers[0] returns a random container every time.

Example output

-----------------------------------------------------------------------------------------------
|                                        DescribeTasks                                        |
+-----------------------------------+---------------------------------------------------------+
|  2022-08-15T21:01:22.513000-07:00 |  lacework/datacollector:latest-sidecar                  |
|  2022-08-15T21:01:21.511000-07:00 |  lacework/datacollector:latest-sidecar                  |
|  2022-08-15T21:01:22.102000-07:00 |  lacework/datacollector:latest-sidecar                  |
|  2022-08-15T21:01:21.743000-07:00 |  999999999999.dkr.ecr.us-east-1.amazonaws.com/bar:prod  |
|  2022-08-15T21:02:02.298000-07:00 |  999999999999.dkr.ecr.us-east-1.amazonaws.com/bar:prod  |
|  2022-08-15T21:02:31.743000-07:00 |  999999999999.dkr.ecr.us-east-1.amazonaws.com/bar:prod  |
+-----------------------------------+---------------------------------------------------------+

Can I filter the list to keep the primary containers only, or at least sort containers in some consistent way?


Solution

  • A possible solution to that is to query the list by images starting with your ECR account ID.

    I made it work like this:

    aws ecs describe-tasks \
    --cluster yourClusterName \
    --output table \
    --query 'tasks[] | [].[startedAt,containers[?starts_with(image, to_string(`999999999999`))].image]' \
    --tasks `aws ecs list-tasks --desired-status RUNNING --query taskArns --cluster yourClusterName --output text`
    

    Which produces an output like this one:

    ------------------------------------------------------------------------------
    |                                DescribeTasks                               |
    +----------------------------------------------------------------------------+
    |  2022-10-24T17:29:16.003000+02:00                                          |
    |  999999999999.dkr.ecr.us-east-2.amazonaws.com/business:v0.9.1              |
    |  2022-10-19T17:53:46.015000+02:00                                          |
    |  999999999999.dkr.ecr.us-east-2.amazonaws.com/datacore:v0.5.1              |
    |  2022-10-24T17:30:05.670000+02:00                                          |
    |  999999999999.dkr.ecr.us-east-2.amazonaws.com/application:v0.16.2          |
    |  2022-10-24T18:53:31.795000+02:00                                          |
    |  999999999999.dkr.ecr.us-east-2.amazonaws.com/frontend:development-v1.9.7  |
    +----------------------------------------------------------------------------+
    

    I wasn't able to fix the format of the output. JMESPath is not really my thing.