10

I am using a Java lambda function to put a custom event to the AWS EventBridge. The target of this eventbridge is another Java lambda function. How to receive the Event in the target lambda function? I mean what is the input type in the handleRequest method I have to use? Tried using ScheduledEvent as an input type but it didn't work. Searched many EventBridge API documents but didn't get the details as how to receive the data in the Java lambda function from Eventbridge.

The below is an example for receiving the SQS Event. In the same way what type I should use for the events triggered from EventBridge?

@Override
  public String handleRequest(SQSEvent event, Context context)

3 Answers 3

2

I am able to access the Event as Map<String,Object>. The "detail" key in the map gives the actual values those are put in the Eventbridge.

2

Events from EventBridge are in the format of Cloudwatch ScheduledEvents. AWS Lambda Runtime can unmarshall the incoming json into com.amazonaws.services.lambda.runtime.events.ScheduledEvent:

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.ScheduledEvent;

public class Handler implements RequestHandler<ScheduledEvent, Boolean> {

    @Override
    public Boolean handleRequest(ScheduledEvent input, Context context) {
        return true;
    }
}
1

You need to change you request handler from using RequestHandler<SQSEvent, String> to using RequestHandler<Map<String,String>, String>. This will also lead to additional changes in your class / functions.

EventBridge events (schedules or your events) will show up in the input as an json encoded string.

Personally, I find it easier to leverage the RequestStreamHandler defined in https://docs.aws.amazon.com/lambda/latest/dg/java-handler.html#java-handler-interfaces. There's also some sample code linked that you may find helpful around deserialization.

1
  • Ah, I think one thing that wasn't clear to me at first (hence the other answers I gave, which I've deleted) is that you're saying the class should implement RequestHandler<Map<String,String>, String>, which means the method parameter should be Map<String,String>. It makes sense now, but the question showed the method signature, so the prescription of what to change it to in this answer wasn't clear to me at first. Commented Jun 27, 2023 at 1:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.