Java / Maven

Micronaut RequestStreamHandler as a GraalVM Native Executable to AWS Lambda

Learn how to distribute a Micronaut function with an AWS Lambda RequestStreamHandler built as a GraalVM native executable to an AWS Lambda custom runtime.

Sergio del Amo
On this guide
In this section

In this guide, we will deploy a Micronaut serverless function to AWS Lambda using a RequestStreamHandler.

Getting Started

In this guide, we will create a Micronaut application written in Java.

What you will need

To complete this guide, you will need the following:

Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

Writing the Application

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-function-app example.micronaut.micronautguide \
    --features=aws-lambda,graalvm \
    --build=maven \
    --lang=java
Note
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.
If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.

If you use Micronaut Launch, select serverless function as application type and add the aws-lambda feature.

The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.

RequestStreamHandler

To use your own serialization, implement the RequestStreamHandler interface. With this interface, Lambda passes your handler an input stream and output stream. The handler reads bytes from the input stream, writes to the output stream, and returns void.

The application contains a class extending MicronautRequestStreamHandler

Replace the generated FunctionRequestHandler with the following:

src/main/java/example/micronaut/FunctionRequestHandler.java

You will use MicronautRequestStreamHandler in combination with a class annotated with @FunctionBean that implements one of the interfaces from the java.util.function package.

src/main/java/example/micronaut/RequestFunction.java

Tests

You can test the handler behaves as expected, as shown in the following example:

src/test/java/example/micronaut/FunctionRequestHandlerTest.java

Testing the Application

To run the tests:

./mvnw test

Lambda

Create a Lambda function. For the runtime, select Custom Runtime. Select the x86_64 or arm64 architecture to match the computer you use to build the GraalVM Native Image.

create function bootstrap

The Micronaut framework eases the deployment of your functions as a Custom AWS Lambda runtime.

The main API you interact with is AbstractMicronautLambdaRuntime. This abstract class can be subclassed to create your custom runtime mainClass. That class includes the code to perform the Processing Tasks described in the Custom Runtime documentation.

LambdaRuntime

Create a class that will be the entry point of the GraalVM native image.

java/src/main/java/example/micronaut/FunctionLambdaRuntime.java
package example.micronaut;

import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.function.aws.runtime.AbstractRequestStreamHandlerMicronautLambdaRuntime;

import java.net.MalformedURLException;

class FunctionLambdaRuntime extends AbstractRequestStreamHandlerMicronautLambdaRuntime<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {

    public static void main(String[] args) {
        try {
            new FunctionLambdaRuntime().run(args);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected @Nullable RequestStreamHandler createRequestStreamHandler(String... args) {
        return new FunctionRequestHandler();
    }
}

Build GraalVM Native Executable

./mvnw package -Dpackaging=docker-native -Dmicronaut.runtime=lambda -Pgraalvm

The above command generates a ZIP file which contains a GraalVM Native Executable of the application, and a bootstrap file which executes the native executable. The GraalVM Native Executable of the application is generated inside a Docker container.

Once you have a ZIP file, upload it.

lambda custom runtime uploadcode

Handler

The handler used is the one created at FunctionLambdaRuntime.

Thus, you don’t need to specify the handler in the AWS Lambda console.

However, you can specify it in the console as well:

example.micronaut.FunctionRequestHandler

lambda custom runtime functionrequest handler

Test

You can test it easily with a JSON Event:

{
  "path": "/",
  "httpMethod": "GET",
  "headers": {
    "Accept": "application/json"
  }
}

When working with Amazon API Gateway, it’s easy to use apigateway-aws-proxy as an Event Template to get started:

test event

You should see a 200 response:

test result

Next Steps

Explore more features with Micronaut Guides.

Read more about:

Help with the Micronaut Framework

The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.

License

Note
All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images).