Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
var https = require('https');
var util = require('util');
exports.handler = function(event, context) {
try {
var message = JSON.parse(event.Records[0].Sns.Message);
var eventType = message.Event;
var autoScaleGroupName = message.AutoScalingGroupName;
var description = message.Description;
var cause = message.Cause;
var slackMessage = [
"*Event*: " + eventType,
"*Description*: " + description,
"*Cause*: " + cause,
].join("\n");
var postData = {
channel: "#notifications",
username: "AWS Bot",
text: "*" + autoScaleGroupName + "*",
attachments: [{ text: slackMessage, mrkdwn_in: ["text"] }]
};
var options = {
method: 'POST',
hostname: 'hooks.slack.com',
port: 443,
path: '/services/WEBHOOKURL'
};
var req = https.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
context.done(null);
});
});
req.on('error', function(e) {
context.fail(e);
console.log('problem with request: ' + e.message);
});
req.write(util.format("%j", postData));
req.end();
} catch (e) {
context.fail(e)
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment