Created
May 1, 2016 23:34
-
-
Save marcelog/b96bd8ce87de0d13a5f869c106b8f604 to your computer and use it in GitHub Desktop.
Use this Lambda in combination with SNS to send HipChat notifications from CodeDeploy during the lifecycle of a Deploy
This file contains hidden or 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 http = require('http'); | |
var https = require('https'); | |
var buffer = require('buffer').Buffer; | |
var util = require('util'); | |
var querystring = require('querystring'); | |
function hipchat(message, color, callback) { | |
var token = 'your_hipchat_v1_token'; | |
var body = querystring.stringify({ | |
message: message, | |
message_format: 'text', | |
color: color, | |
notify: 1, | |
from: 'message_source', | |
room_id: 'a_room_name_or_id' | |
}); | |
var options = { | |
host: 'api.hipchat.com', | |
port: 443, | |
path: ('/v1/rooms/message?format=json&auth_token=' + token), | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': Buffer.byteLength(body) | |
} | |
}; | |
var req = https.request(options, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
}); | |
res.on('end', function (chunk) { | |
callback(); | |
}); | |
}); | |
req.on('error', function(err) { | |
callback(err); | |
}); | |
req.write(body); | |
req.end(); | |
} | |
exports.handler = function(event, context, callback) { | |
console.log(event.Records[0].Sns.Message); | |
var text = event.text; | |
var color = event.color; | |
if(event.Records) { | |
var msg = event.Records[0].Sns; | |
var deployMsg = JSON.parse(msg.Message); | |
var subject = msg.Subject; | |
var status = deployMsg.status; | |
text = msg.text; | |
if(status === 'SUCCEEDED' || /SUCCEEDED/.test(subject)) { | |
color = 'green'; | |
} else if(status === 'CREATED' || /CREATED/.test(subject)) { | |
color = 'yellow'; | |
} else if(status === 'INPROGRESS' || /INPROGRESS/.test(subject)) { | |
color = 'yellow'; | |
} else { | |
color = 'red'; | |
} | |
text = subject; | |
if(deployMsg.deploymentOverview) { | |
text = text + ': ' + deployMsg.deploymentOverview; | |
} | |
if(deployMsg.deploymentGroupName) { | |
text = text + ' (' + deployMsg.deploymentGroupName + ')'; | |
} | |
if(deployMsg.lifecycleEvents) { | |
text = text + ' ' + deployMsg.lifecycleEvents; | |
} | |
} | |
hipchat(text, color, callback); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the version that works with HipChat API v2