aws-lambdasamaws-http-api

AWS SAM APIGatewayProxyRequestEvent HttpMethod not set when starting API locally


I created an application with a AWS::SERVERLESS::FUNCTION, which has an HttpApi Event attached to it. I thought it would be a good idea to create one lambda per resource, so e.g. Post, Get and Put on /customer are all handled by a single lambda, which decides which action to take using

switch (input.getHttpMethod()) {
    case "GET": ...
    case "POST: ...
}

So now coming to my problem: When starting an application using sam local start-api my lambda gets called correctly, but neither input.getHttpMethod() nor input.getRequestContext().getHttpMethod() is set.

Given that SAM supports multiple HttpApi events, failing to provide the http method when running the application locally mitigates local development virtually completely. Am I doing something wrong, or is this really not working? I'm using Java btw, I can't tell if this problem exists using other languages as well.

Just in Case: Is my "one lambda per resource" approach wrong, should every single action have its own lambda?


Solution

  • I found the problem. HttpApi uses "PayloadFormatVersion: 2.0" as default. input was of type com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent, but this represents format 1.0, I had to use com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent to get it to work. This fails silently because instances are just constructed from json input, and several fields are the same among both classes.