列出所有使用Boto3的“活动” EMR群集

时间:2019-02-13 19:18:24

标签: python amazon-web-services boto3

我正在尝试使用boto3列出EMR上的所有活动集群,但是我的代码似乎无法正常工作,它只会返回null。

我正在尝试使用boto3做到这一点

1)列出所有活动的EMR群集

aws emr list-clusters --active

2)仅列出集群ID和活动集群的名称 集群名称

aws emr list-clusters --active --query "Clusters[*].{Name:Name}" --output text

集群ID

aws emr list-clusters --active --query "Clusters[*].{ClusterId:Id}" --output text

但是我在使用boto3的开始阶段受阻

import boto3
client = boto3.client("emr")
response = client.list_clusters(
    ClusterStates=[
        'STARTING',
    ],
)

print response

任何建议如何将这些CLI命令转换为boto3

谢谢

2 个答案:

答案 0 :(得分:0)

以下代码可以显示活动的emr名称和ID:

import boto3
client = boto3.client("emr")
response = client.list_clusters(
    ClusterStates=[
        'STARTING', 'BOOTSTRAPPING', 'RUNNING', 'WAITING', 'TERMINATING'
    ]
)
for cluster in response['Clusters']:
    print(cluster['Name'])
    print(cluster['Id'])

答案 1 :(得分:0)

稍微更新@shifu.zheng 的回答:

import boto3
client = boto3.client("emr",region_name = "us-west-1", aws_access_key_id = "sdufashdifos123121", aws_secret_access_key ="sjdfnsaldfoasd1231312")
response = client.list_clusters(ClusterStates=['STARTING', 'BOOTSTRAPPING', 'RUNNING', 'WAITING', 'TERMINATING'])
for cluster in response['Clusters']:
print(cluster['Name'])
print(cluster['Id'])

您需要在 boto3.client 对象中有所需的参数。