I tried running the code below locally (as a maven project in IntelliJ) and it runs OK. When I try to run it as a Lambda function, I always get a java.lang.NoClassDefFoundError. Somehow, lambda makes it so the class cannot be found when I include the AmazonS3 line below.
package example;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
public class ParserLambda implements RequestHandler<Object, Object> {
@Override
public Object handleRequest(Object input, Context context) {
String retval = "";
try {
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();
} catch (Exception ex) {
return "{'status':'error', 'retval': '" + ex.getMessage() + "'}";
}
return "{'status':'done', 'retval': '" + retval + "'}";
}
}
If I comment out the following line
//AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();
it gives: "{'status':'done', 'retval': ''}". This is a valid result.
When uncommented
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().build();
I get:
{
"errorMessage": "com/amazonaws/services/s3/AmazonS3ClientBuilder",
"errorType": "java.lang.NoClassDefFoundError",
"stackTrace": [
"example.ParserLambda.handleRequest(ParserLambda.java:11)",
"sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
"sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
"sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
"java.lang.reflect.Method.invoke(Method.java:498)"
],
"cause": {
"errorMessage": "com.amazonaws.services.s3.AmazonS3ClientBuilder",
"errorType": "java.lang.ClassNotFoundException",
"stackTrace": [
"java.net.URLClassLoader.findClass(URLClassLoader.java:381)",
"java.lang.ClassLoader.loadClass(ClassLoader.java:424)",
"java.lang.ClassLoader.loadClass(ClassLoader.java:357)",
"example.ParserLambda.handleRequest(ParserLambda.java:11)",
"sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
"sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
"sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
"java.lang.reflect.Method.invoke(Method.java:498)"
]
}
}
My pom file includes:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.286</version>
</dependency>
I have also tried:
AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(Regions.US_EAST_1).withForceGlobalBucketAccessEnabled(true).build();
but with no success.