1
votes

I am trying to follow the AWS Lambda Java example: http://docs.aws.amazon.com/lambda/latest/dg/with-s3-example-deployment-pkg.html#with-s3-example-deployment-pkg-java

Created the new class LambdaFunctionHandler to stick in the S3 image modifying lambda code. Added aws-lambda-java-core 1.0.0, aws-lambda-java-events 1.0.0, maven-shade-plugin 2.4.3, and aws-java-sdk-core 1.11.0 to maven dependencies. Created jar, and got an error after uploading and and testing the lambda.

The following error appears:

START RequestId: 9b800497-52c2-11e6-a2c2-416f188f3ed0 Version: $LATEST
Error loading class LambdaFunctionHandler: com/amazonaws/services/s3/AmazonS3: class java.lang.NoClassDefFoundError
java.lang.NoClassDefFoundError: com/amazonaws/services/s3/AmazonS3
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.services.s3.AmazonS3
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

I used those version numbers because other answers seemed to reference those specific versions.

Just to make sure I wasn't just using maven incorrectly, I used the AWS plugin for Eclipse to create an AWS Lambda project, and still resulted in the same error.

2

2 Answers

1
votes

You need to add de S3 dependency

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-s3</artifactId>
    <version>1.11.0</version>
</dependency>

Or you could just put

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-s3</artifactId>
    <version>1.11.0</version>
</dependency>

This one has libraries for all services.

0
votes

Make sure you have all the dependencies in your POM and also, for AWS lambda you need a fat jar which includes these dependencies. If you think you have all thedependencies in your classpath then see if including below shade plugin helps you (use the fat jar created by maven-shade-plugin):

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <finalName>${project.artifactId}</finalName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>