查找使用的 aws ecs 最新任务和图像

Posted

技术标签:

【中文标题】查找使用的 aws ecs 最新任务和图像【英文标题】:Find aws ecs latest task and image used 【发布时间】:2018-08-05 23:01:38 【问题描述】:

我试图找出用于 ECS 容器服务的最新任务和图像。我尝试使用 AWS ECS cli,能够列出正在运行的服务/任务,但无法找到正在运行的最新任务 ID 和用于该任务的图像。有人可以帮助使用 AWS CLI 或 Boto 脚本来找出最新的任务和使用的图像。

【问题讨论】:

aws ecs describe-services --service --cluster --query services[].taskDefinition 有了这个我可以看到任务定义。 【参考方案1】:

你能做这样的事情吗(未经测试)?

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

这个想法是按反向创建日期列出集群中的服务,并返回第一个taskDefinition

我的帐户中没有活动服务,因此目前无法对此进行测试,但逻辑应该成立

【讨论】:

将查询稍微更改为 "reverse(sort_by(services, &amp;createdAt))[0].taskDefinition" 对我有用【参考方案2】:

接受的答案对我不起作用,我需要获取最新的任务定义 ARN。

常规 aws cli 命令

提供输出为:

     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"
    ]

即使 filter --status active 也无济于事,因为它默认处于启用状态。

因此,我通过这个命令进行了更多挖掘,它提供了干净的最新工作 aws 任务定义版本 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

【讨论】:

添加 --output text 表示没有 jq【参考方案3】:

今天,我需要做这个,但在一项没有服务的任务中。而且,令我惊讶的是命令:

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

返回一个带有最后一个任务定义的 json。

所以,我把这个名字取自 json:

aws ecs describe-task-definition --task-definition task-definition-name | jq '.[] | .taskDefinitionArn'

【讨论】:

以上是关于查找使用的 aws ecs 最新任务和图像的主要内容,如果未能解决你的问题,请参考以下文章

从图像创建 ec2 实例 - 使用 boto 查找内核 id 属性

更新 AWS ECS 服务任务的最佳实践

AWS ECS:ENV 变量超出任务定义 JSON

查找所有没有特定标签的 Amazon AWS 实例

在 AWS ECS 中将带有卷参数的 docker 容器作为任务定义或服务运行

如何在AWS中将Windows EC2实例复制到S3存储桶?