7

I have set up http quarkus project using quarkus-amazon-lambda-http mvn dependency. It is working fine, but on top of that I want to add custom lambda handler for SQS events.

I have added sqs lambda handler

@Named("SqsHandler")
public class SqsHandlerLambda implements RequestHandler<InputObject, OutputObject> {

}

and added handler definition in application.properties: quarkus.lambda.handler=SqsHandler

Whenever I try to run it: mvn quarkus:dev it gives:

io.quarkus.builder.BuildException: Build failure: Multiple handler classes.  You have a custom handler class and the AWS Lambda HTTP extension.  Please remove one of them from your deployment

My idea was to use environment vairables in SAM template to deploy 2 functions, one which handle http requests, other SQS events. But is it possible to achieve what I want?

80% of the code base is the same for the Http events and SQS events, so there would be a lot of duplicate code if I do it in two separate code repositories.

1 Answers1

0

Lambda provides a lower level handler that I personally find a bit easier to deal with:

public void handleRequest(InputStream inputStream,
                          OutputStream outputStream,
                          Context context) throws IOException

This allows you do do something like:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

....

@Named("lambdaHandler")
public class LambdaHandler implements RequestStreamHandler {

    public void handleRequest(InputStream inputStream,
                              OutputStream outputStream,
                              Context context) throws IOException {

        LambdaLogger logger = context.getLogger();

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(inputStream);

        logger.log("input is " + objectMapper.writeValueAsString(rootNode));

        // i'm totally guessing at the path
        String value = rootNode.path("request").path("email").asText();

This would allow you to accept any input. The downside is that you need to do some introspection on the InputStream to determine what you got.

stdunbar
  • 16,263
  • 11
  • 31
  • 53