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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With