0

I have a lambda fuction which is subscribed to sns topic. The lambda function sends some message to slack channel with the information passed from sns topic for example { fruit = apple}. Value of apple is posted on slack

Now I am experiencing a problem where multiple messages lets say total 3 messages are sent to sns topic with different values of fruit. The information in lambda is overwritten by the value of latest message in sns topic. ( i.e 3rd message) in other words the values of fruit of the 1st message or 2nd message is not posted on slack.

So how do I make sure that multiple lambda are invoked for multiple messages in the sns topic like 1 message per 1 lambda invocation from a single sns topic. Right now it's like for all the messages single lambda is invoked.

Jatin Mehrotra
  • 9,286
  • 4
  • 28
  • 67
  • This looks like a config error or a logic error without knowing any details on the code and the config hard to tell. – NewUser Jul 05 '23 at 04:26
  • I recommend you add a debugging line to `print(event)` and see what is _actually_ passed to the function. If you are still having problems, please Edit your question to show us the contents of the `event` and also show us your code that is reading the contents of the `event`. – John Rotenstein Jul 05 '23 at 04:44

1 Answers1

0

When the AWS Lambda function is invoked by Amazon SNS, it is possible that multiple messages are being passed to the Lambda function.

The Lambda function should loop through the records like this:

def handler(event, context):
  for record in event['Records']:
    # Do something with record

Alternatively, you can force the Lambda function to only receive one message by setting the Batch Size to 1. This can be configured on the Trigger in the Lambda function.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • Hi @John, I got your point but this is normally the case when we are using Event Source Mapping, here this is asynchronous invocation. Second thing, I read this [answer](https://stackoverflow.com/a/33692863/1273882), and [this](https://aws.amazon.com/sns/faqs/#:~:text=Q%3A%20Will%20a%20notification%20contain%20more%20than%20one%20message%3F), both says this array will not have more than one item. Can you please help me understand. – Ankush Jain Jul 05 '23 at 10:30
  • Best thing is to log the contents of `event` to CloudWatch Logs, with something like `print(event)` (will vary depending on your chosen language). Then, check what the logs contain. You can access the logs via the **Monitoring** tab in the Lambda function, and hit "View in CloudWatch Logs". – John Rotenstein Jul 05 '23 at 10:55