Hook AWS notifications into Slack with a Lambda function
inopinatus
We have AWS RDS instances that send lifecycle notifications to an SNS topic. This was ending up in email, but I prefer to receive notifications in a Slack channel. Fortunately they are easy to integrate using AWS Lambda and Slack’s webhooks.
Here’s the Lambda function I’m using*, which parses the message object (if it can) and formats the notifications before posting.
console.log('Loading function');
const https = require('https');
const url = require('url');
const slack_url = 'https://hooks.slack.com/services/...';
const slack_req_opts = url.parse(slack_url);
slack_req_opts.method = 'POST';
slack_req_opts.headers = {
'Content-Type': 'application/json'
};
exports.handler = function(event, context) {
(event.Records || []).forEach(function(rec) {
if (rec.Sns) {
var req = https.request(slack_req_opts, function(res) {
if (res.statusCode === 200) {
context.succeed('posted to slack');
} else {
context.fail('status code: ' + res.statusCode);
}
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
context.fail(e.message);
});
var text_msg = JSON.stringify(rec.Sns.Message, null, ' ');
try {
var msg_data = [];
var parsed = JSON.parse(rec.Sns.Message);
for (var key in parsed) {
msg_data.push(key + ': ' + parsed[key]);
}
text_msg = msg_data.join("\n");
} catch (e) {
console.log(e);
}
var params = {
attachments: [{
fallback: text_msg,
pretext: rec.Sns.Subject,
color: "#D00000",
fields: [{
"value": text_msg,
"short": false
}]
}]
};
req.write(JSON.stringify(params));
req.end();
}
});
};
To do it yourself, create the webhook in Slack. Then publish the code above as a Lambda function in your AWS account (a basic execution role will be fine). Update line 5 with your own webhook URL. Have RDS notify a SNS topic and subscribe the Lambda function to that topic.
You can check the integration using Lambda’s standard SNS test event. For an end-to-end & event parsing test I suggest creating and deleting a RDS snapshot.