'Create and deploy Micronaut AWS Lambda GraalVM Native Image CRUD Application to handle APIGatewayV2HttpEvent by delegating to Controller

I am trying to deploy (via Docker) a CRUD Micronaut AWS Lambda (Function/Application?) that should handle APIGatewayV2HTTPEvent requests and should compile into native-image, but couldn't find concise guide to do that so I am trying to gather info from different sources and mix them together to achieve what I want.

The idea is having a single handler similar to this one:

@Introspected
public class BookRequestHandler extends MicronautRequestHandler<Book, BookSaved> {

    @Override
    public BookSaved execute(Book input) {
        BookSaved bookSaved = new BookSaved();
        bookSaved.setName(input.getName());
        bookSaved.setIsbn(UUID.randomUUID().toString());
        return bookSaved;
    }
}

handling an APIGatewayV2HttpEvent by delegating it to BookController instead of having multiple BookRequestHandler (ergo multiple Lambdas) handle each type of endpoint.

I have created boilerplate app via this command:

mn create-app example.micronaut.micronautguide --features=graalvm,aws-lambda --build=maven --lang=java

and I see the entry point of the application specified in pom.xml is:

<exec.mainClass>io.micronaut.function.aws.runtime.MicronautLambdaRuntime</exec.mainClass>

I've specified same above entry point in Lambda definition in Terraform.

When I build dockerfile and try to run

RUN /usr/lib/graalvm/bin/native-image --no-server -cp target/api-*.jar

I get:

│ Error: Main entry point class
│ 'io.micronaut.function.aws.runtime.MicronautLambdaRuntime' not found.

which makes sense because the class isn't in target folder

Dockerfile:

FROM maven:3.6.3-openjdk-11 as builder
COPY . /home/application
WORKDIR /home/application
RUN mvn -e package
FROM amazonlinux:2018.03.0.20191014.0 as graalvm

ENV LANG=en_US.UTF-8

RUN yum install -y gcc gcc-c++ libc6-dev  zlib1g-dev curl bash zlib zlib-devel zip

ENV GRAAL_VERSION 20.1.0
ENV JDK_VERSION java11
ENV GRAAL_FILENAME graalvm-ce-${JDK_VERSION}-linux-amd64-${GRAAL_VERSION}.tar.gz

RUN curl -4 -L https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-${GRAAL_VERSION}/${GRAAL_FILENAME} -o /tmp/${GRAAL_FILENAME}

RUN tar -zxvf /tmp/${GRAAL_FILENAME} -C /tmp \
    && mv /tmp/graalvm-ce-${JDK_VERSION}-${GRAAL_VERSION} /usr/lib/graalvm

RUN rm -rf /tmp/*
CMD ["/usr/lib/graalvm/bin/native-image"]

FROM graalvm
COPY --from=builder /home/application/ /home/application/
WORKDIR /home/application
RUN /usr/lib/graalvm/bin/gu install native-image
RUN /usr/lib/graalvm/bin/native-image --no-server -cp target/api-*.jar
RUN chmod 777 bootstrap
RUN chmod 777 api
RUN zip -j function.zip bootstrap api
EXPOSE 8080
ENTRYPOINT ["/home/application/api"]

So the broad question is: is it possible to create native-image CRUD Lambda Function that acts as backend server that handles APIGatewayV2HttpEvent requests by delegating them to controller (fundamentally one single API handler delegating to controller)?

Micronaut version: 3.4.2
Lambda runtime: "provided.al2"

Sorry for the articulated question and thanks a lot!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source