Internal server error on 3rd party API call inside Lambda function

Hi,

I’m trying to download a file from s3 and then upload it to another server. Below mentioned is my code. The file gets downloaded from s3 successfully but i guess the code breaks after i make call to 3rd party APIs which are in the separate function callNGUpload().
When i run this code offline or local invoke it runs perfectly but when i run it through endpoint after deploying it gives “internal server error”.
I think there is some issue related to proper response object return. I tried independent return at the end as well as callback method but nothing worked. I’m unable to find what exactly the problem is. Did someone face the same issue?
‘use strict’;
var AWS = require(‘aws-sdk’);
var s3 = new AWS.S3();
var fs = require(‘fs’);
var apiContext = require(‘mozu-node-sdk/clients/platform/application’)();
var documentResource = require(‘mozu-node-sdk/clients/content/documentlists/document’)(apiContext);

      module.exports.imageUpload = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  console.log("Event : "+JSON.stringify(event.queryStringParameters.name));
  var qryString = event.queryStringParameters.name;
    // var original = qryString.split('-bdh-')[0]; 
    // var newFileName =  qryString.split('-bdh-')[1];
    var original = event.queryStringParameters.original; 
    var newFileName =  event.queryStringParameters.name;
    console.log("Original : "+original);
    console.log("New File : "+newFileName);
    console.log("Params : "+JSON.stringify(event.queryStringParameters));

    // var fileNm = "Capture1.JPG"
    const params = {
        Bucket: "kibocustomerfiles",
        Key: original
    }
   var s3client = new AWS.S3({
        accessKeyId:"AKIAXN3ILGE3LB7BDA44",
        secretAccessKey:"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"
        // accessKeyId:"AKIA4CBYWWYGDQ5VGFT5",
        // secretAccessKey:"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"
    });

   s3client.getObject(params, (err, data) => {
      if(err) {
        console.log(err);
        callback(null, err);
      } else {
        console.log("LENGTH : "+data.Body.length);
        var img = data.Body;
        console.log("Length : "+img.length);
        callNGUpload(data.Body, newFileName, callback);
      }
    })
  }
function callNGUpload(img, fileName) {
 // img = fs.readFileSync(img);
 // console.log("SIZE : "+img.length);
// console.log("Image : "+img);
documentResource.createDocument({
    documentListName: 'files@mozu'
}, {
    body:{
        contentLength: img.length,
        contentMimeType: "image/JPG",
        documentTypeFQN: "image@mozu",
        listFQN: "files@mozu",
        name: fileName,
        publishState: "Active"
    }
}).then(data => {
  console.log("DATA SOURCE");
        return documentResource.updateDocumentContent({documentListName: "files@mozu", documentId: data.id}, {body: img, headers: {'Content-Type': 'image/JPG'}})
        .then((result)=>{
            console.log("Success !!");
            console.log(result);
            callback(null, "Successfully uploaded");
        }).catch((err)=>{
            console.log("Not Uploaded !!");
            console.error(err);
            callback(null, "Upload failed");
        });
    })
}

You can’t mix the async and callback handler syntax. This isn’t valid

async (event, context, callback)

use

async (event, context)

or

(event, context, callback)

The way your code is currently written the Lambda returns a Promise that is resolved almost immediately after the second s3.getObject() and before you use the callback. Lambda then terminates without completing your code.

I wrote a blog post about how async handlers work a while ago.

Sorry, I pasted the wrong code and have update the post with correct code above. I had the async removed already from the actual code i was trying. It works fine as in, download from s3 and uploads file to the 3rd party server. The only problem is it doesn’t give any legit response and only returns ‘Internal Server error’, which is a kind of bugger.

Are you using lambda integration? If you are then the response is incorrect and needs to be something like

callback(null, { statusCode: 200, body: JSON.stringify({ message: "Upload failed"}) });