0

By default, Micronaut's handler is the class MicronautLambdaHandler. But on the Micronaut guide it says that

The main API you will interact with is AbstractMicronautLambdaRuntime. An abstract class which you can extend to create your custom runtime mainClass.

So I tried creating a custom handler and runtime, basically copying the existing MicronautLambdaHandler and MicronautLambdaRuntime

Custom MyRuntime

public class MyRuntime extends AbstractMicronautLambdaRuntime<AwsProxyRequest, AwsProxyResponse, AwsProxyRequest, AwsProxyResponse> {

    @Override
    protected RequestHandler<AwsProxyRequest, AwsProxyResponse> createRequestHandler(String... args) {
        try {
            return new MyHandler(createApplicationContextBuilderWithArgs(args));
        } catch (ContainerInitializationException e) {
            throw new ConfigurationException("Exception thrown instantiating MyHandler", e);
        }
    }

    public static void main(String[] args) {
        try {
            new MyRuntime().run(args);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}

Custom MyHandler that is just returning null

@Introspected
public class MyHandler implements RequestHandler<AwsProxyRequest, AwsProxyResponse>,
        ApplicationContextProvider, Closeable {

    protected final MicronautLambdaContainerHandler handler;

    public MyHandler() throws ContainerInitializationException {
        this.handler = new MicronautLambdaContainerHandler();
    }

    public MyHandler(ApplicationContextBuilder applicationContextBuilder) throws ContainerInitializationException {
        this.handler = new MicronautLambdaContainerHandler(applicationContextBuilder);
    }

    public MyHandler(ApplicationContext applicationContext) throws ContainerInitializationException {
        this.handler = new MicronautLambdaContainerHandler(applicationContext);
    }

    @Override
    public AwsProxyResponse handleRequest(AwsProxyRequest input, Context context) {
        System.out.println("hello custom handler");
        System.out.println(context.getAwsRequestId());
        return null;
    }

    @Override
    public ApplicationContext getApplicationContext() {
        return this.handler.getApplicationContext();
    }

    @Override
    public void close() {
        this.getApplicationContext().close();
    }
}

Then on my Lambda Function I set the method handler to example.micronaut.MyHandler and the runtime to Custom Runtime on Amazon Linux 2

When testing my Lambda it doesn't read my custom handler. It always run the default MicronautLambdaHandler.

Also, what's weird is that even if I set the method handler to some random words like this.is.not.a.handler, it still run the MicronautLambdaHandler by default.

Is there a way to create my custom handler for Micronaut Lambda as Native Image as the guide suggests? Or I'm just really limited to using the default MicronautLambdaHandler?

berty
  • 160
  • 9

1 Answers1

1

Was able to make it work. I need to explicitly put the dependencies implementation("io.micronaut.aws:micronaut-function-aws") implementation("io.micronaut.aws:micronaut-function-aws-custom-runtime")

What I don't understand is that these artifacts already exist on the classpath even without specifying on build.gradle, that's why I never put them.

berty
  • 160
  • 9
  • This works, but the `input.getRecords()` gives the empty messages (in case of `SQSEvent`). Any idea how we can retrieve the input message body? – Jignesh M. Khatri Aug 31 '22 at 11:19