简体   繁体   English

使用AWS Lambda函数使用AWS SQS队列

[英]Using AWS Lambda Functions to Consume AWS SQS Queues

I'm using an AWS Lambda function that is triggered from an SNS event trigger to consume from an SQS queue. 我正在使用从SNS事件触发器触发的AWS Lambda函数,以从SQS队列使用。 When the Lambda function executes, it pulls 10 messages from the queue, processes them, pulls another 10, and so on and so forth - up to a certain time limit that's coded into the Lambda function (less than the max of 5 minutes, obviously). Lambda函数执行时,它将从队列中提取10条消息,对其进行处理,再提取10条消息,依此类推-直到编码为Lambda函数的特定时间限制(少于5分钟的最大值) )。

It's my understanding that a Lambda function triggered by an SNS event is one-to-one, is that correct? 据我了解,由SNS事件触发的Lambda函数是一对一的,对吗? In other words, one SNS event won't trigger multiple Lambda functions (up to the maximum concurrent execution limit). 换句话说,一个SNS事件不会触发多个Lambda函数(最大并发执行限制)。 There's no scaling based on load. 没有基于负载的缩放。

Are there any other potential solutions, leveraging Lambda, that would let me consume from SQS as frequently/fast as possible? 还有其他潜在的解决方案,可以利用Lambda使我尽可能频繁/快速地从SQS消费吗? I had considered trying to auto-scale my Lambda functions by leveraging CloudWatch alarms (and SNS event triggers) based on SQS queue size, but it seems like those alarms can fire, at most, every 5 minutes. 我曾考虑过尝试根据SQS队列大小利用CloudWatch警报(和SNS事件触发器)来自动缩放Lambda函数,但看来这些警报最多每5分钟就会触发一次。 I've also considered developing a master Lambda function that can automatically execute (many) slave Lambdas based on querying the queue size. 我还考虑过开发一个主要的Lambda函数,该函数可以基于查询队列大小自动执行(许多)从属Lambda。

I understand that the more optimal design may be to leverage Kinesis instead of SNS. 我知道,更好的设计可能是利用Kinesis而不是SNS。 I may consider incorporating Kinesis in the future, but let's just pretend that Kinesis is not an option at this time. 我可能会考虑在将来合并Kinesis,但让我们假装Kinesis目前不可行。

There is no best way to do this. 没有最好的方法可以做到这一点。 One approach (which you've kind of already mentioned) is to use CloudWatch and schedule a Lambda function to run every minute (that's the minimum schedule time for Lambda). 一种方法(您已经提到过)是使用CloudWatch并安排Lambda函数每分钟运行一次(这是Lambda的最短安排时间)。 This Lambda function will then look for new SQS messages and invoke other Lambda functions to handle new message(s). 然后,此Lambda函数将查找新的SQS消息,并调用其他Lambda函数来处理新消息。 Here is a very good article for that use case: https://cloudonaut.io/integrate-sqs-and-lambda-serverless-architecture-for-asynchronous-workloads/ 这是关于该用例的非常好的文章: https : //cloudonaut.io/integrate-sqs-and-lambda-serverless-architecture-for-asynchronous-workloads/

Personally, I do not recommend triggering your Lambda by SNS for this use case, because SNS doesn't give a full guarantee for delivery and recommend sending the SNS notifications to SQS - which does not solve your problem. 就个人而言,我不建议在这种情况下通过SNS触发您的Lambda,因为SNS不能完全保证交付,并且建议您将SNS通知发送给SQS-这不能解决您的问题。 From the FAQ's: 从常见问题解答中:

[...] If it is critical that all published messages be successfully processed, developers should have notifications delivered to an SQS queue (in addition to notifications over other transports). [...]如果成功处理所有已发布的消息很关键,则开发人员应将通知传递到SQS队列(除了通过其他传输的通知之外)。

Source: https://aws.amazon.com/sns/faqs/ 资料来源: https : //aws.amazon.com/sns/faqs/

For this kind of processing, instead of SQS if you push messages to Kinesis Stream you should be able to flexibly process(In batches of needed size) the messages. 对于这种处理,如果将消息推送到Kinesis Stream而不是SQS,则应该能够灵活地处理(按所需大小批量处理)消息。

Note: If you use SQS, after triggering a Lambda function through SNS (or using a Scheduled Lambda), it can invoke inner Lambda functions to check the queue where multiple concurrent inner Lambdas are spawned. 注意:如果使用SQS,则在通过SNS(或使用计划的Lambda)触发Lambda函数之后,它可以调用内部Lambda函数来检查产生多个并发内部Lambda的队列。 However the problem is that its not practical to process SQS items in batches. 但是,问题在于分批处理SQS项目不切实际。

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

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