Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS ALB returns 502 Bad Gateway from lambda

I have a lambda function which return base64 string, when I invoke lambda from code it works, but when I call lambda behind ALB and base64 string is large size, ALB gives me error 502 Bad Gateway. Note:for small size string ALB also works.

// 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',
  };
// ALB gives error 502 when buffer size is large
  const answer = 'This is my audio buffer'.toString('base64');
  response.body = answer
  return response;
  };
like image 775
Dharam Avatar asked Apr 25 '19 20:04

Dharam


1 Answers

Check that you are not exceeding the limits. As per the AWS docs, when using Lambda as an ALB target the maximum response size is 1MB; if the response is more than 1MB you will get an error.

You can see this link for more information on using Lambda as a target for your ALB: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html

like image 91
hephalump Avatar answered Sep 22 '22 15:09

hephalump