Skip to content

Instantly share code, notes, and snippets.

@cesc1989
Created September 22, 2017 20:10
Show Gist options
  • Save cesc1989/31e18f5f1e381bfe5813a9e22cd3fe48 to your computer and use it in GitHub Desktop.
Save cesc1989/31e18f5f1e381bfe5813a9e22cd3fe48 to your computer and use it in GitHub Desktop.
Send Code Deploy deployment status to Slack via SNS using a Lambda function
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": "#server-deploys",
"username": "AWS SNS via Lambda",
"text": "*" + event.Records[0].Sns.Subject + "*",
"icon_emoji": ":aws:"
};
var message = JSON.parse(event.Records[0].Sns.Message);
var severity = "good";
var error_information = "No news, good news";
var final_error = "";
var formated_message = "";
var deployment_status = message.status;
if (deployment_status == "CREATED"){
severity = "warning";
}else if (deployment_status == "FAILED"){
severity = "danger";
error_information = JSON.parse(message.errorInformation);
}
if (typeof(error_information) == "string"){
final_error = error_information;
}else{
final_error = error_information.ErrorMessage;
}
formated_message = message.deploymentGroupName +
" deployment *" +
message.status +
"*. Deployment for " +
message.applicationName +
" in region " +
message.region +
".\n\nError information: " +
final_error;
postData.attachments = [
{
"color": severity,
"text": formated_message
}
];
var options = {
method: 'POST',
hostname: 'hooks.slack.com',
port: 443,
path: '/services/[TOKEN]'
};
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