如何使用 Boto 列出所有正在运行的 EMR 集群?

let*_*tsc 5 python amazon-web-services boto3

如何使用 boto 列出我的 aws 帐户中所有正在运行的集群?使用命令行我可以使用它们:

aws emr list-clusters --profile my-profile --region us-west-2 --active
Run Code Online (Sandbox Code Playgroud)

但是我想使用 boto3 做同样的事情。但是,以下代码不返回任何集群:

import boto3

session = boto3.Session(profile_name='my-profile')

client = session.client('emr', region_name= 'us-west-2')

response = client.list_clusters(
    ClusterStates=['RUNNING']
)

print response
Run Code Online (Sandbox Code Playgroud)

结果:

{u'Clusters': [], 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '577f3961-bdc80772f266', 'HTTPHeaders': {'x-amzn-requestid': '577f3961-34e5-11e7-a12a-bdc80772f266', 'date': 'Tue, 09 May 2017 18:28:47 GMT', 'content-length': '15', 'content-type': 'application/x-amz-json-1.1'}}}
Run Code Online (Sandbox Code Playgroud)

Lam*_*nus 8

这是分页器解决方案。

import boto3

boto3 = boto3.session.Session(region_name='ap-northeast-2')
emr = boto3.client('emr')

page_iterator = emr.get_paginator('list_clusters').paginate(
    ClusterStates=['RUNNING','WAITING']
)

for page in page_iterator:
    for item in page['Clusters']:
        print(item['Id'])
Run Code Online (Sandbox Code Playgroud)

结果是

j-21*****
j-3S*****
Run Code Online (Sandbox Code Playgroud)