Find aws ecs latest task and image used
Asked Answered
B

3

6

I trying to find out the latest task and image used for ECS container service. I tried using AWS ECS cli, able to list services/tasks running but not able to find out running latest task ID and image used for that task. Can someone help with AWS CLI or Boto script to find out latest task and image used.

Backman answered 26/2, 2018 at 13:11 Comment(1)
aws ecs describe-services --service <name> --cluster <name> --query services[].taskDefinition With this I can see the task defination.Backman
E
5

Can you do something like this (not tested)?

aws ecs describe-services \
--service <name> \
--query "reverse(sort_by(services, &CreatedAt)).taskDefinition[0] 

The idea is to list services in your cluster by reverse creation date, and get the first taskDefinition returned.

I have no active services in my account so can't currently test this out, but the logic should hold

Exhibitionism answered 2/3, 2018 at 17:38 Comment(1)
Changing the query slightly to "reverse(sort_by(services, &createdAt))[0].taskDefinition" worked for meAntilogarithm
F
10

Today, I needed make this but in a task without service. And, to my surprise the command:

aws ecs describe-task-definition --task-definition task-definition-name

return a json with the last task definition.

So, I made this to take the name from json:

aws ecs describe-task-definition --task-definition task-definition-name | jq '.[] | .taskDefinitionArn'
Fairweather answered 25/6, 2020 at 16:30 Comment(1)
It's great because it's simple and it works, but is that somewhere documented ?Donatist
E
5

Can you do something like this (not tested)?

aws ecs describe-services \
--service <name> \
--query "reverse(sort_by(services, &CreatedAt)).taskDefinition[0] 

The idea is to list services in your cluster by reverse creation date, and get the first taskDefinition returned.

I have no active services in my account so can't currently test this out, but the logic should hold

Exhibitionism answered 2/3, 2018 at 17:38 Comment(1)
Changing the query slightly to "reverse(sort_by(services, &createdAt))[0].taskDefinition" worked for meAntilogarithm
H
5

Accepted answer didn't work for me, I needed to get latest Task Definition ARN.

Th regular aws cli command

provides output as:

     aws ecs list-task-definitions --status active
{
    "taskDefinitionArns": [
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/diag:1",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/diag:2",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/diag:3",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/diag:4",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/diag:5",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/diag:6",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/diag:7",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/diag:8",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/diag:9",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/diag:10",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/diag:11",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/diag:12",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/diag:13",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/diag:14",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kafka:1",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kafka:2",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kafka:3",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kafka:4",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kafka:5",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kafka:6",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kafka:7",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/mongo-dev:1",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/mongo-dev:2",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/mongo-dev:3",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/mongo:1",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/mongo:2",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/mongo:3",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/mongo:4",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/mongo:5",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/mongo:6",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/mongo:7",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/sqlserver:1",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/sqlserver:2",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/sqlserver:3",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/sqlserver:4",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/sqlserver:5",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/sqlserver:6",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:1",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:2",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:3",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:4",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:5",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:6",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:7",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:8",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:9",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:10",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:11",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:12",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:13",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:14",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:15",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:16",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:17",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:18",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:19",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:20",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:21",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/web-api:22",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/apm:1",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/apm:15",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/apm:17",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/apm:18",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/elasticsearch:1",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/elasticsearch:2",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/elasticsearch:3",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/elasticsearch:4",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/elasticsearch:5",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/elasticsearch:6",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/elasticsearch:7",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/filebeat:1",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/filebeat:2",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/filebeat:3",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/hadoop-single:1",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/hadoop-single:2",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/hadoop-single:3",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/hadoop-single:4",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/hadoop-single:5",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kibana:1",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kibana:2",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kibana:3",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kibana:4",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kibana:5",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kibana:6",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kibana:7",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kibana:8",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kibana:9",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kibana:10",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kibana:11",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kibana:12",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kibana:13",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/kibana:14",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:1",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:2",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:3",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:4",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:5",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:6",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:7",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:8",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:9",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:10",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:11",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:12",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:13",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:14",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:15",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:16",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:17",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:18",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:19",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:20",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:21",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/logstash:23",
        "arn:aws:ecs:eu-north-1:nnnnnnnnnnnn:task-definition/metricbeat:1"
    ]
}

even filter --status active does not help, as it's on by default.

So digging more a little bit got me with this command, which provides clean latest working aws task definition version arn:

aws ecs describe-services --cluster ecs-cluster --service task1 \
--query "services[].taskDefinition" | jq -r ".[0]"

arn:aws:ecs:eu-central-1:nnnnnnnnnnnn:task-definition/task1:22
Harmonic answered 16/10, 2019 at 10:44 Comment(1)
add --output text for no jqEdo

© 2022 - 2024 — McMap. All rights reserved.