# SNS - possible to send multiple messages in one call?

**URL:** <https://forum.serverless.com/t/sns-possible-to-send-multiple-messages-in-one-call/2747>\
**Category:** Serverless Framework\
**Created:** [September 22, 2017, 7:15pm UTC](https://forum.serverless.com/t/sns-possible-to-send-multiple-messages-in-one-call/2747 "2017-09-22T19:15:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![walshe](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/walshe/32/1032_2.png) [@walshe](https://forum.serverless.com/u/walshe)\
**Post date:** [September 22, 2017, 7:15pm UTC](https://forum.serverless.com/t/sns-possible-to-send-multiple-messages-in-one-call/2747/1 "2017-09-22T19:15:48Z")

</div>

I have a lambda that recieves dynamodb events.

For each event I want to to trigger another lambda to do some processing on that event.

I had thought that calling this other lambda asynchronously via an InvocationType.Event was maybe too many separate operations - as a separate call is needed for each lambda invocation , so I thought maybe sending multiple messages at once to an SNS queue (which would trigger the lambda) would be a better idea. However it seems that one can only send one message at a time to an SNS topic ? is it not possible to post multiple in onbe call ?

---

<div class="post-metadata">

**Author:** ![rowanu](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/rowanu/32/75_2.png) [@rowanu](https://forum.serverless.com/u/rowanu)\
**Post date:** [September 24, 2017, 8:34pm UTC](https://forum.serverless.com/t/sns-possible-to-send-multiple-messages-in-one-call/2747/2 "2017-09-24T20:34:10Z")

</div>

The [SNS API’s publish API](http://docs.aws.amazon.com/sns/latest/api/API_Publish.html) only allows one topic/message per call.

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [September 25, 2017, 3:08am UTC](https://forum.serverless.com/t/sns-possible-to-send-multiple-messages-in-one-call/2747/3 "2017-09-25T03:08:04Z")

</div>

In situations where I have a Lambda processing events from DynamoDB that need to trigger multiple other Lambda I find the best solution is to publish a generic message to SNS then use the fan out functionality by configuring multiple Lambda as subscribers (just add the same topic as a trigger to each Lambda).

For example: When a new user is added to DynamoDB you could publish a “created” message to the “user” topic then subscribe two Lambda to the user topic (one to send a welcome message and one to add the user to a mailing list)

---

<div class="post-metadata">

**Author:** ![walshe](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/walshe/32/1032_2.png) [@walshe](https://forum.serverless.com/u/walshe)\
**Post date:** [September 25, 2017, 3:35pm UTC](https://forum.serverless.com/t/sns-possible-to-send-multiple-messages-in-one-call/2747/4 "2017-09-25T15:35:58Z")

</div>

I assume though if only one Lambda needs to be called then a direct call to Lambda is preferable over SNS scenario

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [September 26, 2017, 1:49am UTC](https://forum.serverless.com/t/sns-possible-to-send-multiple-messages-in-one-call/2747/5 "2017-09-26T01:49:32Z")

</div>

Not really. SNS provides loose coupling that a direct call doesn’t.

If you only need to call one Lambda then I’d question why you don’t just put that functionality into the Lambda processing the stream.

---

<div class="post-metadata">

**Author:** ![walshe](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/walshe/32/1032_2.png) [@walshe](https://forum.serverless.com/u/walshe)\
**Post date:** [September 26, 2017, 1:38pm UTC](https://forum.serverless.com/t/sns-possible-to-send-multiple-messages-in-one-call/2747/6 "2017-09-26T13:38:56Z")

</div>

well, the thing is the Lambda that processes the stream can receive a bunch of events at once… so doing a bunch of work there (3 rest calls which I want to do together in parallel) would be too much for that Lambda itself

---

<div class="post-metadata">

**Author:** ![slikk66](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/slikk66/32/1114_2.png) [@slikk66](https://forum.serverless.com/u/slikk66)\
**Post date:** [October 8, 2017, 12:52am UTC](https://forum.serverless.com/t/sns-possible-to-send-multiple-messages-in-one-call/2747/7 "2017-10-08T00:52:13Z")

</div>

You would subscribe multiple items to the topic. Topic called once, many subscripted items get the message.

---

<div class="post-metadata">

**Author:** ![walshe](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/walshe/32/1032_2.png) [@walshe](https://forum.serverless.com/u/walshe)\
**Post date:** [October 9, 2017, 2:22am UTC](https://forum.serverless.com/t/sns-possible-to-send-multiple-messages-in-one-call/2747/8 "2017-10-09T02:22:52Z")

</div>

so you are in favor of SNS over a lambda call ?

---

<div class="post-metadata">

**Author:** ![slikk66](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/slikk66/32/1114_2.png) [@slikk66](https://forum.serverless.com/u/slikk66)\
**Post date:** [October 9, 2017, 2:44am UTC](https://forum.serverless.com/t/sns-possible-to-send-multiple-messages-in-one-call/2747/9 "2017-10-09T02:44:12Z")

</div>

I mean either can work, but it appears that because of the timeout issue and/or multiple requests doing a direct call won’t work. So, options are to trigger other multiple lambdas via sns by having them subscribe to the sns, or something else like sqs queues or even dynamo again by storing data in a a table to process. From the little info I know about your use case, having a lambda call an sns to invoke multiple other lambdas asynchronously sounds like a good approach. If you did sqs or a dynamo table, you’d just have another task running on a schedule to process results from the first task running on a schedule. So, you’re not gaining anything. That is, when you read the stream data from dynamo and realize you need to act on it, that should be the point where you do the action (i.e. call the sns to fan out to othwr lambdas).

---

<div class="post-metadata">

**Author:** ![walshe](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/walshe/32/1032_2.png) [@walshe](https://forum.serverless.com/u/walshe)\
**Post date:** [October 9, 2017, 3:19am UTC](https://forum.serverless.com/t/sns-possible-to-send-multiple-messages-in-one-call/2747/10 "2017-10-09T03:19:28Z")

</div>

theres actually no need for fan out right now in my use case - there is just one job to be done for each dynamodb event - so there would only be one subscriber lambda. Sure its more future proof to have sns, but just wondered which was more economical (in terms of api call) - calling a lambda directly and asynchronously or publishing to SNS

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [October 9, 2017, 7:44am UTC](https://forum.serverless.com/t/sns-possible-to-send-multiple-messages-in-one-call/2747/11 "2017-10-09T07:44:52Z")

</div>

My general rule of thumb is that if you have one Lambda directly invoking another then you’re doing it wrong. There are obviously some use cases for this but generally don’t do it. Use SNS because it decouples the processes and adds retries. You get a similar result with Kinesis streams and DynamoDB streams. Don’t use SQS because it’s not currently supported as an event source for Lambda.

When setting up topics I try to use names like “user created” instead of commands like “send welcome email”.

---

<div class="post-metadata">

**Author:** ![walshe](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/walshe/32/1032_2.png) [@walshe](https://forum.serverless.com/u/walshe)\
**Post date:** [October 9, 2017, 12:21pm UTC](https://forum.serverless.com/t/sns-possible-to-send-multiple-messages-in-one-call/2747/12 "2017-10-09T12:21:09Z")

</div>

good point,

btw do you ever have to add any de-dupe logic for SNS messages when there is only one subscriber ?

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [October 9, 2017, 10:42pm UTC](https://forum.serverless.com/t/sns-possible-to-send-multiple-messages-in-one-call/2747/13 "2017-10-09T22:42:38Z")

</div>

Your Lambda’s should be idempotent anyway. If they are then de-duplication of messages from SNS doesn’t matter.
