Home Java javaTutorial Lambda function with GraalVM Native Image - Part ow to develop and deploy Lambda function with custom runtime

Lambda function with GraalVM Native Image - Part ow to develop and deploy Lambda function with custom runtime

Oct 21, 2024 pm 04:16 PM

Introduction

In the part 1 of the series we gave an introduction to the GraalVM and especially its Native Image capabilities. We also explained its benefits for the Serverless applications. In the this part of the series we'll explain how to develop and deploy AWS Lambda function with custom runtime containing GraalVM Native Image.

Sample Application

For the sake of explanation we'll use our sample application. In this application we'll create and retrieve products and use DynamoDB as the NoSQL database. We'll re-use the application introduced in the article Measuring Java 21 Lambda cold starts and adjust it to be deployed as Lambda Custom Runtime containing GraalVM Native Image.

Lambda function with GraalVM Native Image - Part ow to develop and deploy Lambda function with custom runtime

General Setup

In order to build GraalVM Native Image we'll need to do the following:

  • Setup m5.large AWS Cloud9 EC2 instance. You can of course use your own (local) Linux environment.
  • Install SDKMAN
curl -s "https://get.sdkman.io" | bash  

source "/home/ec2-user/.sdkman/bin/sdkman-init.sh"
Copy after login
Copy after login
Copy after login
  • Install the latest GraalVM version. I used version 22 in my example (but you can use the newest one):
sdk install java 22.0.1-graal  (or use the newest GraalVM version)
Copy after login
Copy after login
  • Install Native Image
sudo yum install gcc glibc-devel zlib-devel 
sudo dnf install gcc glibc-devel zlib-devel libstdc++-static
Copy after login
Copy after login
  • Install Maven capable of building with the installed GraalVM version. We need Maven version capable of dealing with the Java 21 and higher version of the source code. For example :
wget https://mirrors.estointernet.in/apache/maven/maven-3/3.8.5/binaries/apache-maven-3.8.5-bin.tar.gz tar -xvf apache-maven-3.8.5-bin.tar.gz sudo mv apache-maven-3.8.5 /opt/

M2_HOME='/opt/apache-maven-3.8.5' 

PATH="$M2_HOME/bin:$PATH" 

export PATH
Copy after login
Copy after login

If this mirror becomes unavailable, please use another one available for your operating system.

Making sample application GraalVM Native Image capable

In order to our sample application to run as GraalVM Native Image we need to declare all classes which objects will be instantiated per reflection. These classes needed to be known by AOT compiler at compile time. This happens in reflect.json. As we can see we need to declare there

  • all our Lambda functions like GetProductByIdHandler and CreateProductHandler
  • entities like Product and Products that will be converted from JSON payload and back
  • APIGatewayProxyRequestEvent and all its inner classes because we declared this event type as request event in our Lambda functions like GetProductByIdHandler and CreateProductHandler
  • org.joda.time.DateTime which will be used to convert timestamp as from String and back which is a part of the APIGateway proxy request and response events

In order to avoid errors with Loggers during the initialization described in the article Class Initialization in Native Image we need to add GraalVM Native Image build arguments in the native-image.properties.

curl -s "https://get.sdkman.io" | bash  

source "/home/ec2-user/.sdkman/bin/sdkman-init.sh"
Copy after login
Copy after login
Copy after login

The native-image.properties should be placed in the META-INF/native-image/${MavenGroupIid}/${MavenArtifactId}

As we use slf4j-simple Logger in pom.xml we need to place native-image.properties in the path META-INF/native-image/org.slf4j/slf4j-simple.

Lambda Custom Runtime

In order to deploy the Lambda function as custom runtime we need to package everything into the file with .zip extension which includes the file with the name bootstrap. This file can either be the GraalVM Native Image in our case or contain instructions how to invoke GraalVM Native Image placed in another file. Let's explore it.

Building GraalVM Native Image

We'll build GraalVM Native image automatically in the package phase defined in the pom.xml. The relevant part is defined in the following plugin:

sdk install java 22.0.1-graal  (or use the newest GraalVM version)
Copy after login
Copy after login

