python - 使用带有 SNS 和 EMR 客户端的 lambda 中的 boto3 来获取集群的详细信息并将其发送到邮件
问题描述
我可以在下面的代码中使用 SNS 服务获取输出并通过电子邮件发送。但是,它一次为 1 个集群输出发送邮件,而不是在 1 封邮件中发送所有集群详细信息。
导入 boto3 从日期时间导入 json 导入 timedelta
地区 = 'us-east-1'
Topic_Arn = "arn:aws:sns:us-east-1:000000:testlog"
emrclient = boto3.client('emr', region_name=REGION) snsclient = boto3.client('sns', region_name=REGION)
def lambda_handler(event, context): EMRS = emrclient.list_clusters( ClusterStates = ['STARTING', 'RUNNING', 'WAITING'] )
clusters = EMRS["Clusters"]
for cluster_details in clusters :
id = cluster_details.get("Id")
describe_cluster = emrclient.describe_cluster(
ClusterId = id
)
cluster_values = describe_cluster["Cluster"]
name = cluster_values.get("Name")
tag_val = cluster_values.get("Tags")
Instancehours = cluster_values.get("NormalizedInstanceHours")
emr_ig = emrclient.list_instance_groups(
ClusterId = id
)
emrid = emr_ig["InstanceGroups"]
for item in emrid :
purchase_type = item.get("Market")
instancegroup_id = item.get("Id")
instancegroup_type = item.get("InstanceGroupType")
status = item.get("Status")
state = status.get("State")
timeline = status.get("Timeline")
autoscaling = item.get("AutoScalingPolicy", None)
#autoscaling_status = autoscaling.get("Status")
#autoscaling_state = autoscaling_status.get("State")
create_date_time = timeline.get("CreationDateTime")
ready_date_time = timeline.get("ReadyDateTime")
emrdetails = "Cluster_ID = " + id + "," + "status_of_cluster = " + state + "," + " Instance_Group = " + instancegroup_type + "," + " Market = " + purchase_type + "," + " CreationDateTime = " + str(create_date_time) + "," + " NormalizedInstanceHours = " + str(Instancehours) + "," + " Autosacle = " + str(autoscaling)
emr_status_list = []
emr_status_list.append(emrdetails)
emrStatusCheck = []
for emr_status in emr_status_list :
if ((emr_status.split(",")[3]).split("=")[1].strip() == str("ON_DEMAND") and (emr_status.split(",")[2]).split("=")[1].strip() == str("CORE") and (emr_status.split(",")[6]).split("=")[1].strip() == str("None")):
emrStatusCheck.append(emr_status)
print(emrStatusCheck)
snsclient.publish(
TopicArn = Topic_Arn,
Subject = "EMR Cluster Details",
Message = emrStatusCheck
)
解决方案
是的,将 SNS 发布语句移到 for 循环之外。您可能还需要将每个集群的消息聚合成单个复合消息
推荐阅读
- python-2.7 - ValueError:在 Python 中解包的值太多。这是网络错误吗?
- c++ - 尽管在多个文件中使用 ifndef,但 C++ 多重定义
- c# - 如何设置 WPF 按钮的文本
- r - 渲染大型瓦片数据集
- codeigniter-3 - 有什么办法可以清除缓存并在codeigniter中查看
- javascript - 点击事件大部分时间都不起作用
- linux - 如何在ansible中使用yum模块禁用所有存储库?
- java - 使用 java s3 sdk 列出存储桶(拥有并且我被授予访问权限)
- corda - 带有 corda-node-driver 的 Cordapp 配置文件
- python - ipaddress 没有正确标记 192.0.0.0/24 中的保留 IP?