List all “Active” EMR cluster using Boto3

孤人 提交于 2021-01-27 18:52:16

问题


I'm trying to list all active clusters on EMR using boto3 but my code doesn't seem to be working it just returns null.

Im trying to do this using boto3

1) list all Active EMR clusters

aws emr list-clusters --active

2) List only Cluster id's and Names of the Active one's cluster names

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

Cluster id's

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

But i'm blocked in the starting stage of using boto3

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

print response

Any suggestions how can i convert those CLI commands to boto3

Thanks


回答1:


The following codes can print the active emr name and 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'])



回答2:


slightly updated on @shifu.zheng's answer:

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'])

You need to have the required parameters in the boto3.client object.



来源:https://stackoverflow.com/questions/54677831/list-all-active-emr-cluster-using-boto3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!