Execution failed due to configuration error: Malformed Lambda proxy response

0

Hi everyone,

I am working on a small cloud project, the goal of it is to invoke a Lambda function throughout Amazon API Gateway.

Business logic is not implemented yet but Lambda function is going to get email and password values from request body and then will persist these values in a DynamoDB table called "users"

API endpoint:

POST-> https://{API_ID}.execute-api.{REGION}.amazonaws.com/prod/user

Request body:

{
	"email":"test@mail.com",
	"password":"test12345"
}

Lambda implementation:

Right now as a way to validate lambda function is receiving request body correctly I am just getting email,password and then I return this values as function response

  const body = JSON.parse(event.body);
  
  const email = body.email;
  const password = body.password;

    return  {
    statusCode: 200,
    body: {
      email,
      password,
    },
  };

Lambda testing:

Function is being invoked from API Gateway in Manage Console

Enter image description here

Exception:

When clicking on Test button I get this exception

Response body

{"message": "Internal server error"}

...

Response headers

{
  "x-amzn-ErrorType": "InternalServerErrorException"
}

...

Execution failed due to configuration error: Malformed Lambda proxy response

Error description might suggest my response is not structured properly but I am not sure about it.

CDK Stack:

Here are the cloud resources and configuration I am provisioning

    // 1. Lambda function
    const userSignUpFn = new lambda.Function(this, "userSignUpFn", {
      functionName: "user-sign-up",
      runtime: lambda.Runtime.NODEJS_18_X,
      code: lambda.Code.fromAsset(
        path.join(__dirname, "../src/lambda/user-sign-up")
      ),
      handler: "index.handler",
      architecture: lambda.Architecture.X86_64,
    });

    // 2. ApiGateway
    const userApi = new apigateway.LambdaRestApi(this, "usersApi", {
      restApiName: "users-api",
      handler: userSignUpFn,
    });
    const user = userApi.root.addResource("user");
    user.addMethod("POST");

    // 3. DynamoDB table
    const usersTable = new dynamodb.Table(this, "usersTable", {
      tableName: "users",
      partitionKey: { name: "email", type: dynamodb.AttributeType.STRING },
      removalPolicy: cdk.RemovalPolicy.DESTROY,
    });

Thanks in advance for your assistance! :)

1 回答
2
已接受的回答

Hi,

Take a look at the following example, it may be of help to you.

export const handler = function(event, context, callback) {
    const body = JSON.parse(event.body)
    const email = body.email
    const password = body.password
    var res ={
        "statusCode": 200,
        "headers": {
            "Content-Type": "*/*"
        },
        "body": JSON.stringify({email, password})
    };
    callback(null, res);
};


profile picture
专家
已回答 2 个月前
profile picture
专家
已审核 2 个月前
  • I just updated and deployed Lambda code but got same error: Mon Mar 18 22:24:29 UTC 2024 : Execution failed due to configuration error: Malformed Lambda proxy response

  • Hi,

    I am sorry, I made a mistake. I already have updated the code example :)

    (In short, you need to execute an JSON.stringify before returning the response)

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则