Created
April 23, 2017 10:26
-
-
Save ukcloudpro/e918d0371b27432c9100a862bb052b56 to your computer and use it in GitHub Desktop.
AWS Lambda node.js code to pass SNS messages to Slack Webhook
This file contains hidden or 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
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