Search code examples
node.jsamazon-web-servicesaws-lambdaaws-sdk-nodejsaws-alb

AWS ALB returns 502 Bad Gateway when return audio buffer from Lambda in nodejs


I have a lambda function which does return audio buffer in response, when I invoke lambda from code, it works fine but when I call lambda behind ALB ,I get an error -

502 Bad Gateway

// Lambda function handler

'use strict';


module.exports.handler = async (event, context) => {
  // ALB expects following response format
  // see: https://docs.aws.amazon.com/lambda/latest/dg/services-alb.html
  const response = {
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Content-Type': 'application/json',
    },
    isBase64Encoded: true,
    statusCode: 200,
    statusDescription: '200 OK',
  };
// getting buffer from backend api
  const answer = 'This is my audio buffer'.toString('base64');
  return {
    response,
    body: JSON.stringify({
      id: 123,
      myBuffer: answer,
    }),
  };
};

Solution

  • Your return param doesn't seem to be correct according to JSON format.

    What about this?

      ...
      const answer = 'This is my audio buffer'.toString('base64');
      response.body = JSON.stringify({
        id: 123,
        myBuffer: answer
      });
      return response;
    };