Skip to content

AWS Lambda Environment fails to find S3EventNotification** class. Works locally #262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
flipch opened this issue Jul 26, 2021 · 4 comments
Closed

Comments

@flipch
Copy link

flipch commented Jul 26, 2021

try {
// Try to load newer version of S3EventNotification from aws-lambda-java-events v3+
Class eventNotificationRecordClass = SerializeUtil.loadCustomerClass(
S3_EVENT_NOTIFICATION_CLASS_V3 + "$S3EventNotificationRecord", classLoader);
jsonObject = serializeEvent(eventNotificationRecordClass, value, S3_EVENT_NOTIFICATION_CLASS_V3);
} catch (Exception ex) {
// Fallback to aws-lambda-java-events pre-v3 (relies on aws-s3-sdk)
Class eventNotificationRecordClass = SerializeUtil.loadCustomerClass(
S3_EVENT_NOTIFICATION_CLASS_V2 + "$S3EventNotificationRecord", classLoader);
jsonObject = serializeEvent(eventNotificationRecordClass, value, S3_EVENT_NOTIFICATION_CLASS_V2);
}

Something seems to be failing here when trying to find the com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification** classes as my lambda clearly has them available. I've exported from the AWS Console and extracted the jar file.

image

Yet I still get a runtime error when attempting to deserialize a string into an S3Event

java.lang.ClassNotFoundException: com.amazonaws.services.s3.event.S3EventNotification$S3EventNotificationRecord: com.amazonaws.services.lambda.runtime.serialization.util.ReflectUtil$ReflectException
com.amazonaws.services.lambda.runtime.serialization.util.ReflectUtil$ReflectException: java.lang.ClassNotFoundException: com.amazonaws.services.s3.event.S3EventNotification$S3EventNotificationRecord
        at com.amazonaws.services.lambda.runtime.serialization.util.ReflectUtil.loadClass(ReflectUtil.java:85)
        at com.amazonaws.services.lambda.runtime.serialization.util.SerializeUtil.loadCustomerClass(SerializeUtil.java:37)
        at com.amazonaws.services.lambda.runtime.serialization.events.serializers.S3EventSerializer.deserializeEvent(S3EventSerializer.java:157)
        at com.amazonaws.services.lambda.runtime.serialization.events.serializers.S3EventSerializer.fromJson(S3EventSerializer.java:93)

Or when instantiating an deserializer for S3EventNotificationRecord

java.lang.ClassNotFoundException: org.joda.time.DateTime: com.amazonaws.services.lambda.runtime.serialization.util.ReflectUtil$ReflectException
com.amazonaws.services.lambda.runtime.serialization.util.ReflectUtil$ReflectException: java.lang.ClassNotFoundException: org.joda.time.DateTime
        at com.amazonaws.services.lambda.runtime.serialization.util.ReflectUtil.loadClass(ReflectUtil.java:85)
        at com.amazonaws.services.lambda.runtime.serialization.util.SerializeUtil.loadCustomerClass(SerializeUtil.java:37)
        at com.amazonaws.services.lambda.runtime.serialization.events.modules.DateTimeModule.<init>(DateTimeModule.java:28)
        at com.amazonaws.services.lambda.runtime.serialization.events.LambdaEventSerializers.serializerFor(LambdaEventSerializers.java:248)
        ```
@flipch flipch changed the title AWS Lambda Environment fails to find class. Works locally AWS Lambda Environment fails to find S3EventNotification** class. Works locally Jul 26, 2021
@flipch
Copy link
Author

flipch commented Jul 26, 2021

The issue seems to revolve around the lambda's environment class loader not functioning properly.

The solution was to change the passed in class loader, i.e from
this.s3EventSerializer = LambdaEventSerializers.serializerFor(S3EventNotification.class, ClassLoader.getSystemClassLoader();
to
this.s3EventSerializer = LambdaEventSerializers.serializerFor(S3EventNotification.class, Thread.currentThread().getContextClassLoader());

@flipch flipch closed this as completed Jul 26, 2021
@kedarundale17
Copy link

@flipch I faced the same issue , the above mentioned solution works like a charm.
Thanks

@marczis
Copy link

marczis commented Nov 16, 2023

Hello, I'm not supposed to see this in 2023 right? Or.... :)

@shinusuresh
Copy link

2024 yey

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants