EGHDK
EGHDK

Reputation: 18130

How do I define a function for AWS Lambda to start in Java?

I'm using this doc as a tutorial http://docs.aws.amazon.com/lambda/latest/dg/get-started-step4-optional.html

The entry method/function that they supply for AWS lamda looks like this:

public String myHandler(int myCount, Context context) {
    LambdaLogger logger = context.getLogger();
    logger.log("received : " + myCount);
    return String.valueOf(myCount);
}

My issue is that I don't know what arguments I can define and how AWS lambda knows what to pass to me. It'd be great to see all potential method signatures I could come up with. Maybe I'm just not looking in the right place for this documentation, but I'd love to be able to do something like

public String myHandler(String json) {
   MyJsonValidatorClass validator = new ...;
boolean isValid = validator.isValidJson(json);
    return String.valueOf(isValid);
}

I'm just not sure what I'm able to do in AWS Lamdas really. When writing a java main application, I know that I have String[] args to deal with and nothing else. Am I missing something here? Or am I just thinking about this totally wrong?

Upvotes: 1

Views: 1889

Answers (1)

zapl
zapl

Reputation: 63955

The lambda runtime uses reflection to see what type your method wants as it's first parameter, then tries to parse the raw input data according to that specification. The types it supports are listed here:

  • Simple Java types (AWS Lambda supports the String, Integer, Boolean, Map, and List types)
  • POJO (Plain Old Java Object) type
  • Stream type (If you do not want to use POJOs or if Lambda's serialization approach does not meet your needs, you can use the byte stream implementation. [..])

Examples for how handler methods would look like are

// you could do your own json parsing in here
String handler(String input, Context context)

// lambda parses json for you
JoinResponsePojo handler(JoinRequestPojo request, Context context)

// when even String is not enough
void handler(InputStream inputStream, OutputStream outputStream, Context context)

For convenience and to help you prevent errors, there are the RequestHandler and RequestStreamHandler interfaces which capture exactly above method signatures (docs). I'd usually use those rather than freestyle-implementing handler methods.

Usually the most convenient way is to work with POJOs directly, since usually the input is json. There are also some predefined POJOs for common events in aws-lambda-java-events you can use. Or you can write your own like outlined in "Example: Using POJOs for Handler Input/Output (Java)"


js callbacks are used to return data, so your example is either

public class ExampleHandler1 implements RequestHandler<String, String> {
    @Override
    public String handleRequest(String input, Context context) {
        // would preferably use some other way to generate json
        return "{\"speech\": \"hello theres\"}";
    }
}

or using a pojo like

public class ExampleHandler2 implements RequestHandler<String, Speech> {

    public static class Speech {
        private String speech;
        public String getSpeech() {
            return speech;
        }
        public void setSpeech(String speech) {
            this.speech = speech;
        }
    }

    @Override
    public Speech handleRequest(String input, Context context) {
        Speech speech = new Speech();
        speech.setSpeech("hello theres");
        return speech;
    }
}

Upvotes: 3

Related Questions