Malformed Lambda proxy response from API Gateway Invocation

I have been roaming the forums trying to find a resolution to a “Malformed Lambda proxy response” (status: 502) error message you get when deploying and running an API Gateway Invokation that uses a Lambda-Proxy.

Excuse the post length for sake of being exact.

  • I followed the API Gateway tutorial as described in the serverless docs.

  • I ran into a currently open issue with serverless where lambda triggers do not display in the AWS Console, so I added the API Gateway part from API Gateway itself.

  • Now, my Lambda function works as expected, but the response from API Gateway is that it is malformed (see code snippet below)

    Thu Jan 12 20:23:08 UTC 2017 : Endpoint response body before transformations: {“statusCode”:200,“body”:{“tip”:“asdf”}}
    Thu Jan 12 20:23:08 UTC 2017 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, x-amzn-RequestId=…, Connection=keep-alive, Content-Length=78, Date=Thu, 12 Jan 2017 20:23:08 GMT, Content-Type=application/json}
    Thu Jan 12 20:23:08 UTC 2017 : Execution failed due to configuration error: Malformed Lambda proxy response
    Thu Jan 12 20:23:08 UTC 2017 : Method completed with status: 502

  • I then created a Response Model that would shape the response

    {
    "$schema": “http://json-schema.org/draft-04/schema#”,
    “title”: “Custom Response Schema”,
    “type”: “object”,
    “properties”: {
    “statusCode”: { “type”: “number” },
    “body”: {
    “type”: “object”,
    “properties”: {
    “tip”: { “type”: “string” }
    }
    }
    }
    }
    But, that didn’t seem to help (not does specifying Empty)

  • I then reviewed my .yml file and it declared the function as follows

    functions:
    tips:
    handler: handler.getTip
    events:

    • http:
      path: tip
      method: get
      integration: lambda-proxy
  • And this seemed all fine, so I looked specifically at the IAM Role to see if I had the correct permissions where I had added explicit invoke access to the resource as follows:

      {
          "Effect": "Allow",
          "Action": [
              "execute-api:Invoke"
          ],
          "Resource": [
              "arn:aws:execute-api:us-east-1:<AWS_ID>:<APP_ID>/<STAGE>/GET/tip"
          ]
      }
    
  • Which seemed to clear up all permission messages, but I still can’t get past that 502 status of a Malformed Lambda proxy response.

Any ideas what I’ve missed and could I have done all of this via Serverless alone. I am new to Serverless so if I could create the response template, add the IAM permissions, and configure API Gateway to cache the response to 15 minute intervals (on my todo list) ALL within serverless, please help because I’m stumped.

I got it to work, but I’m not sure what it was particularly so I’m outlining the top probabilities for you:

I added the following to my serverless.yml file:

iamRoleStatements:
- Effect: Allow
  Action:
    - execute-api:Invoke
  Resource: "arn:aws:execute-api:us-east-1:*:*"

I changed the return execution in my handler.js to invoke the context.suceed method

context.succeed(result);

The issue was that API Gateway didn’t like what Lambda was giving it, which is odd because I began the project serverless-first so perhaps a step is missing in the documentation OR some specific documentation needs to be added to address this approach.

Hey

I’m not sure why you’re using a response model with Lambda Proxy? The whole point of Lambda Proxy Integration is that you return the status code, headers and body that you want the API gateway to send back.

Rich

1 Like

I thought that was a necessity for API Gateway to conform to. The 502 error I had been running into is not at all informative, so I’m not yet certain what can be trimmed to no effect. Also, I was outlining my path of resolution, so the response model isn’t actually active in my current working code.

Thanks for pointing that out as it would indeed be redundant and its final exclusion was not clearly mentioned in my posts.