Serverless not creating API correctly

I’m trying to use Serverless to set up a basic REST API for a web app. I’ve written and tested my Lambda functions, and they all work perfectly (even with Serverless if I use the “invoke” command) and return the expected results. However, my problem starts when I add an HTTP event, particularly a post event. Here is how my serverless.yml file is set up:

functions:
  getPlayers:
    handler: getPlayers.getPlayers
    events:
      - http:
          path: players
          method: get
          integration: lambda
  addPlayer:
    handler: addPlayer.addPlayer
    events:
      - http:
          path: players
          method: post
          integration: lambda
  deletePlayer:
    handler: deletePlayer.deletePlayer
    events:
      - http:
          path: players
          method: delete
          integration: lambda

I now have my getPlayers API call working correctly (had to add integration: lambda to make it work), but I cannot get addPlayer or deletePlayer to work. When I go to AWS console and test my addPlayer API directly, I get the following error:

{
  "errorMessage": "Process exited before completing request"
}

I should not be getting these errors since I’m calling the callback() function correctly in my Lambda code. If I plug the exact same inputs into my Lambda function, I get the correct outputs, so something is clearly wrong with how Serverless is setting up my API. Does anyone have suggestions for how to fix this? I would really appreciate the help!

1 Like

I figured out my own problem! For anyone else looking for the solutions to this, it turns out that something Serverless does transforms your JSON input to the API. So I was putting in the following JSON:

{
	"email": "shroo",
	"score": 47
}

but it was being transformed into:

{
...
    "body": {
	"email": "shroo",
	"score": 47
    }
...
}

so my Lambda function had to be changed to call event.body.email instead of event.email like it was before.

2 Likes

Nice work posting the answer to your own question - I wish more people would do that!

1 Like