Skip to content

Instantly share code, notes, and snippets.

@mhayes
Created April 11, 2017 19:16
Show Gist options
  • Save mhayes/28ec0d2e05920e2f8752683bee81d1d1 to your computer and use it in GitHub Desktop.
Save mhayes/28ec0d2e05920e2f8752683bee81d1d1 to your computer and use it in GitHub Desktop.
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