繁体   English   中英

在带有 SNS 和 EMR 客户端的 lambda 中使用 boto3 来获取集群的详细信息并将其发送到邮件

[英]Using boto3 in lambda with SNS & EMR client to fetch the details of cluster and send it over to the mail

我可以使用以下代码中的 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 循环,以便只执行一次。 例子 :

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 循环之外。 您可能还需要将每个集群的消息聚合为单个复合消息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM