问题
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. The Lambda function can then read messages from the SQS queue and process several of them in parallel (hundreds).
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?
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.
回答1:
What you're looking for is currently not possible with one 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.
Solution might be to have two SNS Topics
and publish messages to the first one and have your SQS
subscribe to it. 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
.
Another possible solution might be the above, you could just send some periodic message to the second topic to run the subscribed Lambda
. This would allow you to scale your Lambda SQS Workers
.
回答2:
For this purpose, triggering the lambda could be better and efficient if used from a cloud watch alert. 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.
回答3:
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. I've tested this process just now. With every attempt the lambda function is invoked after the SQS message is inserted. 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.
来源:https://stackoverflow.com/questions/30459682/invoke-aws-lambda-sns-event-only-after-sqs-subscription-on-same-topic-has-been-p