Skip to content

Instantly share code, notes, and snippets.

@aalbertson
Forked from vgeshel/function.js
Last active December 4, 2017 12:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aalbertson/7592eada9280c03b325a to your computer and use it in GitHub Desktop.
Save aalbertson/7592eada9280c03b325a to your computer and use it in GitHub Desktop.
AWS Lambda function for forwarding SNS notifications to Slack
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'};
slack_notify = '<!channel>' // notification control: !channel = @channel, !here = @here, etc...
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);
});
var full_message = JSON.stringify({text: slack_notify + JSON.stringify(rec.Sns.Message, null, ' ')});
console.log (full_message);
req.write(full_message);
req.end();
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment