0

I have an AWS Lambda function in Java that uses APIGatewayProxyRequestEvent (LAMBDA_PROXY) to get input from API gateway. For API calls through the API Gateway, I can get and print the input body without a problem.

Now I want an EventBridge scheduled event that triggers the Lambda function every 5 minutes. However, I can NOT get the event payload as it shows null when I print it in the codes below.

Lambda function:

@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input,
        Context context) {

    logger.info("RawBody: {}", input.getBody());
    // RawBody: null

EventBridge event payload:

{"key": "value"}

Any idea how I can get the event payload from EventBridge in my Lambda function?

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • How is Java supposed to convert that input into an ApiGatewayEvent? It can't. Solution: do not accept that event but a more generic thing, e.g. simply a string and then do the parsing yourself based on some logic. – luk2302 Aug 07 '23 at 06:58

1 Answers1

0

The Lambda function utilizes serialization to convert the JSON payload into the specified JAVA class type. According to your lambda function implementation, all JSON payloads are being converted to the APIGatewayProxyRequestEvent type. As a result, since the EB payload doesn't match the APIGatewayProxyRequestEvent type, you received a null value when triggering the lambda function from the EB scheduler.

To trigger the mentioned lambda function from both API Gateway and EB, you can follow any one of the below-mentioned steps:

  • If you plan to simulate the API request from the EB scheduler, then make sure to modify your payload to represent the APIGatewayProxyRequestEvent. You can find the format details for the API Gateway proxy event in this document.

  • Alternatively, instead of directly calling the lambda function, you can utilize the EB to call your API Gateway endpoint. By doing so, you can pass the payload directly to the API endpoint, and the API Gateway will internally invoke the lambda function. For more information on triggering API Gateway endpoints from EB, refer to this document.

  • However, if your use case for the EB scheduler serves an entirely different purpose than the API Gateway endpoints, I strongly recommend to use a separate lambda function to handle the EB scheduler events.

  • If you're just doing an experiment and want to create a lambda function that accepts more than one event trigger, then check out this SO answer.

codeninja.sj
  • 3,452
  • 1
  • 20
  • 37