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!