Let’s compile your Aws Lambda in Java

Introduction

Your Current lambda

  • src/main/java/example/App.java
  • pom.xml
mvn package
  • Give a name to your lambda, for example Hello-Java
  • Choose Java 11 (Corretto) as the Runtime
  • upload your deployment package (hello-lambda-java11–1.0-SNAPSHOT-shaded.jar) from the console or via S3
  • Define example.App::sayHello as the Handler (where example.App is the full classname and sayHello is the method name).

Current Performance

  • There is no Init Duration indicating that it is effectively a hot start
  • In that case, the user will see a total time of 1 ms only! (Note: We should include network time + de-serialization time...)
  • the lambda execution environment
  • the custom lambda runtime and Java RIC
  • graalVM native-image
  • docker

Understanding the lambda execution environment

Architecture diagram of the execution environment
  • The Lambda Function to execute the business code (your code) containing the Handler
  • The Lambda Runtime responsible for providing the Lambda Function the event to use and controlling its. execution. It consists of an execution loop waiting for a new event and retrieving it from the Runtime API, then posting the return value from the Lambda Function to the Runtime API (see more here).
  • The Runtime API providing a REST API mainly GET /runtime/invocation/next and POST /runtime/invocation/<AwsRequestId>/response respectively for retrieving the AwsRequest and to send the response (see more here).

The custom lambda runtime and Java RIC

  • Bundle the native code in a zip containing a file named bootstrap that will be the entry point.
  • But also bundle our custom lambda runtime inside the native code!
  • So the native code must be the compilation of our lambda function + the custom lambda runtime (for linux x86–64).
com.amazonaws.services.lambda.runtime.api.client.AWSLambda
bootstrap : our bootstrap scriptfunc : our native executable

GraalVM Native-Image

native-image -jar hello-lambda-custom-1.0-SNAPSHOT.jar

Docker

docker build -t hello-lambda:latest .
docker create hello-lambda:latest
docker cp <container_id>:/function/function.zip .
docker container rm <container_id>

The Solution with the custom runtime

  • choose the custom runtime (AL2)
  • set the Handler to: example.App::sayHello

Let’s test the performance

Bonus: Run the lambda inside a container

docker run -p 9000:8080 hello-lambda:latest
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'

--

--

--

Founder and CTO of Solusoft.Tech, Java Expert & Architect, AWS Serverless happy user and flutter early adopter.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Netflix not working on laptop

Restart docker from another docker on Windows 10 (Windows container)

665: The First Steps

UDF in PySpark….An Expensive way of creating functions.

Application development in 2020

This Is Us

Create a “Real-world” Data Science environment at home

My Computer Engineering Master’s Degree Process

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Frank Afriat

Frank Afriat

Founder and CTO of Solusoft.Tech, Java Expert & Architect, AWS Serverless happy user and flutter early adopter.

More from Medium

GitHub Packages: Publish and Consume Artifacts of a Spring Gradle Project

Spring Cloud Gateway with logs

Store JSON as VARCHAR in Oracle DB 12c ,Spring Boot JPA

Let's create an API in Spring Boot