Skip to content

Instantly share code, notes, and snippets.

@allanlei
Created August 31, 2015 03:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allanlei/6c8306077561611bc700 to your computer and use it in GitHub Desktop.
Save allanlei/6c8306077561611bc700 to your computer and use it in GitHub Desktop.
AWS Lambda Slack notification via SNS events
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/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';
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('Webhook successful');
} else {
context.fail('status code: ' + res.statusCode);
}
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
context.fail(e.message);
});
req.write(JSON.stringify({
text: JSON.stringify(rec.Sns.Message, null, ' '),
}));
req.end();
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment