Попробуйте изменить код следующим образом:
const http = require('http');
exports.handler = function (event, context, callback) {
console.log(event);
var data = JSON.stringify({
'name': 'test'
});
const options = {
hostname: 'example.com',
path: '/path-to-resource',
port: 80,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
};
function sendFileToUrl(options, data, callback) {
console.log('Sending File to the URL:' + url);
if (url && data) {
const req = http.request(url, JSON.stringify(data)).then(function (res) {
console.log("Data Sent & response: " + res.statusCode);
callback(null, 'success msg');
});
req.on('error', function (e) {
console.log("Could not send the data & error: " + e.message);
callback(new Error('failure'));
});
req.write(data);
req.end();
}
}
sendFileToUrl(options, data, callback);
};