-
-
Save antonioned/a27ae07dca8ff263842e0d57b498ce40 to your computer and use it in GitHub Desktop.
AWS Lambda function for forwarding SNS notifications to Slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = process.env.SLACK_WEBHOOK_URL // add the SLACK_WEBHOOK_URL to the env. variables in the Lambda | |
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('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); | |
}); | |
const message = rec.Sns.Message; | |
const text = message.slice(1, -1); | |
console.log('SLICED', text); | |
req.write(JSON.stringify({text: JSON.stringify(text, null, ' ')})); // send the alert without the " in it | |
//req.write(JSON.stringify({text: JSON.stringify(rec.Sns.Message, null, ' ')})); // for testing: , channel: '@vadim' | |
req.end(); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment