Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I have a few lambdas working together through SNS. One lambda receives a request and sends data to SNS. Another lambda is subscribed to SNS. It was easy to do in JavaScript as the incoming message just a JS object. Now I am rewriting the lambda to Java. I am looking for the type to use in the handler.

Here's what the lambda looks like. SNSMessage is the placeholder for the type.

public class ArchiveRequestHandler implements RequestHandler<SNSMessage?, Void> {

    @Override public Void handleRequest(SNSMessage? input, Context context) {

        // do something with the message

        return null;

    }

}

This is how an example message looks like:

{

  "Records": [

    {

      "EventVersion": "1.0",

      "EventSubscriptionArn": "arn:aws:sns:EXAMPLE",

      "EventSource": "aws:sns",

      "Sns": {

        "SignatureVersion": "1",

        "Timestamp": "1970-01-01T00:00:00.000Z",

        "Signature": "EXAMPLE",

        "SigningCertUrl": "EXAMPLE",

        "MessageId": "1234567-ee98-5cb9-9903-4c221d41eb5e",

        "Message": "Hello from SNS!",

        "MessageAttributes": {

          "Test": {

            "Type": "String",

            "Value": "TestString"

          },

          "TestBinary": {

            "Type": "Binary",

            "Value": "TestBinary"

          }

        },

        "Type": "Notification",

        "UnsubscribeUrl": "EXAMPLE",

        "TopicArn": "arn:aws:sns:EXAMPLE",

        "Subject": "TestInvoke"

      }

    }

  ]

}

Now I am sure I can create my own type to parse this, but I was hoping there is a more standard way of doing this. However, I haven't found anything in the lambda nor SNS SDK dependencies that looks like this object.

1 Answer

0 votes
by (44.4k points)

This object is part of the aws-lambda-java-events library. Add this dependency:

compile 'com.amazonaws:aws-lambda-java-events:1.3.0'

Now we can do this:

public class ArchiveRequestHandler implements RequestHandler<SNSEvent, Void> {

    @Override public Void handleRequest(SNSEvent input, Context context) {

        // a message

        return null;

    }

}

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...