Skip to content

Instantly share code, notes, and snippets.

@jarosite
Last active April 14, 2016 14:53
Show Gist options
  • Save jarosite/c127f2063a9b3edfad604aaa340ccdfc to your computer and use it in GitHub Desktop.
Save jarosite/c127f2063a9b3edfad604aaa340ccdfc to your computer and use it in GitHub Desktop.
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/XXXX/XXXXX/XXXXXX';
const slack_req_opts = url.parse(slack_url);
const slack_channel = '@user_name'; //or channel name
slack_req_opts.method = 'POST';
slack_req_opts.headers = {'Content-Type': 'application/json'};
exports.handler = function(event, context, callback) {
(event.Records || []).forEach(function (rec) {
if (rec.Sns) {
var req = https.request(slack_req_opts, function (res) {
if (res.statusCode !== 200) {
callback('failed to post message, status code: ' + res.statusCode);
}
});
req.on('error', function(e) {
callback(e);
});
req.write(JSON.stringify(
{
channel: slack_channel,
username: 'AWS',
attachments: [{
fallback: JSON.stringify(rec.Sns.Subject, null, ' '),
pretext: rec.Sns.Subject,
text: rec.Sns.Message
}]
}));
req.end();
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment