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.

Requirements

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

micronaut-function.png

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:

micronaut-structure.png

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

FunctionLambdaRuntime.java

package mn.aws.lambda.s3; import com.amazonaws.services.lambda.runtime.RequestHandler; import com.amazonaws.services.lambda.runtime.events.SQSEvent; import io.micronaut.core.annotation.Nullable; import io.micronaut.function.aws.runtime.AbstractMicronautLambdaRuntime; import java.net.MalformedURLException; 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(); } }

FunctionRequestHandler.java

package mn.aws.lambda.s3; import com.amazonaws.services.lambda.runtime.events.SQSEvent; import io.micronaut.core.annotation.Introspected; import io.micronaut.function.aws.MicronautRequestHandler; @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; } }

Build

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!

gradle-build-lambda-zip.png

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

lambda-micronaut-zip.png

Deploy

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

    set-handler.png
  3. upload the zip:

Upload the zip that gradle generated under :

build/libs/mn-aws-lambda-s3-0.1-lambda.zip
upload-lambda.png

Test

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

sqs-tst.png

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

Result:

running-micronaut-lambda.png
Last modified: 12 March 2024