简体   繁体   中英

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,
    }),
  };
};

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;
};

You have isBase64Encoded: true but this should be set to false .

The only time you want to set this to true is if the entire response.body is base64 encoded and you want the balancer to decode it before returning it to the browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM