0

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?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Nick Wright
  • 87
  • 1
  • 9
  • Does this answer your question -- https://stackoverflow.com/questions/50106048/how-to-make-an-external-api-call-within-an-aws-lambda-function – smac2020 Aug 10 '23 at 13:12
  • unfortunately not, I spotted that and the symptoms are the same, but I'm not running it in a VPC – Nick Wright Aug 10 '23 at 14:48
  • unfortunately I have never attempted to hit an external URL from within a Lambda function. I have only used the AWS SDK from within Lambda functions to invoke AWS Service operations - which work fine. – smac2020 Aug 10 '23 at 14:57
  • I call out from Lambda to external API's often. Are you sure that the endpoint you're calling allows calls from AWS? – stdunbar Aug 10 '23 at 18:59
  • Well, the code works locally when I run it in vsCode. I'm concerned by what you mean about the endpoint allowing calls from AWS? Is that a common issue? – Nick Wright Aug 11 '23 at 07:59
  • 1
    I've seen it before that an endpoint doesn't allow connections from cloud providers. This can occur when the service can't handle the volume that a cloud provider can send or that they only expect humans to connect, not API's. Alternatively, it's possible that the API requires an IP passlist to allow a connection. I'm not positive given that you're not getting a specific error but it's something to consider. – stdunbar Aug 11 '23 at 15:32
  • Also, check the [status page](http://www.autotaskstatus.net/) as there were issues recently with connections. – stdunbar Aug 11 '23 at 17:24

0 Answers0