5

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?

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

Comments

5

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

Comments

0

Ashraf's answer is correct, however, if you have custom classes that you also wanted to be included in the jar file, you have to zip the whole fat jar to /java/lib 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.