Skip to content

Instantly share code, notes, and snippets.

@mhayes mhayes/slack-sns.js
Created Apr 11, 2017

Embed
What would you like to do?
SNS to Slack via Lambda
const https = require("https");
const util = require("util");
const SLACK_WEBHOOK_PATH = process.env.SLACK_WEBHOOK_PATH;
const SLACK_CHANNEL = process.env.SLACK_CHANNEL;
const SLACK_USERNAME = process.env.SLACK_USERNAME || "aws";
const SLACK_EMOJI = process.env.SLACK_EMOJI || ":aws:";
function noop (ret) {
if (ret) {
return ret;
}
}
function notifySlack (payload) {
var options = {
method: "POST",
hostname: "hooks.slack.com",
port: 443,
path: SLACK_WEBHOOK_PATH
};
var req = https.request(options, function (res) {
res.setEncoding("utf8");
res.on("data", noop);
});
req.on("error", function (e) {
console.log("[ERROR] Problem with request: " + e.message);
});
req.write(util.format("%j", payload));
req.end(function () {
console.log("Done.");
});
}
exports.handler = function (event, context, callback) {
(event.Records || []).forEach(function (rec) {
if (rec.Sns) {
notifySlack({
"channel": SLACK_CHANNEL,
"username": SLACK_USERNAME,
"text": "*" + rec.Sns.Subject + "* - " + rec.Sns.Message,
"icon_emoji": SLACK_EMOJI,
"mrkdwn": true
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.