Question:
How do I list all my running clusters in my aws account using boto? Using the the command line I can get them using :
1 2 |
aws emr list-clusters --profile my-profile --region us-west-2 --active |
However I wanna do the same using boto3. However the following code does not return any clusters:
1 2 3 4 5 6 7 8 9 10 11 12 |
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 |
Result:
1 2 |
{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'}}} |
Answer:
Here is the paginator solution.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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']) |
The result is
1 2 3 |
j-21***** j-3S***** |