首页 文章

如何更改cloudwatch SNS电子邮件?

提问于
浏览
0

使用这个tutorial我创建了一个lambda函数,当它失败时, Cloud 监视警报会导致SNS使用lambda errors指标发送电子邮件 . 我正在用它来检查是否有任何ec2实例上调scheduled events . 现在,这是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:

我想将此消息更改为还包含来自我的lambda脚本的信息(例如列出我定义为失败的ec2实例) . 我怎样才能做到这一点?我猜它涉及以某种方式改变 Monitored Metric: - Dimensions: 的输出 .

或者更好的是,我如何让我的电子邮件包含我的lambda函数的日志输出?

1 回答

  • 0

    是的,您可能让lambda函数发布您自己的消息,这可能是日志信息 . 你没有提到你正在使用的语言,所以我只是想用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
    

    我不确定您是否能够从您显示的默认SNS电子邮件中访问lambda日志中的信息 . 我列出的选项可能是您的解决方案 . 您将需要创建一个单独的SNS主题并订阅它,因此这可能是您正在寻找的内容的过多开销 .

    我希望这有帮助!

    (编辑:我现在意识到你在9个月前问过这个问题,所以你可能已经弄明白了,哎呀 . )

相关问题