为什么在 Lambda 层中调用类文件会引发错误?

huangapple go评论76阅读模式
英文:

Why invocation of class file in lambda layer throwing error?

问题

以下是您要求的翻译内容:

层代码

  1. 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>
  1. 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

  1. pom.xml

     &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
     xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
     xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven- 
     4.0.0.xsd&quot;&gt;
         &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
         &lt;groupId&gt;com.i3l.layer&lt;/groupId&gt;
         &lt;artifactId&gt;customlayer&lt;/artifactId&gt;
         &lt;version&gt;1.1.0&lt;/version&gt;
     &lt;/project&gt;
    
  2. DataHandler.java

    package customlayer;
    
    public class DataHandler {
    
     public String getData(String input) {
        System.out.println(&quot;Inside Layer&quot;);
        return &quot;Hello from Layer &quot;+input;
     }
    }
    

Function code:
I have added the layer dependency in pom.xml like:

&lt;dependency&gt;
	&lt;groupId&gt;com.i3l.layer&lt;/groupId&gt;
	&lt;artifactId&gt;customlayer&lt;/artifactId&gt;
	&lt;version&gt;1.1.0&lt;/version&gt;
	&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;

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&lt;RequestObject, String&gt; {

@Override
public String handleRequest(RequestObject input, Context context) {
    
	System.out.println(&quot;call to layer&quot;);
	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.

&lt;assembly xmlns=&quot;http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
          xsi:schemaLocation=&quot;http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd&quot;&gt;
    &lt;id&gt;bin&lt;/id&gt;
    &lt;baseDirectory&gt;/&lt;/baseDirectory&gt;
    &lt;formats&gt;
        &lt;format&gt;zip&lt;/format&gt;
    &lt;/formats&gt;
    &lt;fileSets&gt;
        &lt;fileSet&gt;
            &lt;directory&gt;${project.build.directory}&lt;/directory&gt;
            &lt;outputDirectory&gt;/java/lib&lt;/outputDirectory&gt;
            &lt;includes&gt;
                &lt;include&gt;${project.artifactId}.jar&lt;/include&gt;
            &lt;/includes&gt;
        &lt;/fileSet&gt;
    &lt;/fileSets&gt;
&lt;/assembly&gt;

Then in your pom.xml, add the plugin and configure this assembly.xml file.

        &lt;plugin&gt;
            &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
            &lt;version&gt;2.6&lt;/version&gt;
            &lt;configuration&gt;
                &lt;descriptors&gt;
                    &lt;descriptor&gt;assembly.xml&lt;/descriptor&gt;
                &lt;/descriptors&gt;
                &lt;appendAssemblyId&gt;false&lt;/appendAssemblyId&gt;
            &lt;/configuration&gt;
            &lt;executions&gt;
                &lt;execution&gt;
                    &lt;id&gt;make-assembly&lt;/id&gt; &lt;!-- this is used for inheritance merges --&gt;
                    &lt;phase&gt;package&lt;/phase&gt; &lt;!-- append to the packaging phase. --&gt;
                    &lt;goals&gt;
                        &lt;goal&gt;single&lt;/goal&gt;
                    &lt;/goals&gt;
                &lt;/execution&gt;
            &lt;/executions&gt;
        &lt;/plugin&gt;

huangapple
  • 本文由 发表于 2020年10月22日 22:03:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64483961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定