We use native-image-maven-plugin from org.graalvm.nativeimage tools and execute native-image in the package phase. This plugin requires the definition of the main class which Lambda function doesn't have. That's why we use Lambda Runtime GraalVM and define its main class com.formkiq.lambda.runtime.graalvm.LambdaRuntime. Lambda Runtime GraalVM is a Java library that makes it easy to convert AWS Lambda written in Java programming language to the GraalVM. We defined it previously in pom.xml as dependency

sudo yum install gcc glibc-devel zlib-devel 
sudo dnf install gcc glibc-devel zlib-devel libstdc++-static
Copy after login
Copy after login

We then give the native image name aws-pure-lambda-java21-graalvm-native-image and include some GraalVM Native Image arguments and previously defined reflect.json.

wget https://mirrors.estointernet.in/apache/maven/maven-3/3.8.5/binaries/apache-maven-3.8.5-bin.tar.gz tar -xvf apache-maven-3.8.5-bin.tar.gz sudo mv apache-maven-3.8.5 /opt/

M2_HOME='/opt/apache-maven-3.8.5' 

PATH="$M2_HOME/bin:$PATH" 

export PATH
Copy after login
Copy after login

In order to zip the built GraalVM Native Image as function.zip required by Lambda Custom Runtime we use the maven-assembly plugin:

Args=--allow-incomplete-classpath \
    --initialize-at-build-time=org.slf4j.simple.SimpleLogger,\
      org.slf4j.LoggerFactory
    -- --trace-class-initialization=org.slf4j.simple.SimpleLogger,\
      org.slf4j.LoggerFactory
Copy after login

The finalName we define as function and id as native-zip We also include native.xml assembly. This assembly defines file format as zip (the complete file name will be ${finalName}-${id}.zip, in our case function-native-zip.zip), adds the previously built GraalVM Native Image with the name aws-pure-lambda-java21-graalvm-native-image and adds the already defined bootstrap which basically invoked the GraalVM Native Image :

<plugin>
      <groupId>org.graalvm.nativeimage</groupId>
      <artifactId>native-image-maven-plugin</artifactId>
      <version>21.2.0</version>
      <executions>
            <execution>
                  <goals>
                        <goal>native-image</goal>
                  </goals>
                  <phase>package</phase>
            </execution>
      </executions>
      <configuration>
            <skip>false</skip>
            <mainClass>com.formkiq.lambda.runtime.graalvm.LambdaRuntime</mainClass>
            <imageName>aws-pure-lambda-java21-graalvm-native-image</imageName>
            <buildArgs>
                  --no-fallback
                  --enable-http
                  -H:ReflectionConfigurationFiles=../src/main/reflect.json
                </buildArgs>
      </configuration>
</plugin>

Copy after login

In the end we have to build GraalVM Native Image packaged as a zip file which can be deployed as Lambda Custom Runtime with :

<dependency>
   <groupId>com.formkiq</groupId>
   <artifactId>lambda-runtime-graalvm</artifactId>
   <version>2.3.1</version>
</dependency>
Copy after login

Deploying GraalVM Native Image as a Lambda Custom Runtime

In the AWS SAM template we the Lambda runtime as provided.al2023, which is the newest version of the custom runtime) and provide the path to the previously built GraalVM Native Image function-native-zip.zip.

  <buildArgs>
    --no-fallback
    --enable-http
    -H:ReflectionConfigurationFiles=../src/main/reflect.json
 </buildArgs>
Copy after login

Now we are ready to deploy our application with

curl -s "https://get.sdkman.io" | bash  

source "/home/ec2-user/.sdkman/bin/sdkman-init.sh"
Copy after login
Copy after login
Copy after login

Conclusion

In this part of the series, we explained how to develop and deploy AWS Lambda function with custom runtime containing GraalVM Native Image. In the next part of the series we'll measure the cold and warm start times of the Lambda function for such scenario for different memory settings of the Lambda function.

The above is the detailed content of Lambda function with GraalVM Native Image - Part ow to develop and deploy Lambda function with custom runtime. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1652
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

See all articles