Skip to content

Instantly share code, notes, and snippets.

@chroju
Created April 16, 2016 13:30
Show Gist options
  • Select an option

  • Save chroju/43abb861045c2c866a0bc7a01804b852 to your computer and use it in GitHub Desktop.

Select an option

Save chroju/43abb861045c2c866a0bc7a01804b852 to your computer and use it in GitHub Desktop.
aws to slack lambda function (node.js)
console.log('Loading function');
const https = require('https');
const url = require('url');
const slack_url = 'https://hooks.slack.com/xxxxx';
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);
});
// この辺で適当にslack postするテキストを調整する.
var message = JSON.parse(rec.Sns.Message);
var status = message.NewStateValue;
if (status === "ALARM") {
status = ":exclamation: " + status;
}
if (status === "OK") {
status = ":+1: " + status;
}
var str = "*" +
status +
": " +
message.AlarmDescription +
"*" +
"\n" +
message.NewStateReason;
req.write(JSON.stringify({text: str}));
req.end();
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment