Last active
February 21, 2018 15:47
-
-
Save gboyegadada/ca985bdc8a509b53da05e250cf78b947 to your computer and use it in GitHub Desktop.
Send approval request from AWS CodePipeline to Slack using SNS and Lambda
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
// Based on: https://gist.github.com/terranware/962da63ca547f55667f6 | |
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": "#development", | |
"username": "AWS CodePipeline via Lambda", | |
"text": "*" + event.Records[0].Sns.Subject + "*", | |
"icon_emoji": ":aws_codepipeline:" | |
}; | |
var message = event.Records[0].Sns.Message; | |
var severity = "good"; | |
var dangerMessages = [ | |
" but with errors", | |
" to RED", | |
"During an aborted deployment", | |
"Failed to deploy application", | |
"Failed to deploy configuration", | |
"has a dependent object", | |
"is not authorized to perform", | |
"Pending to Degraded", | |
"Stack deletion failed", | |
"Unsuccessful command execution", | |
"You do not have permission", | |
"Your quota allows for 0 more running instance"]; | |
var warningMessages = [ | |
" aborted operation.", | |
" to YELLOW", | |
"Adding instance ", | |
"Degraded to Info", | |
"Deleting SNS topic", | |
"is currently running under desired capacity", | |
"Ok to Info", | |
"Ok to Warning", | |
"Pending Initialization", | |
"Removed instance ", | |
"Rollback of environment" | |
]; | |
for(var dangerMessagesItem in dangerMessages) { | |
if (message.indexOf(dangerMessages[dangerMessagesItem]) != -1) { | |
severity = "danger"; | |
break; | |
} | |
} | |
// Only check for warning messages if necessary | |
if (severity == "good") { | |
for(var warningMessagesItem in warningMessages) { | |
if (message.indexOf(warningMessages[warningMessagesItem]) != -1) { | |
severity = "warning"; | |
break; | |
} | |
} | |
} | |
var msgObj = JSON.parse(message); | |
var fields = [ | |
{ | |
"title": "Pipeline Name", | |
"value": msgObj.approval.pipelineName, | |
"short": false | |
}, | |
{ | |
"title": "Stage Name", | |
"value": msgObj.approval.stageName, | |
"short": false | |
}, | |
{ | |
"title": "Action Name", | |
"value": msgObj.approval.actionName, | |
"short": false | |
}, | |
{ | |
"title": "Expires", | |
"value": msgObj.approval.expires, | |
"short": false | |
}, | |
{ | |
"title": "Staging URL", | |
"value": msgObj.approval.externalEntityLink, | |
"short": false | |
}, | |
{ | |
"title": "Approval Review Link", | |
"value": msgObj.approval.approvalReviewLink, | |
"short": false | |
}, | |
{ | |
"title": "Note", | |
"value": msgObj.approval.customData, | |
"short": false | |
} | |
]; | |
postData.attachments = [ | |
{ | |
"color": severity, | |
"fallback": msgObj.approval.customData + ' ' + msgObj.approval.approvalReviewLink, | |
"fields": fields | |
} | |
]; | |
var options = { | |
method: 'POST', | |
hostname: 'hooks.slack.com', | |
port: 443, | |
path: '/services/copy-from-slack' | |
}; | |
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