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)

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