AWS lambda api gateway error “Malformed Lambda proxy response”

匿名 (未验证) 提交于 2019-12-03 08:30:34

问题:

I am trying to set up a hello world example with AWS lambda and serving it through api gateway. I clicked the "Create a Lambda Function", which set up the api gatway and selected the Blank Function option. I added the lambda function found on AWS gateway getting started guide:

exports.handler = function(event, context, callback) {   callback(null, {"Hello":"World"});  // SUCCESS with message }; 

The issue is that when I make a GET request to it, it's returning back a 502 response { "message": "Internal server error" }. And the logs say "Execution failed due to configuration error: Malformed Lambda proxy response".

回答1:

Usually, when you see Malformed Lambda proxy response, it means your response from your Lambda function doesn't match the format API Gateway is expecting, like this

{     "isBase64Encoded": true|false,     "statusCode": httpStatusCode,     "headers": { "headerName": "headerValue", ... },     "body": "..." } 

If you are not using Lambda proxy integration, you can login to API Gateway console and uncheck the Lambda proxy integration checkbox.

Also, if you are seeing intermittent Malformed Lambda proxy response, it might mean the request to your Lambda function has been throttled by Lambda, and you need to request a concurrent execution limit increase on the Lambda function.



回答2:

If lambda is used as a proxy then the response format should be

{ "isBase64Encoded": true|false, "statusCode": httpStatusCode, "headers": { "headerName": "headerValue", ... }, "body": "..." } 

Note : The body should be stringified



回答3:

Yeah so I think this is because you're not actually returning a proper http response there which is why you're getting the error.

personally I use a set of functions like so:

    module.exports = {         success: (result) => {             return {                 statusCode: 200,                 headers: {                     "Access-Control-Allow-Origin" : "*", // Required for CORS support to work                     "Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS                 },                 body: JSON.stringify(result),             }         },         internalServerError: (msg) => {             return {                 statusCode: 500,                 headers: {                     "Access-Control-Allow-Origin" : "*", // Required for CORS support to work                     "Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS                 },                 body: JSON.stringify({                     statusCode: 500,                     error: 'Internal Server Error',                     internalError: JSON.stringify(msg),                 }),             }         } } // add more responses here. 

Then you simply do:

var responder = require('responder')  // some code  callback(null, responder.success({ message: 'hello world'})) 


回答4:

From the AWS docs

In a Lambda function in Node.js, To return a successful response, call callback(null, {"statusCode": 200, "body": "results"}). To throw an exception, call callback(new Error('internal server error')). For a client-side error, e.g., a required parameter is missing, you can call callback(null, {"statusCode": 400, "body": "Missing parameters of ..."}) to return the error without throwing an exception.



回答5:

I had this error because I accidentally removed the variable ServerlessExpressLambdaFunctionName from the CloudFormation AWS::Serverless::Api resource. The context here is https://github.com/awslabs/aws-serverless-express "Run serverless applications and REST APIs using your existing Node.js application framework, on top of AWS Lambda and Amazon API Gateway"



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!