Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# lambda-cloudwatch-slack
# lambda-cloudwatch-slack
Amazon Lambda to receive alerts from CloudWatch and post to Slack.

## What is This?
Expand Down
41 changes: 19 additions & 22 deletions handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@ console.log('[Amazon CloudWatch Notification]');
*/
var ALARM_CONFIG = [
{
condition: "INFO",
channel: "#test",
mention: " ",
color: "#FF9F21",
condition: "OK",
channel: "#cloudwatch",
mention: "<@here>",
color: "#21ff9f",
severity: "INFO"
},
{
condition: "CRITICAL",
channel: "#general",
mention: "<@channel> ",
condition: "ALARM",
channel: "#cloudwatch",
mention: "<@here>",
color: "#F35A00",
severity: "CRITICAL"
}

];

var SLACK_CONFIG = {
path: "YOUR_PATH",
path: "/YOUR_PATH",
};

var http = require ('https');
Expand All @@ -41,19 +42,22 @@ exports.handler = function(event, context) {
var color;

// create post message
var alarmMessage = " *[Amazon CloudWatch Notification]* \n"+
"Subject: "+subject+"\n"+
"Message: "+message+"\n"+
"Timestamp: "+timestamp;
var s1 = subject.split(' ');
var s2 = s1[1].split('awsroute53');
var alarmMessage = "`"+s2[0]+"`";

// check subject for condition
for (var i=0; i < ALARM_CONFIG.length; i++) {
var row = ALARM_CONFIG[i];
console.log(row);
if (subject.match(row.condition)) {
console.log("Matched condition: "+row.condition);

alarmMessage = row.mention+" "+alarmMessage+" ";
if(row.condition=="ALARM")
alarmMessage = row.mention+": "+alarmMessage+" "+" is not responding for the last 5 mins.\n";
else if(row.condition=="OK")
alarmMessage = row.mention+": "+alarmMessage+" "+" is up now.\n";
else
alarmMessage = alarmMessage;
channel = row.channel;
severity = row.severity;
color = row.color;
Expand All @@ -72,14 +76,7 @@ exports.handler = function(event, context) {
"fallback": alarmMessage,
"text": alarmMessage,
"mrkdwn_in": ["text"],
"username": "AWS-CloudWatch-Lambda-bot",
"fields": [
{
"title": "Severity",
"value": severity,
"short": true
}
],
"username": "AWS-CloudWatch",
"color": color
}
],
Expand Down
8 changes: 4 additions & 4 deletions sns_sample_event.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
Records:[
{
"Records":[
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
Expand All @@ -8,7 +8,7 @@ module.exports = {
"Type": "Notification",
"MessageId": "xxxxx-xxx-xxx-xxxxxxxx",
"TopicArn": "arn:aws:sns:EXAMPLE",
"Subject": "ALARM: 'Elasticache-FreeableMemory-CRITICAL' in APAC - Tokyo",
"Subject": "ALARM: This-is-a-test-msg in APAC - Tokyo",
"Message": " {'AlarmName':'Elasticache-FreeableMemory-INFO','AlarmDescription':'Elasticache-FreeableMemory-INFO','AWSAccountId':'1234567890','NewStateValue':'ALARM','NewStateReason':'Threshold Crossed: 1 datapoint (8.60604416E8) was less than or equal to the threshold (1.0E9).','StateChangeTime':'1970-01-01T00:00:00.000+0000','Region':'APAC - Tokyo','OldStateValue':'OK','Trigger':{'MetricName':'FreeableMemory','Namespace':'AWS/ElastiCache','Statistic':'AVERAGE','Unit':null,'Dimensions':[],'Period':60,'EvaluationPeriods':1,'ComparisonOperator':'LessThanOrEqualToThreshold','Threshold':1.0E9}}",
"Timestamp": "1970-01-01T00:00:00.000Z",
"SignatureVersion": "1",
Expand All @@ -28,4 +28,4 @@ module.exports = {
}
}
]
};
}