import boto.emr ACTIVE_STATES = [ 'BOOTSTRAPING', 'RUNNING', 'STARTING', 'WAITING'] def find_active_emr_cluster_id_by_tag(tag_name): """Find an active emr cluster id from a tag name. Args: tag_name (str): the tag to look for. Return: str: the emr cluster id. """ connector = boto.emr.connection.EmrConnection() cluster_list = connector.list_clusters() if not cluster_list: return None for cluster in cluster_list.clusters: if cluster.status.state not in ACTIVE_STATES: continue # List Cluster returns the list of all the clusters but some # informations such as the tag list is missing. Calling cluster.tag # returns constantly an empty list. For all active clusters, we ask # a more complete description – which containst he tags. tags = connector.describe_cluster(cluster.id).tags if not tags: continue for tag in tags: if tag_name == tag.key: return cluster.id return None