assertible / lambda-cloudwatch-slack

Send AWS CloudWatch notifications to a Slack channel using Lambda
https://assertible.com/blog/npm-package-lambda-cloudwatch-slack
MIT License
481 stars 254 forks source link

Add test for SNS event w/ no identifying event info #20

Closed creichert closed 7 years ago

creichert commented 7 years ago

This will be used to fix #19 by adding a "catch all" event processor which will propogate the JSON values to Slack, as opposed to failing with No matching processor for event.

CodyReichert commented 7 years ago

Awesome, we should be able to handle it. I think something like this will work (untested):

var handleCatchAll = function(event, context) {
    var record = event.Records[0]

    var subject = record.Sns.Subject
    var timestamp = new Date(record.Sns.Timestamp).getTime() / 1000;
    var message = JSON.parse(record.Sns.message)

    // TODO - check for warning here as well
    var color = message.NewStateValue === "ALARM" ? "red" : "green"

    // Add all of the values from the event message to the Slack message description
    var description = ""
    for(key in message) {
        description = key + ": " + message[key] + "<br/>"
    }

    var slackMessage = {
        text: "*" + subject + "*",
        attachments: [
          {
            "color": color,
            "fields": [
              { "title": "Message", "value": message.Sns.Subject, "short": false },
              { "title": "Description", "value": description, "short": false }
            ],
            "ts": timestamp
          }
        ]
    }
}

Notice how it doesn't check for any specific fields in the event, since they could be anything. It just formats the entire message description and adds it to the Slack message.

I'll try and test this in a bit and see if it works (prob needs a couple fixes).