import boto3 from logging import getLogger logger = getLogger(__name__) ACTIVE_STATES = [ 'BOOTSTRAPPING', '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. """ emr = boto3.client('emr') # List active EMR clusters response = emr.list_clusters(ClusterStates=ACTIVE_STATES) # Check clusters for the specified tag for cluster_summary in response['Clusters']: cluster_id = cluster_summary['Id'] cluster_details = emr.describe_cluster(ClusterId=cluster_id) tags = cluster_details['Cluster']['Tags'] for tag in tags: if tag['Key'] == tag_name: return cluster_id # If no active cluster with the specified tag is found logger.warning("No active EMR cluster found with the specified tag.") return None