MCPcopy Create free account
hub / github.com/assertible/lambda-cloudwatch-slack / handleCloudWatch

Function handleCloudWatch

index.js:230–281  ·  view source on GitHub ↗
(event, context)

Source from the content-addressed store, hash-verified

228};
229
230var handleCloudWatch = function(event, context) {
231 var timestamp = (new Date(event.Records[0].Sns.Timestamp)).getTime()/1000;
232 var message = JSON.parse(event.Records[0].Sns.Message);
233 var region = event.Records[0].EventSubscriptionArn.split(":")[3];
234 var subject = "AWS CloudWatch Notification";
235 var alarmName = message.AlarmName;
236 var metricName = message.Trigger.MetricName;
237 var oldState = message.OldStateValue;
238 var newState = message.NewStateValue;
239 var alarmDescription = message.AlarmDescription;
240 var alarmReason = message.NewStateReason;
241 var trigger = message.Trigger;
242 var color = "warning";
243
244 if (message.NewStateValue === "ALARM") {
245 color = "danger";
246 } else if (message.NewStateValue === "OK") {
247 color = "good";
248 }
249
250 var slackMessage = {
251 text: "*" + subject + "*",
252 attachments: [
253 {
254 "color": color,
255 "fields": [
256 { "title": "Alarm Name", "value": alarmName, "short": true },
257 { "title": "Alarm Description", "value": alarmReason, "short": false},
258 {
259 "title": "Trigger",
260 "value": trigger.Statistic + " "
261 + metricName + " "
262 + trigger.ComparisonOperator + " "
263 + trigger.Threshold + " for "
264 + trigger.EvaluationPeriods + " period(s) of "
265 + trigger.Period + " seconds.",
266 "short": false
267 },
268 { "title": "Old State", "value": oldState, "short": true },
269 { "title": "Current State", "value": newState, "short": true },
270 {
271 "title": "Link to Alarm",
272 "value": "https://console.aws.amazon.com/cloudwatch/home?region=" + region + "#alarm:alarmFilter=ANY;name=" + encodeURIComponent(alarmName),
273 "short": false
274 }
275 ],
276 "ts": timestamp
277 }
278 ]
279 };
280 return _.merge(slackMessage, baseSlackMessage);
281};
282
283var handleAutoScaling = function(event, context) {
284 var subject = "AWS AutoScaling Notification"

Callers 1

processEventFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected