Skip to content

Instantly share code, notes, and snippets.

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
You can’t perform that action at this time.