Sergio Belli
Sergio Belli

Reputation: 61

Quarkus + AWS Lambda

I have this class that represents the handler AWS Lambda handler of my simple application.

The application is realized through out the Quarkus platform.

package net.sergiobelli.handlers;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import net.sergiobelli.model.Request;
import net.sergiobelli.model.Response;
import net.sergiobelli.services.HelloService;

@Named("test")
@ApplicationScoped
public class RestLambdaFunctionHandler implements RequestHandler<Request, Response> {
    @Inject HelloService helloService;
    public Response handleRequest(Request request, Context context) {
        String name = request.getName();
        String surname = request.getSurname();
        String message = helloService.sayHelloByNameAndSurname(name, surname);
        return new Response(message);
    }
}

As you can see, it is defined a CDI Injection of HelloService component.

Take a look at the deplendency section of my pom.xml.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>${quarkus.platform.group-id}</groupId>
            <artifactId>${quarkus.platform.artifact-id}</artifactId>
            <version>${quarkus.platform.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-amazon-lambda</artifactId>
    </dependency>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <scope>test</scope>
    </dependency>       
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-arc</artifactId>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-resteasy</artifactId>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-junit5</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Seems to be good.

Then I run "mvn clean install -Dnative -DskipTests" in order to create the native package to upload in AWS Lamba console.

The next step is to configure the "Runtime settings" of the Lambda function. In the handler section is set "net.sergiobelli.handlers.RestLambdaFunctionHandler::handleRequest".

Again, in my opinion it seems to be correct.

Then I test the application from the AWS console.

This is the result : { "errorMessage": "Class not found: net.sergiobelli.handlers.RestLambdaFunctionHandler", "errorType": "java.lang.ClassNotFoundException" }

Any suggestion?

I just tried to deploy the app ad a single jar, in this case I obteined a CDI injection error that lead the flow to a NullPointerException.

In general, is it possible to deploy Quarkus App with CDI capabilities as AWS Lambdafunctions?

Thank you.

Upvotes: 0

Views: 412

Answers (1)

Sergio Belli
Sergio Belli

Reputation: 61

Solved!

The handler class has to be annotated with @Named, like this :

@Named("RequestHelloHandler") 
public class RestLambdaFunctionHandler implements RequestHandler<Request, Response> { ... }

Then you MUST have to place this row inside the quarkus application.properties :

quarkus.lambda.handler=RequestHelloHandler

Now you have to package the application in native flavour :

mvn clean install -Dnative -DskipTests

That operation leads to the creation of a "function.zip" zipped file under project target folder.

The last steps are :

  1. create a lambda function with custom runtime (I choose Amazon Linux 2023)
  2. in the "code" tab of the lambda detail page set "io.quarkus.amazon.lambda.runtime.QuarkusStreamHandler::handleRequest" has Runtime settings handler
  3. upload the "function.zip
  4. test the application by passing a json payload

Aws Lambda should run you bootstrap file contained in the zipped file and should not be raised CDI errors!

Thank you for your suggestions, hope this helps other guys in future! Best regards.

Upvotes: 1

Related Questions