Skip to content

Instantly share code, notes, and snippets.

@babelop
Created February 1, 2017 20:49
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
CodeCommit Webhook for AWS Lambda
'use strict';
const url = require('url');
const https = require('https');
exports.handler = (event, context, callback) => {
let webhook_url = event.Records[0].customData;
if (!webhook_url) {
let error = new Error("Web-hook URL not provided as custom data.");
callback(error)
} else {
console.log('POST web-hook to ' + webhook_url)
let options = url.parse(webhook_url);
options['method'] = 'POST';
const req = https.request(options, (res) => {
let body = '';
console.log('Status:', res.statusCode);
console.log('Headers:', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
console.log('Successfully triggered web-hook.');
// If we know it's JSON, parse it.
if (res.headers['content-type'] === 'application/json') {
body = JSON.parse(body);
}
callback(null, body);
});
});
req.on('error', callback);
req.end();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment