I have the following function for making API calls in a Lambda function:
function CallAPIMethod(method, path, params) {
return new Promise((resolve, reject) => {
try
{
console.log("API", `Method ${method}, Path ${path}, Params ${params}`);
let responseBody = '';
var options = {
host: process.env.API_URL,
port: 443,
path: `/atservicesrest/v1.0/${path}/${params}`,
method: method,
headers: {
'Content-Type': 'application/json',
'ApiIntegrationCode': process.env.API_INTEGRATION_CODE,
'UserName': process.env.API_USERNAME,
'Secret': process.env.API_SECRET,
'Accept': "*/*"
}
};
console.log("OPTIONS", `${JSON.stringify(options)}`);
const req = https.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log('BODY: ' + chunk);
responseBody += chunk; // Append the chunk to the responseBody variable
});
res.on('end', function() {
// The API call has completed, resolve the Promise with the responseBody
console.log("RESPONSE", `${responseBody}`);
resolve(responseBody);
});
});
req.on('error', function(err) {
// If there's an error with the API request, reject the Promise with the error
console.log("REJECTED", `${err}`);
reject(err);
});
req.end();
}
catch(err)
{
console.error(`API failed: method ${method}, path ${path}, params ${params}:: Error ${err}`);
}
});
}
It's running fine locally when debugging in VSCode, but when deployed to AWS it stops just after its written the options to the console. I've compared the options locally and from AWS and they're identical.
The output when run locally is:
Calling API method... API Method GET, Path tickets, Params 3168128
OPTIONS {"host":"webservices17.autotask.net","port":443,"path":"/atservicesrest/v1.0/tickets/3168128","method":"GET","headers":{"Content-Type":"application/json","ApiIntegrationCode":"","UserName":"","Secret":"*****","Accept":"/"}} index.mjs:244
STATUS: 200
And from AWS is:
2023-08-10T08:20:17.505Z 3be413e6-2a60-4ff6-b49b-92f766b04d61 INFO Calling API method... 2023-08-10T08:20:17.506Z 3be413e6-2a60-4ff6-b49b-92f766b04d61 INFO API Method GET, Path tickets, Params 3168128 2023-08-10T08:20:17.506Z 3be413e6-2a60-4ff6-b49b-92f766b04d61 INFO OPTIONS {"host":"webservices17.autotask.net","port":443,"path":"/atservicesrest/v1.0/tickets/3168128","method":"GET","headers":{"Content-Type":"application/json","ApiIntegrationCode":"","UserName":"","Secret":"*****","Accept":"/"}} END RequestId: 3be413e6-2a60-4ff6-b49b-92f766b04d61
In other words, it outputs the options, calls the API and then just dies. Doesn't reach the res.on or req.on and doesn't throw an error.
I'm guessing there's something in AWS preventing the call?