简体   繁体   English

仅在处理相同主题的SQS订阅后才调用AWS Lambda SNS事件

[英]Invoke AWS Lambda SNS event only after SQS subscription on same topic has been processed

I would like to implement an Amazon SNS topic which first delivers messages to a SQS queue that is a subscriber on the topic, and then executes an AWS Lambda function that is also a subscriber on the same topic. 我想实现一个Amazon SNS主题,该主题首先将消息传递到作为该主题的订阅者的SQS队列,然后执行也是同一主题的订阅者的AWS Lambda函数。 The Lambda function can then read messages from the SQS queue and process several of them in parallel (hundreds). 然后,Lambda函数可以从SQS队列中读取消息并并行处理其中的几个消息(数百个)。

My question is whether there is any way to guarantee that messages sent to the SNS topic would first be delivered to the SQS queue, and only then to the Lambda function? 我的问题是,是否有任何方法可以确保发送到SNS主题的消息首先传递到SQS队列,然后才传递给Lambda函数?

The purpose of this is to scale to a large number of messages without having to execute the Lambda function separately for every single message. 这样做的目的是扩展到大量消息,而不必为每个消息单独执行Lambda函数。

For this purpose, triggering the lambda could be better and efficient if used from a cloud watch alert. 为此,如果从云监视警报中使用,则触发lambda会更好,更有效。 With the cloud watch alert set at a buffer limit on the SQS, that could fire the lambda to start and process the full queue. 将云监视警报设置为在SQS上的缓冲区限制时,可能会触发lambda以启动和处理完整队列。

What you're looking for is currently not possible with one SNS Topic . 一个SNS Topic当前无法实现您所需要的。 If you subscribe your Lambda to a SNS Topic that particular Lambda gets executed each time that SNS Topic receives a message, in parallel. 如果您为Lambda订阅SNS Topic ,则每当SNS Topic收到一条消息时,并行执行特定的Lambda

Solution might be to have two SNS Topics and publish messages to the first one and have your SQS subscribe to it. 解决方案可能是有两个SNS Topics然后将消息发布到第一个,然后让您的SQS订阅它。 After successful submission of messages to this first topic you could send a message to the second SNS Topic to execute your Lambda to process messages the first SNS Topic stored to SQS . 成功向第一个主题提交消息后,您可以向第二个SNS Topic发送一条消息,以执行Lambda来处理存储到SQS的第一个SNS Topic消息。

Another possible solution might be the above, you could just send some periodic message to the second topic to run the subscribed Lambda . 另一个可能的解决方案可能是上述方法,您可以只向第二个主题发送一些定期消息以运行订阅的Lambda This would allow you to scale your Lambda SQS Workers . 这将使您能够扩展Lambda SQS Workers

Subscribing both an SQS queue and a Lambda function to an SNS topic is a good way to have your Lambda function process SNS messages with low latency. 在SNS主题中同时订阅SQS队列和Lambda函数是让Lambda函数以低延迟处理SNS消息的好方法。 I've tested this process just now. 我刚才已经测试了这个过程。 With every attempt the lambda function is invoked after the SQS message is inserted. 每次尝试都会在插入SQS消息后调用lambda函数。 I wouldn't expect this to always be the case, but it fixes the latency problem as best I am willing to measure. 我不希望总是这样,但是它解决了延迟问题,这是我愿意衡量的。 It's not guaranteed and you will need a CloudWatch scheduled event to pick up any missed messages. 不能保证,您将需要一个CloudWatch预定事件来接听所有丢失的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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