Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use AWS lambda layer by java (Layer is success in Lambda)...error is NoClassDefFoundError

I built two project and added layers to AWS Lamba successfully.

And my functions use these two layers.

This is my structure of layer

Screen shot

When I execute the function, an error happened:

java.lang.NoClassDefFoundError

I know the location of the layer is inside/opt, but how can I use the layer's library in functions?

like image 482
YouTing Liu Avatar asked Dec 14 '18 09:12

YouTing Liu


2 Answers

Anyone trying to figure out how to copy your dependencies (.jar) into java/lib directory, this is maven snippet from a project -

    <build>
    <plugins>
            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <configuration>
                <source>${maven.compiler.source}</source>
                <target>${maven.compiler.target}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/classes/java/lib</outputDirectory>
                        <includeScope>runtime</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin></plugins></build>

Hope this will come handy to someone

like image 152
Tahniat Ashraf Avatar answered Oct 22 '22 11:10

Tahniat Ashraf


You should locate the files into a folder, in according to the language:

  • Node.js –> nodejs/node_modules or nodejs/node8/node_modules (NODE_PATH)

  • Python – python -> python/lib/python3.7/site-packages (site
    directories)

  • Java –> java/lib (classpath)

  • Ruby –> ruby/gems/2.5.0 (GEM_PATH), ruby/lib (RUBY_LIB)

  • Or default All –> bin (PATH), lib (LD_LIBRARY_PATH)

For more details see: https://docs.aws.amazon.com/en_us/lambda/latest/dg/configuration-layers.html#configuration-layers-path

like image 24
Hermes Flores Avatar answered Oct 22 '22 11:10

Hermes Flores