var https = require('https'); | |
var util = require('util'); | |
exports.handler = function(event, context) { | |
console.log(JSON.stringify(event, null, 2)); | |
console.log('From SNS:', event.Records[0].Sns.Message); | |
var postData = { | |
"channel": "#aws-sns", | |
"username": "AWS SNS via Lamda :: DevQa Cloud", | |
"text": "*" + event.Records[0].Sns.Subject + "*", | |
"icon_emoji": ":aws:" | |
}; | |
var message = event.Records[0].Sns.Message; | |
var severity = "good"; | |
var dangerMessages = [ | |
" but with errors", | |
" to RED", | |
"During an aborted deployment", | |
"Failed to deploy application", | |
"Failed to deploy configuration", | |
"has a dependent object", | |
"is not authorized to perform", | |
"Pending to Degraded", | |
"Stack deletion failed", | |
"Unsuccessful command execution", | |
"You do not have permission", | |
"Your quota allows for 0 more running instance"]; | |
var warningMessages = [ | |
" aborted operation.", | |
" to YELLOW", | |
"Adding instance ", | |
"Degraded to Info", | |
"Deleting SNS topic", | |
"is currently running under desired capacity", | |
"Ok to Info", | |
"Ok to Warning", | |
"Pending Initialization", | |
"Removed instance ", | |
"Rollback of environment" | |
]; | |
for(var dangerMessagesItem in dangerMessages) { | |
if (message.indexOf(dangerMessages[dangerMessagesItem]) != -1) { | |
severity = "danger"; | |
break; | |
} | |
} | |
// Only check for warning messages if necessary | |
if (severity == "good") { | |
for(var warningMessagesItem in warningMessages) { | |
if (message.indexOf(warningMessages[warningMessagesItem]) != -1) { | |
severity = "warning"; | |
break; | |
} | |
} | |
} | |
postData.attachments = [ | |
{ | |
"color": severity, | |
"text": message | |
} | |
]; | |
var options = { | |
method: 'POST', | |
hostname: 'hooks.slack.com', | |
port: 443, | |
path: '/services/your-slack-webhook-url-info-goes-here' | |
}; | |
var req = https.request(options, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
context.done(null); | |
}); | |
}); | |
req.on('error', function(e) { | |
console.log('problem with request: ' + e.message); | |
}); | |
req.write(util.format("%j", postData)); | |
req.end(); | |
}; |
This comment has been minimized.
This comment has been minimized.
Hello and thank you for this. I confirm, that line 65 was causing troubles. Replace color with severity and it should be OK. |
This comment has been minimized.
This comment has been minimized.
Great , after the change of line 65 it works |
This comment has been minimized.
This comment has been minimized.
Sorry for the |
This comment has been minimized.
This comment has been minimized.
Anyone have any idea of how to get it to format nicely into Slack? just comes out as a huge blob of JSON that is hardly readable :( |
This comment has been minimized.
This comment has been minimized.
I had to change the request portion to:
|
This comment has been minimized.
This comment has been minimized.
Can anyone please help me with this script as to how to do I alert particular owners (people) in a channel where messages are posted. Thanks in advance |
This comment has been minimized.
This comment has been minimized.
@aayushKumarJarvis You can modify line 11 ( |
This comment has been minimized.
This comment has been minimized.
customized this script.
https://github.com/morugu/aws-lambda-function-templates/blob/master/sns/sns-to-slack.js |
This comment has been minimized.
This comment has been minimized.
I guess the
|
This comment has been minimized.
This comment has been minimized.
@makaivelli When I use your toText function the json is formatted but not all is showed. Would you be able to help with modifying it to show the full formatted json? Is there a way to select which json keys are shown and specify the title in slack message? I'm trying to get alerts in Slack when someone login to AWS. The JSON looks like this:
After this is parsed by toText function I only get this in Slack:
My knowledge of nodejs is very limited as I mostly deal with infrastructure. |
This comment has been minimized.
Thanks for this! Your newest revision removed all definitions of
color
, which causes the dreaded"Process exited before completing request"
error.EDIT: Actually it looks like you renamed to
severity
. So if you change line 65'scolor
toseverity
, all is good!