简体   繁体   中英

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. I'm using it to check if any ec2 instances have upcomming scheduled events . Right now this is the info CloudWatch and SNS sends in its emails:

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). How can I do this? I'm guessing it involves changing the output of the Monitored Metric: - Dimensions: somehow.

Or better yet how can I just have my emails contain the Log output of my lambda function?

Yes, you may have your lambda function publish your own message, which could be the log information. You didn't mention what language you are using, so I'm just going to present it using 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. 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.

I hope this helps!

( EDIT: I now realize you asked this 9 months ago, so you probably already figured it out, oops.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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