当启动类型为 Fargate 时,如何使用 boto3 客户端列出 ecs 任务?
How to list ecs tasks using boto3 client when launch type is Fargate?
我正在尝试使用 boto3 客户端从集群(启动类型 Fargate)获取任务 arns 列表。
如果启动类型是 EC2 那么这有效:
ecs = boto3.client('ecs')
ecs.list_tasks(
cluster='cluster_name',
containerInstance='container_instance_arn',
)
但是当启动类型是 fargate 时,没有容器实例可以提供给函数。
我只尝试使用集群名称
ecs = boto3.client('ecs')
ecs.list_tasks(
cluster='cluster_name'
)
但它随后无法 “not authorized to perform: ecs:ListTasks on resource: *”
当我直接使用 ECS API 时,只需要集群名称。 (启动类型是 Fargate)
aws ecs list-tasks --cluster <cluster_name>
{
"taskArns": [
"arn:aws:ecs:eu-west-1:xxxxxxxxxxxx:task/679ac0fa-107b-4e7c-b630-9d8ae3a1cb8b",
"arn:aws:ecs:eu-west-1:xxxxxxxxxxxx:task/8abe5ea2-6323-46fd-b937-c976f273e517",
"arn:aws:ecs:eu-west-1:xxxxxxxxxxxx:task/98c4e42b-a6a9-4353-b5b9-9ba78f116aa0"
]
}
我如何使用 boto3 获得相同的列表?
编辑:这是我使用的示例策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ecs:ListTasks",
"logs:CreateLogGroup"
],
"Resource": [
"arn:aws:ecs:*:xxxxxxxxxxx:container-instance/*",
"arn:aws:logs:eu-central-1:xxxxxxxxxxx:*"
]
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:eu-central-1:xxxxxxxxxxx:log-group:/aws/lambda/aTestFunction:*"
}
]
}
您正在使用的命令:
ecs.list_tasks(cluster='cluster_name')
正确。但是您得到的错误是:
not authorized to perform: ecs:ListTasks on resource: *
表示无论您使用哪个 IAM user/role 运行,都没有 ecs:ListTasks
的权限。因此,您必须仔细检查哪个 user/role 用于执行您的脚本。
错误信息直接告诉了答案
“not authorized to perform: ecs:ListTasks on resource: *”
需要的是将此声明添加到策略中:
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "ecs:ListTasks",
"Resource": "*"
}
感谢@Marcin 为我指出正确的方向。
我正在尝试使用 boto3 客户端从集群(启动类型 Fargate)获取任务 arns 列表。
如果启动类型是 EC2 那么这有效:
ecs = boto3.client('ecs')
ecs.list_tasks(
cluster='cluster_name',
containerInstance='container_instance_arn',
)
但是当启动类型是 fargate 时,没有容器实例可以提供给函数。 我只尝试使用集群名称
ecs = boto3.client('ecs')
ecs.list_tasks(
cluster='cluster_name'
)
但它随后无法 “not authorized to perform: ecs:ListTasks on resource: *”
当我直接使用 ECS API 时,只需要集群名称。 (启动类型是 Fargate)
aws ecs list-tasks --cluster <cluster_name>
{
"taskArns": [
"arn:aws:ecs:eu-west-1:xxxxxxxxxxxx:task/679ac0fa-107b-4e7c-b630-9d8ae3a1cb8b",
"arn:aws:ecs:eu-west-1:xxxxxxxxxxxx:task/8abe5ea2-6323-46fd-b937-c976f273e517",
"arn:aws:ecs:eu-west-1:xxxxxxxxxxxx:task/98c4e42b-a6a9-4353-b5b9-9ba78f116aa0"
]
}
我如何使用 boto3 获得相同的列表?
编辑:这是我使用的示例策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ecs:ListTasks",
"logs:CreateLogGroup"
],
"Resource": [
"arn:aws:ecs:*:xxxxxxxxxxx:container-instance/*",
"arn:aws:logs:eu-central-1:xxxxxxxxxxx:*"
]
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:eu-central-1:xxxxxxxxxxx:log-group:/aws/lambda/aTestFunction:*"
}
]
}
您正在使用的命令:
ecs.list_tasks(cluster='cluster_name')
正确。但是您得到的错误是:
not authorized to perform: ecs:ListTasks on resource: *
表示无论您使用哪个 IAM user/role 运行,都没有 ecs:ListTasks
的权限。因此,您必须仔细检查哪个 user/role 用于执行您的脚本。
错误信息直接告诉了答案
“not authorized to perform: ecs:ListTasks on resource: *”
需要的是将此声明添加到策略中:
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "ecs:ListTasks",
"Resource": "*"
}
感谢@Marcin 为我指出正确的方向。