Noah Martin
Noah Martin

Reputation: 1809

How to load native libraries in AWS Lambda?

In my java serverless project I have to call a native library for image processing (libvips). I am using Gradle to create a zip file and sending to the lib folder all the dependencies, including the native libraries:

task buildZip(type: Zip) {
archiveName = "${project.name}.zip"
from compileJava
from processResources
from('.') {
    include 'lib/**'
    include 'bin/**'
}
into('lib') {
    from configurations.runtime
}
}

In the generated zip file, in the lib folder all the libraries are there (jars/native/etc).

After deploying the function through serverless deploy I am not able to load the libvips.so library using Native.loadLibrary("/var/task/lib/libvips.so", Object.class). Apparently in /var/task/lib/ are located only java dependencies and not the native libraries.

Is there another path where AWS stores native libraries?

EDIT

Exception being thrown:

ava.lang.UnsatisfiedLinkError: Unable to load library '/var/task/lib/libvips.so': Native library (var/task/lib/libvips.so) not found in resource path ([file:/var/task/, file:/var/task/lib/aopalliance-repackaged-2.5.0-b42.jar, file:/var/task/lib/asm-all-repackaged-2.5.0-b42.jar, file:/var/task/lib/aws-java-sdk-core-1.11.336.jar, file:/var/task/lib/aws-java-sdk-kms-1.11.336.jar, file:/var/task/lib/aws-java-sdk-s3-1.11.336.jar, file:/var/task/lib/aws-lambda-java-core-1.1.0.jar,....

Upvotes: 3

Views: 5460

Answers (2)

Victor Gubin
Victor Gubin

Reputation: 2937

You need to specify java.library.path JVM property.

By modifying JVM command line options

 JAVA_OPTS = $JAVA_OPS -Djava.library.path= /var/task/lib/ 
 java $JAVA_OPTS ...

Or modify it directly in your code

System.setProperty("java.library.path", "/var/task/lib/");
System.loadLibrary("libvips.so");

Also, you can use JNA library. JNA provides functionality to auto-unpack and load native librarians from the JAR archive (resources) added to the JVM class path. It's includes selecting correct operating system and CPU architecture version binaries.

Upvotes: 2

Mausam Sharma
Mausam Sharma

Reputation: 892

Not all native libraries are present in lambda environment, you have to make a custom deployment package using either a docker or Ec2.

here is how you will do that : https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example-deployment-pkg.html#with-s3-example-deployment-pkg-java

Upvotes: 0

Related Questions