0
votes

I have followed this tutorial: https://www.serverless.com/blog/node-rest-api-with-serverless-lambda-and-dynamodb. It guides you on building a Lambda function that takes three parameters and populates a DynamoDB database with an Item built from these parameters.

Everything works as expected when I run things from my console using CURL. However, if I test things via the Lambda Console I get an error, and it seems to be due to the 'event' object in my handler's parameters being shaped differently depending on where I call the Lambda function from (which is weird).

I tested the following:

module.exports.submit = async (event, context) => {
  return {
    statusCode: 200,
    body: Object.keys(event).join()
  }
}

And using CURL with the following statement I get(personal details are from the Tutorial):

curl -H "Content-Type: application/json" -X POST -d '{"fullname":"Shekhar Gulati","email": "[email protected]", "experience":12}' https://__.execute-api.eu-west-2.amazonaws.com/dev/__

The response from this CURL statement is quite long

resource,path,httpMethod,headers,multiValueHeaders,queryStringParameters,multiValueQueryStringParameters,pathParameters,stageVariables,requestContext,body,isBase64Encoded

On the other hand, in the console, the response is simply:

firstname,email,experience

Should I just ignore the console for testing purposes or am I missing something?

1
When you test via curl it's going through API Gateway which is adding lots of things to the event object. When you test directly in the Lambda console you probably aren't passing all those API Gateway values in the test event object you created, thus the difference. - Mark B

1 Answers

1
votes

When calling your endpoint using curl or Postman, it sends an HTTP request to API Gateway. API Gateway then creates a specific event object and passes that to the Lambda function. When you invoke the Lambda directly in the console you pass whatever event object you create manually. The way to test your Lambda function directly would be to take an example API Gateway event and modify it to fit what you are trying to test. you can do this by logging out the event object from the Lambda when called using an HTTP request.