Skip to content

Instantly share code, notes, and snippets.

@ukcloudpro
Created April 23, 2017 10:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ukcloudpro/e918d0371b27432c9100a862bb052b56 to your computer and use it in GitHub Desktop.
Save ukcloudpro/e918d0371b27432c9100a862bb052b56 to your computer and use it in GitHub Desktop.
AWS Lambda node.js code to pass SNS messages to Slack Webhook
var https = require('https');
var util = require('util');
var SLACK_CHANNEL = process.env.SLACK_CHANNEL;
var SLACK_PATH = process.env.SLACK_PATH;
exports.handler = function(event, context) {
console.log(JSON.stringify(event, null, 2));
console.log('From SNS:', event.Records[0].Sns.Message);
var postData = {
"channel": SLACK_CHANNEL,
"username": "AWS SNS",
"text": "*" + event.Records[0].Sns.Subject + "*",
"icon_emoji": ":aws:"
};
var message = event.Records[0].Sns.Message;
var severity = "good";
postData.attachments = [
{
"color": severity,
"text": message
}
];
var options = {
method: 'POST',
hostname: 'hooks.slack.com',
port: 443,
path: SLACK_PATH
};
var req = https.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
context.done(null, postData);
});
});
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