1

How will the Lambda handler of multiple different types of trigger look like in java? I want to set a trigger for Cloudwatch Event Rule, S3. How can I do this?

public class App implements RequestHandler<S3Event, Context>
{
public Context handleRequest(S3Event s3event, Context context) {
     System.out.println("welcome to lambda");
    }
}
4
  • Do you mean a single lambda will be triggered by multiple different types of trigger (S3Event and ScheduledEvent, for example) ? Commented Mar 19, 2021 at 9:21
  • @mattfreake Yes Commented Mar 19, 2021 at 10:14
  • Why do you want to do this? Commented Mar 19, 2021 at 12:54
  • The stream solution below definitely works, but lambdas are meant to be small units of code, so it can be a red flag that your lambda is trying to do too much. You are also limiting your ability to manage the processing of the types of event differently, if you only have one lambda whose concurrency etc you can modify Commented Mar 20, 2021 at 8:27

2 Answers 2

2

You can use Map<String,String> as input

public class Handler implements RequestHandler<Map<String,String>, String>{
  @Override
  public String handleRequest(Map<String,String> event, Context context) {
  }
}

Or use RequestStreamHandler and parse the InputStream to the correct object.

public class HandlerStream implements RequestStreamHandler {
  @Override
  public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

using a generic Object could work, I use Serverless Framework with:

package com.serverless;

import java.util.Collections;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

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

public class Handler implements RequestHandler<Object, Object> {

    private static final Logger LOG = LogManager.getLogger(Handler.class);

    @Override
    public Object handleRequest(final Object input, final Context context) {
        LOG.info("received: {}", input);
        return input;
    }
}

Comments

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.