Created
September 22, 2017 20:10
Star
You must be signed in to star a gist
Send Code Deploy deployment status to Slack via SNS using a Lambda function
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 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