Skip to content

Instantly share code, notes, and snippets.

@teeli
Last active October 13, 2022 13:18
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save teeli/29a0e79397fa5560e1819b80025981af to your computer and use it in GitHub Desktop.
Save teeli/29a0e79397fa5560e1819b80025981af 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');
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]);
}
fields.push({
"title": key
.replace(/([A-Z])/g, ' $1')
.replace(/^./, function (str) {
return str.toUpperCase();
}),
"value": value.join(', '),
});
} else {
fields.push({
"title": key
.replace(/([A-Z])/g, ' $1')
.replace(/^./, function (str) {
return str.toUpperCase();
}),
"value": messageJSON[key],
"short": 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
}
];
var options = {
method: 'POST',
hostname: 'hooks.slack.com',
port: 443,
path: '/services/yourservicehookhere'
};
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();
};
@luisdalmolin
Copy link

luisdalmolin commented Jan 4, 2018

Thank you!

@alex-bashkatov-ezlo
Copy link

Hi! Could you please point how actually is this lambda triggered once CodeDeploy deployment completed? What's the bridge setup?

@hughesjj
Copy link

You can set up an sns subscribption for code deploy events. Looks like it's triggered by those

@KeiKawano
Copy link

KeiKawano commented Aug 10, 2018

🍺

@ali-ahasan
Copy link

Nice and its working perfectly, I have one issue/suggestion, slack show time in UTC for each deployment, is there any way we can change that to User machine timezone. Slack API have this ability

@hendrixroa
Copy link

Thanks

@tekka171
Copy link

Thanks

@redhawk19
Copy link

How to set microsoft team webhook in that lambda function ?

@annehjpark
Copy link

error

{
"errorType": "SyntaxError",
"errorMessage": "Unexpected token e in JSON at position 0",
"trace": [
"SyntaxError: Unexpected token e in JSON at position 0",
" at JSON.parse ()",
" at Runtime.exports.handler (/var/task/index.js:10:28)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment