简体   繁体   English

如何更改cloudwatch SNS电子邮件?

[英]How to change cloudwatch SNS emails?

Using this tutorial I created a lambda function, when it fails a cloud watch alarm causes SNS to send out emails using the lambda errors metric. 通过本教程,我创建了一个lambda函数,当该函数失败时,云监视警报会导致SNS使用lambda错误度量标准发送电子邮件。 I'm using it to check if any ec2 instances have upcomming scheduled events . 我用它来检查是否有任何ec2实例有即将到来的预定事件 Right now this is the info CloudWatch and SNS sends in its emails: 现在,这是CloudWatch和SNS在其电子邮件中发送的信息:

Alarm Details:
- Name:                       ec2-scheduled-events-alarm
- Description:                an ec2 instance has an upcomming scheduled event
- State Change:               OK -> ALARM
- Reason for State Change:    Threshold Crossed: 1 datapoint (1.0) was greater than or equal to the threshold (1.0).
- Timestamp:                  Wednesday 12 September, 2016 00:16:54 UTC
- AWS Account:                ..........

Threshold:
- The alarm is in the ALARM state when the metric is GreaterThanOrEqualToThreshold 1.0 for 300 seconds. 

Monitored Metric:
- MetricNamespace:            AWS/Lambda
- MetricName:                 Errors
- Dimensions:                 
- Period:                     300 seconds
- Statistic:                  Sum
- Unit:                       not specified

State Change Actions:
- OK: 
- ALARM: [arn:aws:sns:us-west-2:..........:ec2-scheduled-events]
- INSUFFICIENT_DATA: 

I would like to change this message to also contain info from my lambda script (such as listing out ec2 instances I defined as failing). 我想将此消息更改为也包含来自我的lambda脚本的信息(例如列出我定义为失败的ec2实例)。 How can I do this? 我怎样才能做到这一点? I'm guessing it involves changing the output of the Monitored Metric: - Dimensions: somehow. 我猜想它涉及更改“ Monitored Metric:的输出Monitored Metric: - Dimensions:以某种方式。

Or better yet how can I just have my emails contain the Log output of my lambda function? 或者更好的是,如何让我的电子邮件包含lambda函数的Log输出?

Yes, you may have your lambda function publish your own message, which could be the log information. 是的,您可以让您的lambda函数发布您自己的消息,该消息可以是日志信息。 You didn't mention what language you are using, so I'm just going to present it using python. 您没有提到您使用的是哪种语言,因此我将使用python进行介绍。

from __future__ import print_function
import json
import boto3
import logging
import time
import datetime

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def Publish():
        """
        Send a message to the topic
        """

        sns = boto3.client('sns')
        subject = ''
        message = 'Here is the message'
        # You can likely insert the debugging prints from logger into the 
        # message

        # This will have TargetArn - the arn of the topic
        # Subject and message of the email. There are other parameters as 
        # well. Just check out boto3 sns api

        sns.publish(
            TargetArn='arn:',
            Subject= subject,
            Message= message)#of email
  return

I am uncertain if you would be able to access the information in your lambda log from the default SNS email you showed. 我不确定您是否能够从显示的默认SNS电子邮件中访问lambda日志中的信息。 The option I listed might be your solution. 我列出的选项可能是您的解决方案。 You will need to make a separate SNS topic and subscribe to it as well, so this might be too much overhead for what you are looking for. 您将需要制作一个单独的SNS主题并同时订阅它,因此这对于您寻找的内容可能会产生过多的开销。

I hope this helps! 我希望这有帮助!

( EDIT: I now realize you asked this 9 months ago, so you probably already figured it out, oops.) (编辑:我现在意识到9个月前您问过这个问题,所以,您可能已经知道了,糟糕。)

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

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