-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (46 loc) · 1.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const https = require('https');
const url = require('url');
function processSns(context, sns) {
console.log(sns);
const slack_url = process.env["SLACK_URL"];
const slack_channel = process.env["SLACK_CHANNEL"];
const params = url.parse(slack_url);
params.method = "POST";
params.headers = {
"Content-Type": "application/json"
};
const req = https.request(params, function (res) {
if (res.statusCode === 200) {
context.succeed('posted to slack: ' + slack_channel);
} else {
context.fail('status code: ' + res.statusCode);
}
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
context.fail(e.message);
});
const message = JSON.parse(sns.Message);
var status = message.NewStateValue;
if (status === "ALARM") {
status = ":exclamation: " + status;
} else if (status === "OK") {
status = ":+1: " + status;
}
var str = "*" +status +"*\n" +
"Name: " + message.AlarmName + "\n" +
"Description: " + message.AlarmDescription +"\n" +
"Reason: " + message.NewStateReason;
req.write(JSON.stringify({
channel: slack_channel,
text: str
}));
req.end();
}
exports.handler = function(event, context) {
(event.Records || []).forEach(function (rec) {
if (rec.Sns) {
processSns(context, rec.Sns);
}
});
};