I've seen a lot of people using SNS to trigger their lambda function rather than using the API gateway to do it. Any specific reasons to do this?
Personally i think allowing the API gateway to do this is a lot more flexible than using SNS. Any good elaboration as to why do this ? Would i get any performance or cost improvements if i use SNS to trigger the function?
TLDR: The choice boils down to request-response vs. publish-subscribe models.
Request-Response:
If you need to know what the Lambda returns, you have to invoke the Lambda synchronously and wait for the result.
This can be done through API Gateway (from client applications) or by directly invoking the Lambda synchronously (from other Lambdas).
Examples are typical HTTP requests, REST APIs, etc.
Publish-Subscribe:
If you don't care what the return value is, you can invoke the Lambda asynchronously and move on without waiting for the result.
When something is published to an SNS topic, a Lambda that subscribes to that topic will then get triggered.
The publisher doesn't care what the subscribers will do.
Examples are usually background task triggers like sending emails, sending SMS, or starting long-running tasks like video transcoding, image processing, web scraping, etc.
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