0
votes
  • I am publishing a String message as message payload using SNS notification from Raspberry Pi using Python program and I want to pass that message payload to a Lambda function.
  • I have configured the requirement in the SNS console on AWS i.e., I have created a topic and added the lambda function to its subscribers.
  • Now, I want to get that message payload in the lambda function. But I can't find any method that can help me do that. For example, something like getMessage or something similar to that.
  • So my questions are: Since I have configured the publishing and subscription on AWS, can I assume that the clients are connected and if I publish a message I should be getting that at the subscriber's end which is my lambda function here? Also, what's the technique in which I can get the message payload in my lambda function? I am adding the below as per cjwfuller's suggestion.
  • Below I have written down the method for publishing in Python

client_boto = boto3.client('sns', aws_access_key_id='@@@', aws_secret_access_key='@@@', region_name='us-west-2')

REGION = 'us-west-2'

TOPIC = 'arn:aws:sns:us-west-2:***:topic_name'

MSG = ntpath.basename(f_string)

SUBJECT_boto = 'File Name'

pub =client_boto.publish(TopicArn = TOPIC, Message = MSG, Subject=SUBJECT_boto)

I am writing the subscribing code in Java. Since my lambda func is already subscribed to it on AWS console, should my Java program include the subscription again or is there a way to get the msg payload directly.

2

2 Answers

0
votes

Which language are you writing the function in? JavaScript as an example:

exports.myHandler = function(event, context, callback) {
    console.log("value1 = " + event.key1);
    console.log("value2 = " + event.key2);
    // ...
}

Source: http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

It's useful testing the sort of stuff in the AWS Lambda console before writing all the code.

Since I have configured the publishing and subscription on AWS, can I assume that the clients are connected

Clients aren't really "connected", they're simply subscribed to a topic.

publish a message I should be getting that at the subscriber's end which is my lambda function here?

Sounds like you're doing the right sort of thing - posting example code will help us come up with more precise answers.

0
votes

On searching, I have found the class for SNSEvent which is, 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

This Class contains all the methods related to and needed to get the message payload.

The Lambda function handler in Java goes something like this, example;

public void handleRequest(SNSEvent input, Context context){
  String this_takes_message=input.getRecords().get(0).getSNS().getMessage();
}