Functions error decoding lambda response: invalid status code returned from lambda: 0

Hi folks, there are a few other posts that mention similar issues, but having tried numerous variations of solutions, I still can’t get this working.

I am calling a Netlify function via POST from a separate AWS Lambda function (not part of my Netlify project). When I run netlify dev locally and route my POST to my local dev server via ngrok, things work as expected: my Netlify function receives the POST request, and it returns to the aws lambda function without issue.

However, when I deploy to Netlify and do the same thing: 1) My Netlify function receives the POST request without error (as verified via function logs), but 2) My aws lambda function receives a response of error decoding lambda response: invalid status code returned from lambda: 0 from my Netlify function.

I would very much appreciate help resolving this!

Here is the function code in its present form (though I’ve tried numerous variations of return values based on other forum questions to no avail):

(fwiw current endpoint of this function is https://boring-varahamihira-cc7a90.netlify.app/.netlify/functions/completedTranscriptionCallback ):

exports.handler = (event, context) => {
  console.log('completedTranscriptionCallback');
  console.log(event);
  console.log(context);

  let responseBody = {
    message: "Body test",
    input: "An input!"
  };

  let response = {
    statusCode: 200,
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(responseBody)
  };
  console.log("response: ", JSON.stringify(response))

  return response;
}

As mentioned, if I route this to my local dev environment via e.g. ngrok my aws lambda function is able to call it without error and receives the proper response body. It’s only once I deploy to Netlify that the error is returned and logged in lambda.

Thanks! Happy to provide additional info as needed.

Hey @Malcolm

Try changing

exports.handler = (event, context) => {
  // code...
}

to

exports.handler = async (event, context) => {
  // code...
}

Check out the first two examples on this page for more details on why you need async.

Hey @coelmay thanks! That indeed resolved the issue.

Despite spending some time in the docs (today and in the past) I’m still a bit confused regarding the async keyword in this context. According to this page It appears that we’re always supposed to use async promise syntax for synchronous Netlify functions? Is that correct?

Is there ever a time we should not use exports.handler = async (event, context) in Netlify functions? Or is it essentially a required syntax for synchronous functions? (which was also throwing me a bit, since we’re using async in a synchronous function call… Although this appears consistent with the aws lambda docs as well, which I know is what’s running here under-the-hood)

Thanks again for the quick fix!

When you use a callback (as per playground link above.)

exports.handler = function (event, context, callback) {
  callback(null, {
    statusCode: 200,
    body: "Hello, World",
  });
};

That’s the simple explanation. There are others in these forums, and across the Interwebs who could give a fair more detailed explanation than I. Try callback vs async using your preferred search engine :slight_smile:

1 Like

Actually I do have one followup question: do you know why the incorrect (no async) code runs fine when testing locally, and only fails once I push to Netlify?

This I cannot. Perhaps a bug?

Yup here’s at least one similar version of it from two years ago: Function response should have empty body if async keyword is missing · Issue #1047 · netlify/cli · GitHub

1 Like

By all means, add your voice to the issue, link back to this thread too for cross-reference.

2 Likes

Thanks so much for adding your experience to the issue, @Malcolm. We appreciate it!