首页 > 解决方案 > 使用带有 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
                      )

标签: pythonpython-3.xpython-2.7amazon-web-servicesboto3

解决方案


是的,将 SNS 发布语句移到 for 循环之外。您可能还需要将每个集群的消息聚合成单个复合消息


推荐阅读