Skip to content

Instantly share code, notes, and snippets.

@orlando
Last active November 3, 2016 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orlando/8ee4d138f9e2146f242f2c33f53989cc to your computer and use it in GitHub Desktop.
Save orlando/8ee4d138f9e2146f242f2c33f53989cc to your computer and use it in GitHub Desktop.
lambda sns slack notify function
var slackWebhookPath = '/services/T02N0QWAS/B2N58DH4N/lczo4THnmZvr2sNVMutCGH4P';
var https = require('https');
var util = require('util');
var colorMap = {
'ALARM': 'danger',
'OK': 'good',
'INSUFFICIENT_DATA': 'warning'
};
exports.handler = function(event, context) {
var postData = {
'text': '*' + event.Records[0].Sns.Subject + '*'
};
var message = JSON.parse(event.Records[0].Sns.Message);
var fields = [];
Object.keys(message).forEach(function (key) {
var value = message[key];
if (typeof value === 'object') {
value = JSON.stringify(value);
}
fields.push({
'title': key,
'value': value,
'short': true
});
});
// Grab the color given the Alarm state.
// Default to 'danger'
var color = colorMap[message.NewStateValue] || 'danger';
postData.attachments = [{
'color': color,
'fields': fields
}];
var options = {
method: 'POST',
hostname: 'hooks.slack.com',
port: 443,
path: slackWebhookPath
};
var req = https.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
context.done(null);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write(util.format('%j', postData));
req.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment