英文:
Why invocation of class file in lambda layer throwing error?
问题
以下是您要求的翻译内容:
层代码
- 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;
}
}
函数代码:
我已经在 pom.xml 中添加了层的依赖,如下所示:
<dependency>
<groupId>com.i3l.layer</groupId>
<artifactId>customlayer</artifactId>
<version>1.1.0</version>
<scope>provided</scope>
</dependency>
在 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;
}
}
我尝试过直接将 JAR 文件上传到层,也尝试过将 JAR 文件放入 ZIP 文件中,然后将其上传到层。在这两种情况下,我都遇到了错误:
"errorMessage": "customlayer/DataHandler",
"errorType": "java.lang.NoClassDefFoundError",
注意:我已经在 Lambda 函数中添加了带有版本号的层。
如果有人能帮助我找出为什么在层中找不到 Java 类,那将会很棒。我已经从层中下载了代码,并且我可以看到类文件的路径与导入路径相匹配。
英文:
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.
答案1
得分: 1
这里的问题出在目录结构上。层的依赖关系应该放在特定的目录中,而这会随着运行环境而变化。所以在这种情况下,customlayer-1.1.0.jar
应该放在 java/lib
目录中,然后才能进行归档。假设归档名称是 customlayer.zip
,那么目录结构将如下:
customlayer.zip
└ java/lib/customlayer-1.1.0.jar
这个结构会根据运行环境而变化。
更多详细信息请参考:https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html
英文:
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 a java/lib
and then it will be archived. Let's say the archive name is customlayer.zip
then the structure will be
customlayer.zip
└ java/lib/customlayer-1.1.0.jar
This structure will vary with the runtime environment.
For more details: https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html
答案2
得分: 1
此外,要自动创建它,可以创建一个类似下面的 assembly.xml 文件。
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>bin</id>
<baseDirectory>/</baseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/java/lib</outputDirectory>
<includes>
<include>${project.artifactId}.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
然后在你的 pom.xml 中,添加插件并配置这个 assembly.xml 文件。
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- 用于继承合并的标识 -->
<phase>package</phase> <!-- 添加到打包阶段 -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
英文:
Moreover, to create it automatically, one can create an assembly.xml like below.
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>bin</id>
<baseDirectory>/</baseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory>/java/lib</outputDirectory>
<includes>
<include>${project.artifactId}.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>
Then in your pom.xml, add the plugin and configure this assembly.xml file.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- append to the packaging phase. -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论