paint-brush
What Causes Malformed Lambda Proxy Response and How to Fix it by@taavi-rehemagi
12,847 reads
12,847 reads

What Causes Malformed Lambda Proxy Response and How to Fix it

by Taavi RehemägiJanuary 18th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

What Causes Malformed Lambda Proxy Response and How to Fix it? Read this article by Taavi Rehemägi, CEO of Dashbird. What causes the error and how to fix it? The error is not a configuration error, but it could very well be that the routine that checks your return value also checks for other things that can be configured from the outside, like from AWS CloudFormation. After all, Lambda can be used for more than just handling API Gateway requests. It could be used to integrate with many AWS services and the Console UI to make it easier to fix the problem.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - What Causes Malformed Lambda Proxy Response and How to Fix it
Taavi Rehemägi HackerNoon profile picture

One problem that pops up quite frequently when people try to build serverless applications with AWS API Gateway and AWS Lambda is Execution failed due to configuration error: Malformed Lambda proxy response.

There is nothing worse than generic error messages that don’t tell you anything you need to fix the problem, right? And AWS isn’t particularly known for its error message design, if you can even call it that, let alone for giving you the means of fixing the problem. So how to fix this Lambda error and what causes it?

Fixing malformed Lambda proxy response

In order to fix this, you need to change what your Lambda function returns. And to do so, you need to return an object with two attributes:

  • statusCode
     – which is the HTTP status code you want to give your client with type number.
  • body
     – which is the content of your HTTP response with type string.
If you used an asynchronous function, it should look like this:
exports.handler = async function(event, context) {
  return {statusCode: 200, body: "OK"};
};
If you’re more of a promise type of developer, it would look like this:
exports.handler = function(event, context) {
  return new Promise(function(resolove, reject) {
    resolve({statusCode: 200, body: "OK"});
  });
};
And finally, if you’re into callbacks, this will resolve your problem:
exports.handler = function(event, context, callback) {
  callback({statusCode: 200, body: "OK"});
};

Understanding the Malformed Lambda proxy error response

If you have some time to spare, why not learn a bit about the “Why” of the error along the way?

First, Malformed Lambda proxy response isn’t really a configuration error because the problem lies in your Lambda code. But still, it could very well be that the routine that checks your return value also checks for other things that can be configured from the outside, like from AWS CloudFormation.

But what about the proxy part of that error message? 

If you configure a route for your API in AWS API Gateway in the AWS Console, you get to choose an integration for that route, in our case, a Lambda integration. This integration is the glue between AWS Lambda and AWS API Gateway. After all, Lambda can be used for more than just handling API Gateway requests.

If you configure the route via CloudFormation, things get a bit clearer. 
ExampleApi:
  ...
DefaultStage:
  ...
ExampleRoute:
  Type: AWS::ApiGatewayV2::Route
  Properties:
    ApiId:
      Ref: ExampleApi
    RouteKey: ANY /
    Target:
      Fn::Join:
        - integrations/
        - Ref: ProxyIntegration
ProxyIntegration:
  Type: AWS::ApiGatewayV2::Integration
  Properties:
    ApiId:
      Ref: ExampleApi
    IntegrationType: AWS_PROXY
    IntegrationUri:
      Fn::GetAtt:
        - ExampleFunction
        - Arn
    PayloadFormatVersion: "2.0"
ExampleRole:
  ...
ExampleFunction:
  Type: AWS::Lambda::Function
  Properties:
    Code:
      ZipFile:
        'exports.handler = async () => ({ statusCode: 200, body: "Example" });'
    Handler: index.handler
    Role:
      Fn::GetAtt:
        - ExampleRole
        - Arn
    Runtime: nodejs12.x
  DependsOn:
    - ExampleRole

I guess you already spotted the important point here. The integration we create in the AWS Console is actually a specific type of integration, called

AWS_PROXY
. It’s used to integrate with many AWS services and the AWS Console just has a nice UI to make integrating with Lambda simpler.

There are actually five types of API Gateway integrations. Three you can use for your WebSockets endpoints and two for the HTTP endpoints.

HTTP Integration Types

바카라사이트 바카라사이트 온라인바카라