I am trying to implement lambda layer using java. I have created a project for lambda layer below are the codes:
Layer code
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven- 4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.i3l.layer</groupId> <artifactId>customlayer</artifactId> <version>1.1.0</version> </project>
DataHandler.java
package customlayer; public class DataHandler { public String getData(String input) { System.out.println("Inside Layer"); return "Hello from Layer "+input; } }
Function code: I have added the layer dependency in pom.xml like:
<dependency>
<groupId>com.i3l.layer</groupId>
<artifactId>customlayer</artifactId>
<version>1.1.0</version>
<scope>provided</scope>
</dependency>
In LambdaFunctionHandler.java
package com.amazonaws.lambda.userlayertest;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import customlayer.DataHandler;
public class LambdaFunctionHandler implements RequestHandler<RequestObject, String> {
@Override
public String handleRequest(RequestObject input, Context context) {
System.out.println("call to layer");
DataHandler dataHandler = new DataHandler();
String data = dataHandler.getData(input.getBody());
return data;
}
}
I tried uploading the jar directly to layer and putting the jar into a zip and then uploading it to layer. In both cases I am getting error:
"errorMessage": "customlayer/DataHandler",
"errorType": "java.lang.NoClassDefFoundError",
Note: I have already added layer with the version number to the lambda function.
It would be great if someone help me figuring out why the java class is not found in layer. I have downloaded the code from layer and I can see the class file is in correct path as per the import.
Here the issue was with the directory structure. The layer dependencies should be in a particular directory and it varies with the runtime environment. So in this case the
customlayer-1.1.0.jar
should be in ajava/lib
and then it will be archived. Let's say the archive name iscustomlayer.zip
then the structure will beThis structure will vary with the runtime environment.
For more details: https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html