Trying to post this on AWS forums, but it looks like my account is not ready yet, whatever that means.
I installed the AWS Lambda function (written in Java) that accepts POJOs to enable automatic JSON deserialization. The JSON test that I use is given below and represents a JSON string that will be sent from the main message source after everything is up and running.
{"sender":"Joe", "id":1, "event":"started", "ticks":2, "time":"20150623T214256Z", "version":"1.0"}
This worked great when I tested it by publishing it from the Lambda "test" console.
Now I am trying to connect to SNS by signing the Lambda function on a topic and I am testing it from the SNS console. I tried to send the exact same message as above, both with "raw data" (which did not show any results) and with JSON generated using the "JSON Generator" data parameter, and I ran into a problem when it seems that SNS sends a message to the Lambda function, the POJO is created, but either the default constructor is called, or the constructor with parameters with all zero values โโis called. In any case, when the Lambda function registers a message by calling the overridden toString () method in the POJO, it displays null for all variables without error messages. Similarly, the SNS theme is configured to log into Cloudwatch, and it also does not report any errors. It receives HTTP status 202.
Here is the new JSON post.
{ "default": "{\"sender\":\"Joe\", \"id\":1, \"event\":\"started\", \"ticks\":2, \"time\":\"20150623T214256Z\", \"version\":\"1.0\"}", "lambda": "{\"sender\":\"Joe\", \"id\":1, \"event\":\"started\", \"ticks\":2, \"time\":\"20150623T214256Z\", \"version\":\"1.0\"}", }
The following are log messages.
Lambda magazines:
START RequestId: 238a0546-627d-11e5-b228-817bf2a1219a Received the following :: We have the following Message{sender=null, id=null, event=null, ticks=null, time=null, version=null} END RequestId: 238a0546-627d-11e5-b228-817bf2a1219a REPORT RequestId: 238a0546-627d-11e5-b228-817bf2a1219a Duration: 26.23 ms Billed Duration: 100 ms Memory Size: 1536 MB Max Memory Used: 69 MB
SNS Logs:
{ "status": "SUCCESS", "notification": { "timestamp": "2015-09-24 05:28:51.079", "topicArn": "arn:aws:sns:us-east-1:256842276575:App", "messageId": "3f5c0fa1-8a50-5ce3-b7c9-41dc060212c8", "messageMD5Sum": "65a5cb6d53616bd385f72177fe98ecc2" }, "delivery": { "statusCode": 202, "dwellTimeMs": 92, "attempts": 1, "providerResponse": "{\"lambdaRequestId\":\"238a0546-627d-11e5-b228-817bf2a1219a\"}", "destination": "arn:aws:lambda:us-east-1:256842276575:function:App-Lambda-Trigger" } }
Below is the corresponding lambda function code:
package com.mycompany; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.LambdaLogger; public class LambdaHandler { public void Handler(Message msg, Context context) { LambdaLogger logger = context.getLogger(); logger.log("Received the following :: " + msg.toString()); } }
.
public class Message { private String sender; private Integer id; private String event; private Integer ticks; private String time; private Double version; public Message(String sender, Integer id, String event, Integer ticks, String time, Double version) { this.sender = sender; this.id = id; this.event = event; this.ticks = ticks; this.time = time; this.version = version; } ... getters/setters ... public Message() { } @Override public String toString() { return "We have the following Message{" + "sender=" + getSender() + ", id=" + id + ", event=" + event + ", ticks=" + ticks + ", time=" + time + ", version=" + version + '}'; }
After doing some searching and looking at some javascript examples (I can't find Java examples of functions subscribed to SNS), it seems like they all get an โeventโ. I found the Java SNSEvent class in the AWS Github repository ( https://github.com/aws/aws-lambda-java-libs/blob/master/aws-lambda-java-events/src/main/java/com/amazonaws/ services / lambda / runtime / events / SNSEvent.java ), however this is not in the official Javadoc. None of the AWS documentation I was able to find a document in which the Java Lambda function function is installed to receive deserialized POJOs (which I canโt believe, itโs not so rare), and I can not find anything that indicates which type the object is sent SNS Theme for the lambda function, if this is not possible, I should not expect the type of POJO.
Can someone clarify what type of object should I expect for my lambda function? Can someone provide sample code?
Any help would be appreciated.
EDIT 1
I changed my function to accept SNSEvent and Context objects as a suggestion, and my function throws the following exception:
Error loading method handler on class com.app.LambdaHandler: class java.lang.NoClassDefFoundError java.lang.NoClassDefFoundError: com/amazonaws/services/lambda/runtime/events/SNSEvent at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at java.lang.Class.privateGetPublicMethods(Class.java:2902) at java.lang.Class.getMethods(Class.java:1615) Caused by: java.lang.ClassNotFoundException: com.amazonaws.services.lambda.runtime.events.SNSEvent at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
As if the runtime doesn't recognize SNSEvent?