Search code examples
javaaws-lambdaamazon-sqs

Return value of an AWS (Java) Lambda function


Suppose I have the following code

    public final class MyLambdaFunction implements RequestHandler<SQSEvent, String> {
           @Override
        public String handleRequest(SQSEvent event, Context context) {
           return "Hello World";
        }
    }

I don't understand the purpose of having an output value (a String, in the example above), so I was wondering if I can use that value somewhere else (e.g. automatically send the return value to another SQS queue)?


Solution

  • From what is written in documentation you might update return type to match your class like

    public final class MyLambdaFunction implements RequestHandler<SQSEvent, MyClass> {
        @Override
        public MyClass handleRequest(SQSEvent event, Context context) {
            MyClass myClassInstance = new MyClass();
            return myClassInstance;
        }
    }
    

    This is due to RequestHandler being a lambda and being able to answer to Http requests (and returning complex objects simplifies development).
    However, to automatically send to another SQS queue you might use SQS sdk in your lambda to publish in another one before returning myClassInstance.

    Keep in mind that RequestHandler is generic to handle any usecase not only SQSEvent