-
-
Save JeffRausch/15eeec45d36684cdf828 to your computer and use it in GitHub Desktop.
Function to send an AWS CloudWatch alarm message to Slack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('Loading function'); | |
const https = require('https'); | |
const url = require('url'); | |
// to get the slack hook url, go into slack admin and create a new "Incoming Webhook" integration | |
const slack_url = 'https://hooks.slack.com/services/...'; | |
const slack_req_opts = url.parse(slack_url); | |
slack_req_opts.method = 'POST'; | |
slack_req_opts.headers = {'Content-Type': 'application/json'}; | |
exports.handler = function(event, context) { | |
(event.Records || []).forEach(function (rec) { | |
if (rec.Sns) { | |
var req = https.request(slack_req_opts, function (res) { | |
if (res.statusCode === 200) { | |
context.succeed('posted to slack'); | |
} else { | |
context.fail('status code: ' + res.statusCode); | |
} | |
}); | |
req.on('error', function(e) { | |
console.log('problem with request: ' + e.message); | |
context.fail(e.message); | |
}); | |
//Parse the CloudWatch alarm message and only post the StateValue and AlarmName to keep it clean. | |
var messageObj = JSON.parse(rec.Sns.Message); | |
var emoji = ""; | |
switch (messageObj.NewStateValue){ | |
case "ALARM": | |
emoji = ":alarm_clock: "; | |
break; | |
case "OK": | |
emoji = ":white_check_mark: "; | |
break; | |
case "INSUFFICIENT_DATA": | |
emoji = ":information_source: "; | |
break; | |
} | |
req.write(JSON.stringify({text: emoji + messageObj.NewStateValue + ": " + messageObj.AlarmName})); // for testing: , channel: '@vadim' | |
req.end(); | |
} | |
}); | |
}; |
Added emoji based on NewStateValue
Removed double JSON.stringify to prevent quotes from showing up in the Slack message
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Modified the original from vgeshel to parse a CloudWatch alarm message into a more readable format.