Iqbal´s DLQ main Help

Build a native AWS Lambda with Java (GraalVM + AWS Lambda + Micronaut 4 - SQS Queue Trigger)

In this article, we will be building a similar Lambda to the one in GraalVM with AWS Lambda - Starter To summarize the previous article, we manually:

  1. created a custom runtime for the lambda

  2. created a graalvm build environment on AL2 with Docker

  3. packaged the lambda as zip on the pipeline

With the help of the Micronaut v4.2 framework that has excellent support for AWS Lambda, we will be achieving the same goal with the framework´s provided tooling.


You will need for this tutorial :

  • jdk 17

  • Micronaut cli (optional)

  • Docker

  • gradle / maven

Generate a project

With the help of the Micronaut Launch page or the Micronaut CLI, you can easily generate a skeleton for your Aws Lambda.

On the Launch page:

  • Pick the Function "Application for Serverless" as the application type

  • Add the features listed below


or via MN CLI:

mn create-function-app --build=gradle_kotlin --jdk=17 --lang=java --test=junit --features=aws-lambda,aws-lambda-events-serde,aws-lambda-custom-runtime,graalvm com.example.demo

Tailor the generated lambda to SQS Queue trigger / SQS Event Consumption

The CLI generates a project with the structure:


By default, the Launcher generates a Lambda for API Proxy Gateway event. let´s adapt for SQS Events:

package; import; import; import io.micronaut.core.annotation.Nullable; import; import; public class FunctionLambdaRuntime extends AbstractMicronautLambdaRuntime<SQSEvent, Void, SQSEvent, Void> { public static void main(String[] args) { try { new FunctionLambdaRuntime().run(args); } catch (MalformedURLException e) { e.printStackTrace(); } } @Override @Nullable protected RequestHandler<SQSEvent, Void> createRequestHandler(String... args) { return new FunctionRequestHandler(); } }

package; import; import io.micronaut.core.annotation.Introspected; import; @Introspected public class FunctionRequestHandler extends MicronautRequestHandler<SQSEvent, Void> { @Override public Void execute(SQSEvent input) { System.out.println(" EVENT RECEIVED - IA - " + input.getRecords().get(0).getBody()); return null; } }


Thanks to Micronaut, this step is now as easy as:

gradle buildNativeLambda

it will start a build environment in docker with graalvm on AL2 and generate a zip for you!


Output with the native image and custom bootstrap for the Lambda:



Once the build finishes, set up your lambda as follows:

  1. Set Runtime to "custom runtime with Amazon Linux 2"

  2. Set handler to your own implementation FunctionRequestHandler

  3. upload the zip:

Upload the zip that gradle generated under :



Let´s run a test once your artefact uploaded:

  1. Provide a name for your event

  2. Choose the SQS Template since we set the serialization for this format

  3. hit Test


There you go, you just built a native lambda with Java, thanks to Micronaut and GraalVM :)


Last modified: 12 March 2024