I am trying to create an AWS Lambda Function using micronaut which will be triggered bt s3 events.

However, when I test the function using console, I am receiving the following error :

    Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (byte[])"{"Records":[{"eventVersion":"2.1","eventSource":"aws:s3","awsRegion":"us-east-2","eventTime":"2019-09-03T19:37:27.192Z","eventName":"ObjectCreated:Put","userIdentity":{"principalId":"AWS:AIDAINPONIXQXHT3IKHL2"},"requestParameters":{"sourceIPAddress":"205.255.255.255"},"responseElements":{"x-amz-request-id":"D82B88E5F771F645","x-amz-id-2":"vlR7PnpV2Ce81l0PRw6jlUpck7Jo5ZsQjryTjKlc5aLWGVHPZLj5NeC6qMa0emYBDXOo6QBU0Wo="},"s3":{"s3SchemaVersion":"1.0","configurationId":"828aa6fc-f7b5-4305-8584-487c791"[truncated 295 bytes]; line: 1, column: 2]
at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1904)
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1349)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1415)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:351)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:184)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4674)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3723)
at io.micronaut.jackson.databind.JacksonDatabindMapper.readValue(JacksonDatabindMapper.java:119)
at io.micronaut.json.codec.MapperMediaTypeCodec.decode(MapperMediaTypeCodec.java:189)
... 47 more
Request loop failed with: Error decoding HTTP response body: Error decoding stream for type [class com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification]: Cannot construct instance of `com.amazonaws.services.lambda.runtime.events.models.s3.S3EventNotification` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (byte[])"{"Records":[{"eventVersion":"2.1","eventSource":"aws:s3","awsRegion":"us-east-2","eventTime":"2019-09-03T19:37:27.192Z","eventName":"ObjectCreated:Put","userIdentity":{"principalId":"AWS:AIDAINPONIXQXHT3IKHL2"},"requestParameters":{"sourceIPAddress":"205.255.255.255"},"responseElements":{"x-amz-request-id":"D82B88E5F771F645","x-amz-id-2":"vlR7PnpV2Ce81l0PRw6jlUpck7Jo5ZsQjryTjKlc5aLWGVHPZLj5NeC6qMa0emYBDXOo6QBU0Wo="},"s3":{"s3SchemaVersion":"1.0","configurationId":"828aa6fc-f7b5-4305-8584-487c791"[truncated 295 bytes]; line: 1, column: 2]
END RequestId: 82e659eb-c21e-4ec2-aaea-38fbf45abfb0
REPORT RequestId: 82e659eb-c21e-4ec2-aaea-38fbf45abfb0  Duration: 3058.81 ms    Billed Duration: 3436 ms    Memory Size: 256 MB Max Memory Used: 102 MB Init Duration: 377.01 ms    
RequestId: 82e659eb-c21e-4ec2-aaea-38fbf45abfb0 Error: Runtime exited without providing a reason
Runtime.ExitError

I uploaded the function code as custom runtime by creating a native image using GraalVM as described in below links

https://guides.micronaut.io/latest/mn-serverless-function-aws-lambda-graalvm-maven-java.html

https://guides.micronaut.io/latest/micronaut-aws-lambda-s3-event-gradle-java.html

Sample S3 Event :

{
  "Records": [
    {
      "eventVersion": "2.1",
      "eventSource": "aws:s3",
      "awsRegion": "us-east-2",
      "eventTime": "2019-09-03T19:37:27.192Z",
      "eventName": "ObjectCreated:Put",
      "userIdentity": {
        "principalId": "AWS:AIDAINPONIXQXHT3IKHL2"
      },
      "requestParameters": {
        "sourceIPAddress": "205.255.255.255"
      },
      "responseElements": {
        "x-amz-request-id": "D82B88E5F771F645",
        "x-amz-id-2": "vlR7PnpV2Ce81l0PRw6jlUpck7Jo5ZsQjryTjKlc5aLWGVHPZLj5NeC6qMa0emYBDXOo6QBU0Wo="
      },
      "s3": {
        "s3SchemaVersion": "1.0",
        "configurationId": "828aa6fc-f7b5-4305-8584-487c791949c1",
        "bucket": {
          "name": "DOC-EXAMPLE-BUCKET",
          "ownerIdentity": {
            "principalId": "A3I5XTEXAMAI3E"
          },
          "arn": "arn:aws:s3:::lambda-artifacts-deafc19498e3f2df"
        },
        "object": {
          "key": "b21b84d653bb07b05b1e6b33684dc11b",
          "size": 1305107,
          "eTag": "b21b84d653bb07b05b1e6b33684dc11b",
          "sequencer": "0C0F6F405D6ED209E1"
        }
      }
    }
  ]
}
1

There are 1 best solutions below

0
On

I made a bundle of changes that I saw where different solutions for this issue that were suggested in different blogs:

  1. add <maven.compiler.parameters>true</maven.compiler.parameters> to pom as this affects the fasterxml.jackson
  2. add
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-serialization</artifactId>
<version>1.0.0</version>
</dependency>

and use it as:

final PojoSerializer<S3EventNotification> s3EventSerializer =
        LambdaEventSerializers.serializerFor(S3EventNotification.class, ClassLoader.getSystemClassLoader());

S3EventNotification eventNotification = s3EventSerializer.fromJson(message.body());
  1. In class use import com.amazonaws.services.s3.event.S3EventNotification;