I am using LocalStack Community to try a lambda to be called by an API Gateway.

import java.util.Map;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;

public class HandlerHelloGateway implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
    @Override
    public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent event, Context context) {
        String helloValue = event.getQueryStringParameters() != null ? event.getQueryStringParameters().get("hello")
                : "default value";
        String responseBody = String.format("{\"message\": \"Input was: %s\"}", helloValue);
        return new APIGatewayProxyResponseEvent()
                .withStatusCode(200)
                .withHeaders(Map.of("Content-Type", "application/json"))
                .withBody(responseBody);
    }
}

I created a zip:

HandlerHelloGateway.zip: hellocucumber-1.0.0-SNAPSHOT.jar
	cp target/classes/HandlerHelloGateway.class .
	cp target/hellocucumber-1.0.0-SNAPSHOT.jar .
	zip -r HandlerHelloGateway.zip hellocucumber-1.0.0-SNAPSHOT.jar HandlerHelloGateway.class
	rm HandlerHelloGateway.class hellocucumber-1.0.0-SNAPSHOT.jar

Then create the lambda by:

awslocal lambda create-function --function-name HandlerHelloGateway \
    --zip-file fileb://HandlerHelloGateway.zip \
    --handler HandlerHelloGateway::handleRequest \
    --runtime java21 \
    --role arn:aws:iam::123456789012:role/lambda-execution-role

call this lambda by:

awslocal lambda invoke --function-name HandlerHelloGateway output.txt --cli-binary-format raw-in-base64-out --payload '"hello"'

Output.txt shows “Error loading method handleRequest on class HandlerHelloGateway”.

If I replace the above lambda function with the below code, then Output.txt shows the expected result.

public class HandlerHelloGateway implements RequestHandler<String, String> {
   @Override
   public String handleRequest(String input, Context context) {
    return "Hello from LocalStack Lambda! Input was: " + input;
  }
}

Any idea why the lambda implementing RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> does not work? Is it not supported by LocalStack Community? Thanks very much!

Hi @jiangok, thank you for your question.

The APIGatewayProxyRequestEvent represents an event coming from an apigateway Lambda Proxy integration. There is a specific payload shape required and you must create a Rest api to invoke properly.

You can learn more about Lambda Proxy integration from the aws documentation. You will also find the required payload shape on this page.

We also have an example in our documentation on how to create a Rest Api on LocalStack.

If after following these steps you are still seeing an error, please share with us your debug log by running LocalStack with DEBUG=1 so we can get more insight into what is happening.