Created
November 5, 2015 13:27
-
-
Save fernandohonig/ea392856086bf574ae4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require ('https'); | |
var querystring = require ('querystring'); | |
exports.handler = function(event, context) { | |
console.log(event); | |
var message = JSON.parse(event.Message); | |
var color = 'warning'; | |
switch(message.NewStateValue) { | |
case "OK": | |
color = 'good'; | |
break; | |
case "ALARM": | |
color = 'danger'; | |
break; | |
} | |
var payloadStr = JSON.stringify({ | |
"username": "Cloudwatch", | |
"attachments": [ | |
{ | |
"title": message.AlarmName, | |
"fallback": message.NewStateReason, | |
"text": message.NewStateReason, | |
"fields": [ | |
{ | |
"title": "Region", | |
"value": message.Region, | |
"short": true | |
}, | |
{ | |
"title": "State", | |
"value": message.NewStateValue, | |
"short": true | |
} | |
], | |
"color": color | |
} | |
], | |
"icon_emoji": ":cloudwatch:" | |
}); | |
var postData = querystring.stringify({ | |
"payload": payloadStr | |
}); | |
var options = { | |
hostname: 'hooks.slack.com', | |
port: 443, | |
path: '/services/<your_unique_web_hook_url>', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': postData.length | |
} | |
}; | |
var req = http.request(options, function(res) { | |
res.on("data", function(chunk) { | |
console.log(chunk); | |
context.done(null, 'done!'); | |
}); | |
}).on('error', function(e) { | |
context.done('error', e); | |
}); | |
req.write(postData); | |
req.end(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment