【问题标题】:Python Boto3 for ECS Help needed需要用于 ECS 帮助的 Python Boto3
【发布时间】:2020-05-29 10:13:34
【问题描述】:

我正在尝试列出我的 AWS ECS 账户中的所有集群。我有大约 13 个集群正在运行。

下面的代码只打印一个集群,而我想打印所有集群。我可以使用for 循环吗?

下面只打印一个集群:

import boto3
client = boto3.client('ecs')
response = client.list_clusters(
    maxResults=50
)
print(response)

下面的for循环不起作用并抛出错误

import boto3
client = boto3.client('ecs')
for response in client.list_cluster():
    print(response)

任何线索都将受到高度赞赏。

【问题讨论】:

  • 我的猜测是您在 API 中查询错误的 AWS 区域。您的 ECS 集群在哪个区域?您在~/.aws/credentials~/.aws/config 中将哪个区域设置为默认区域?
  • 那就是 us-east-2
  • us-east-2 两者都有吗?
  • 这是我在所有 13 个集群中使用的帐户中唯一的区域
  • 如果你这样做client = boto3.client('ecs', region_name='us-east-2')是否有效?

标签: python python-3.x amazon-web-services boto3 amazon-ecs


【解决方案1】:

如果你查看list_clusters api,响应语法是:

 Response Syntax

{
    'clusterArns': [
        'string',
    ],
    'nextToken': 'string'
}

这意味着您将返回一个 ARNs 列表 ([]),它们是 AWS 中的唯一资源标识符。

使用describe_clusters api 然后获取描述,正如@jordanm 所说:

import boto3
client = boto3.client('ecs', region_name='us-east-2')
clusters = client.list_clusters(
    maxResults=50
)
clusters_arns = clusters['clusterArns']

clusters_descriptions = client.describe_clusters(
    clusters=clusters_arns
)

for cluster in clusters_descriptions['clusters']:
    print(cluster['clusterName'])

结果类似于:

prod_nam
eu_nam
someothercluster

【讨论】:

    猜你喜欢
    • 2020-08-13
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 2014-03-11
    • 2011-08-15
    • 2011-12-13
    • 2014-11-04
    • 1970-01-01
    相关资源
    最近更新 更多