Skip to content

Instantly share code, notes, and snippets.

@juancho088
Forked from teeli/codedeploy-to-slack.js
Last active February 23, 2018 10:32
Show Gist options
  • Save juancho088/8a471ea425d5a7abcf39aa7d6d4dd939 to your computer and use it in GitHub Desktop.
Save juancho088/8a471ea425d5a7abcf39aa7d6d4dd939 to your computer and use it in GitHub Desktop.
CodeDeploy notifications to Slack (AWS Lambda via SNS)
var https = require('https');
var util = require('util');
// Based on https://gist.github.com/teeli/29a0e79397fa5560e1819b80025981af
function formatTitle(key) {
return key.replace(/([A-Z])/g, ' $1')
.replace(/^./, function (str) {
return str.toUpperCase();
});
}
function addMessage(fields, title, value, isShort) {
fields.push({
"title": title,
"value": value,
"short": isShort
});
}
exports.handler = function (event, context) {
console.log(JSON.stringify(event, null, 2));
console.log('From SNS:', event.Records[0].Sns.Message);
var severity = "good";
var message = event.Records[0].Sns.Message;
var messageJSON = JSON.parse(message);
var subject = event.Records[0].Sns.Subject;
var postData = {
channel: "#channel",
username: "CodeDeploy",
icon_emoji: ":codedeploy:"
};
if (messageJSON.status == "FAILED") {
severity = "danger"
}
if (messageJSON.status == "STOPPED") {
severity = "warning"
}
var fields = [];
for (var key in messageJSON) {
if (key == 'deploymentOverview') {
var value = [];
var deploymentOverview = JSON.parse(messageJSON[key]);
for (var status in deploymentOverview) {
value.push(status + ': ' + deploymentOverview[status]);
}
addMessage(fields, formatTitle(key), value.join(', '), false);
}
else if (key == 'lifecycleEvents') {
var value = [];
var lifecycleEvents = JSON.parse(messageJSON[key]);
for (var i=0; i < lifecycleEvents.length; i++) {
var tmpValue = [];
tmpValue.push(`*${i+1}. ${formatTitle(lifecycleEvents[i].LifecycleEvent)}*`);
tmpValue.push(`>*Status:* ${lifecycleEvents[i].LifecycleEventStatus}`);
if ('StartTime' in lifecycleEvents[i])
tmpValue.push(`>*Start Time:* ${lifecycleEvents[i].StartTime}`);
if ('EndTime' in lifecycleEvents[i])
tmpValue.push(`>*End Time:* ${lifecycleEvents[i].EndTime}`);
value.push(tmpValue.join('\n'));
}
addMessage(fields, formatTitle(key), value.join('\n'), true);
} else {
addMessage(fields, formatTitle(key), messageJSON[key], true);
}
}
postData.attachments = [
{
"color": severity,
"fallback": message,
"title": subject,
"title_link": "https://console.aws.amazon.com/codedeploy/home?region=" + messageJSON.region + "#/deployments/" + messageJSON.deploymentId,
"fields": fields
}
];
// SLACK_ENDPOINT is environment variable that should follow the structure '/services/yourservicehookhere' or you can always hard coded
var options = {
method: 'POST',
hostname: 'hooks.slack.com',
port: 443,
path: process.env.SLACK_ENDPOINT
};
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();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment