Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send emails using SQS or SNS

I have a web application that should send emails to admin users when something happens in the application. For instance, a new user is registered.

I would like to avoid having the logic of building/sending the emails inside of the application. I would rather prefer that the application publishes a message in a queue, and then, there is another system listening and reacting properly to send the emails.

The flow would be something like that.

  • The application publishes a message in a queue (SQS or SNS??)
  • A lambda function is trigger. Lambda reads the message and calls SES.
  • SES sends the email

I'm not sure if that is the best way of doing this. I have read that there is a gap between SQS and Lambda. Would it be better with SNS?

What would be the proper flow?

App -> SQS -> Lambda -> SES

or

App -> SNS -> Lambda -> SES

Maybe something else?

Please, take into consideration that the idea is always to abstract the web application from all that logic. The web application would only publish a message somewhere. Then the magic happens in the background.

like image 903
osotorrio Avatar asked Jun 28 '16 16:06

osotorrio


People also ask

Which is better SQS or SNS?

SQS cannot send a message to many systems as it does not fan out the messages. Yes, many pollers can pull messages from it, but if one the consumers deletes the message then other subscribers won't be able to consume the same message again. SNS is preferred over SQS if you want to achieve a fan out pattern.

Can you send an email with SQS?

To send an email, a message is sent to a SQS queue which will act as a trigger to a function running on AWS Lambda. The lambda function in turn will use the AWS SDK to send an API request to SES for emails to be sent to the recipients.

Can SNS be used to send email?

SNS stands for Simple Notification Service and, unsurprisingly, that's exactly what it does. Amazon SNS is about sending basic notifications that can take the form of mobile push notifications, SMS's, and even emails.

Should I use SQS between SNS and lambda?

Having SQS in between SNS and Lambda allows reprocessing older unprocessed events in case Lambda fails to process. SQS allows to put a delay, so that message gets processed after some time, it may be useful in the scenario where data takes time to be available.


Video Answer


1 Answers

Based on your description, I'd propose the following architecture:

App -> SQS -> Lambda -> SES

I'd use SQS to execute the Lambda function from the app or use Cron job worker to run it periodically as a worker on the queue.

This architecture decouples the App from the mailing service, provides asynchronous invocation and queueing. If you need to send larger payloads, use S3 to store these objects and just pass the keys in SQS messages to your Lambda.

like image 106
adamkonrad Avatar answered Oct 20 '22 08:10

adamkonrad