0

I want to write an AWS Lambda that uses the event invocation type. According to java-programming-model-handler-types I should do the following:

If you plan to invoke the Lambda function asynchronously (using the Event invocation type), the outputType should be void. For example, if you use AWS Lambda with event sources such as Amazon S3, Kinesis, and Amazon SNS, these event sources invoke the Lambda function using the Event invocation type.

I am looking for an example on how to write such a handler (Java) method.

There are plenty of examples for a Java handler that works with RequestResponse invocation type (e.g., public String myHandler(int myCount, Context context)). There are also examples for using streams (which, IIUC, are also just for RequestResponse invocation type). I could not find any example for a Java Lambda whose handler is handling an Event invocation type

1 Answer 1

2

It will look like this:

public class MyFunction implements RequestHandler<eventType, Void> {

    public Void handleRequest(eventType event, Context context) {

        ...
        return null;
    }

}

The trick is knowing what class your event type will be. You can set eventType to Object, cause your function to be triggered once, and have it print the class name in the function to find out what the event type is going to be.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I want to use API Gateway events, so IIUC it will be an APIGatewayProxyRequestEvent object (see github.com/aws/aws-lambda-java-libs/tree/master/…). Do you happen to know what is the API for sending back an (async) response of type APIGatewayProxyResponseEvent?
If API Gateway is calling the Lambda function then I think you will get Map<String,String> instead of an Event type, and you're function will not be void. Not sure what you mean by "async response". All this is a million times easier in NodeJS or Python by the way, and those languages also perform better on Lambda than Java does.
@MarkB "and those languages also perform better on Lambda than Java does." What makes you think that?
@cy3er Java has cold start issues on Lambda. It takes much longer for a Java Lambda function to be deployed and started in a new Lambda container than it does a NodeJS or Python function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.