如何使用 AWS Lambda 和 Python 获取 EMR 集群的标签列表
本文介绍如何通过 boto3 的 describe_cluster 方法在 AWS Lambda 中高效获取 Amazon EMR 集群的全部标签,替代不存在的 get_tags 接口,并提供可直接部署的示例代码与关键注意事项。 本文介绍如何通过 boto3 的 `describe_cluster` 方法在 aws lambda 中高效获取 amazon emr 集群的全部标签,替代不存在的 `get_tags` 接口,并提供可直接部署的示例代码与关键注意事项。Amazon EMR 并未提供类似 S3 的 get_bucket_tagging 这样专用于标签查询的独立 API,但其集群元数据中完整包含了标签信息。核心方法是调用 describe_cluster —— 该接口不仅返回集群状态、配置和硬件信息,还将所有用户定义的标签以结构化形式嵌套在 response['Cluster']['Tags'] 中,是当前唯一官方支持且稳定的标签获取方式。以下是一个可在 AWS Lambda 中直接运行的完整 Python 示例(基于 boto3):import boto3import jsondef lambda_handler(event, context): # 从事件或环境变量中获取集群 ID(推荐通过 event 传入,提升灵活性与安全性) cluster_id = event.get('ClusterId') if not cluster_id: return { 'statusCode': 400, 'body': json.dumps({'error': 'Missing required parameter: ClusterId'}) } try: emr_client = boto3.client('emr', region_name='us-east-1') # 请根据实际区域调整 response = emr_client.describe_cluster(ClusterId=cluster_id) # 提取标签列表(格式为 [{'Key': 'Name', 'Value': 'prod-emr'}, ...]) tags = response['Cluster'].get('Tags', []) return { 'statusCode': 200, 'body': json.dumps({ 'ClusterId': cluster_id, 'Tags': tags, 'TagCount': len(tags) }) } except emr_client.exceptions.ClusterNotFound: return { 'statusCode': 404, 'body': json.dumps({'error': f'EMR cluster {cluster_id} not found'}) } except Exception as e: return { 'statusCode': 500, 'body': json.dumps({'error': str(e)}) }? 关键注意事项: Adobe Image Background Remover Adobe推出的图片背景移除工具
更多推荐

所有评论(0)