简体   繁体   中英

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?

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 async hronously 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM