Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use SNS to trigger a lambda function, and not API gateway?

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?

like image 859
astroanu Avatar asked Dec 11 '18 12:12

astroanu


1 Answers

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.

like image 166
Noel Llevares Avatar answered Sep 28 '22 16:09

Noel Llevares