Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a SNS message at AWS Lambda Function written in C#

I have a lambda function at AWS written in c#. This lambda function would read an incoming SNS message. Below is my lambda code.

public void FunctionHandler(Amazon.Lambda.SNSEvents.SNSEvent.SNSMessage message, ILambdaContext context)
        {                  
            if (message.Message == null)
            {
                Console.WriteLine("message is null");
            }
            else if (message.Message == string.Empty)
            {
                Console.WriteLine("message is empty");
            }
            else
            {
              Console.WriteLine(message.Message);
            }
       }
    }

I have subscribed this lambda function to a SNS Topic. The lambda function is triggered when I publish the SNS message, but the message is always shown null. i.e the output I am getting is :

message is null

Can anyone help me in reading the SNS message?

like image 822
Prayag Sagar Avatar asked Jul 24 '17 13:07

Prayag Sagar


People also ask

Can Lambda read from SNS?

Amazon SNS and AWS Lambda are integrated so you can invoke Lambda functions with Amazon SNS notifications. When a message is published to an SNS topic that has a Lambda function subscribed to it, the Lambda function is invoked with the payload of the published message.

How do I get messages from SNS topic?

Sign in to the Amazon SNS console . In the left navigation pane, choose Topics. On the Topics page, select a topic, and then choose Publish message.

Does AWS Lambda support C?

AWS Lambda natively supports Java, Go, PowerShell, Node. js, C#, Python, and Ruby code, and provides a Runtime API which allows you to use any additional programming languages to author your functions. Please read our documentation on using Node. js, Python, Java, Ruby, C#, Go, and PowerShell.


1 Answers

From Amazon.Lambda.SNSEvents:

public class Function
{
    public string Handler(SNSEvent snsEvent)
    {
        foreach (var record in snsEvent.Records)
        {
            var snsRecord = record.Sns;
            Console.WriteLine($"[{record.EventSource} {snsRecord.Timestamp}] Message = {snsRecord.Message}");
        }
    }
}

So, it seems that SNSEvent contacts an array of Records, that contain Message.

Start by changing your debug to print message instead of message.Message and take it from there.

like image 130
John Rotenstein Avatar answered Nov 03 '22 08:11

John Rotenstein