0

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,
    }),
  };
};
SuperStar518
  • 2,814
  • 2
  • 20
  • 35
Dharam
  • 469
  • 2
  • 9
  • 23

2 Answers2

2

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;
};
SuperStar518
  • 2,814
  • 2
  • 20
  • 35
  • it worked,I set isBase64Encoded:false and changed the response format as suggested above and it worked for me. – Dharam Apr 25 '19 at 13:36
1

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.

Michael - sqlbot
  • 169,571
  • 25
  • 353
  • 427