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
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! :)
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)