Skip to content

Instantly share code, notes, and snippets.

@ldobson
Created September 26, 2016 16:11
Show Gist options
  • Save ldobson/bf7e1923d3a86e97888849bd9ae1e298 to your computer and use it in GitHub Desktop.
Save ldobson/bf7e1923d3a86e97888849bd9ae1e298 to your computer and use it in GitHub Desktop.
SNS Relay for Slack
var https = require('https');
var util = require('util');
exports.handler = function(event, context) {
console.log(JSON.stringify(event, null, 2));
console.log('From SNS:', event.Records[0].Sns.Message);
var subject = event.Records[0].Sns.Subject;
var message = JSON.parse(event.Records[0].Sns.Message);
var color = '#FF0000';
var username = 'AWS CloudWatch';
var channel;
//Get non-Cloudwatch users
if ( subject.indexOf('AWS Config') > -1 ) {
// Ignore messages on delivery of configuration history
if ( subject.indexOf('Configuration History Delivery Completed') > -1 ) {
console.log("Configuration history - not delivering");
context.done();
return;
}
username = 'AWS Config';
color = '#808080';
channel = '#aws-config';
}
if (username == 'AWS CloudWatch') {
channel = '#aws-cloudwatch';
if (message.NewStateValue == 'OK') {
color = '#00FF00';
}
}
var postData = {
'channel': channel,
'username': username,
'text': '*' + subject + '*'
};
postData.attachments = [
{
'color': color,
'text': message.NewStateReason
}
];
var options = {
method: 'POST',
hostname: 'hooks.slack.com',
port: 443,
path: 'SLACK_WEBHOOK_HERE'
};
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