AWS Lambda Triggered by SQS increases SQS request count

不问归期 提交于 2021-01-27 10:23:17

问题


I have a AWS Lambda function which is triggered by SQS. This function is triggered approximately 100 times daily, but request count to the SQS queue is approximately 20.000 times daily. I don't understand why the number of requests made to the SQS is too high. My expectation is that the number of requests made to the SQS should be same with the Lambda invocation.

I have only one Lambda function and one SQS queue in my account.

Can be related with polling of SQS queue? I tried to change the polling interval of SQS from the queue configuration but nothing changed. Another possibility is to change polling interval from Lambda function configuration. However, I cannot find any related parameter.

Briefy, I want to reduce number of SQS request, how can i do that while invoking Lmabda function with SQS?


回答1:


When using SQS as an event source for AWS Lambda, AWS Lambda regularly polls the configured SQS queue to fetch new messages. While the official documentation isn't clear really about that, the blog post announcing that feature goes into the details:

When an SQS event source mapping is initially created and enabled, or when messages first appear after a period with no traffic, then the Lambda service will begin polling the SQS queue using five parallel long-polling connections.

According to the AWS documentation, the default duration for a long poll from AWS Lambda to SQS is 20 seconds.

That results in five requests to SQS every 20 seconds for AWS Lambda functions without significant load, which sums up to the ~21600 per day, which is close to the 20000 you're experiencing.

While increasing the long poll duration seems like an easy way to decrease the number of requests, that's not possible, as the 20 seconds AWS Lambda is using by default is already the maximum possible duration for an SQS queue. I'm afraid there is no easy way to decrease the requests to SQS, when using it as event source for AWS Lambda. Instead depending it could be worth evaluating if another event source, like SNS, would fit your use case as well.




回答2:


Here is how we originally implemented when there is no SQS trigger.

Create a SNS trigger with the SQS Cloudwatch Metric

ApproximateNumberOfMessagesVisible > 0

Trigger a Lambda from SNS, Read Messages from SQS and deliver it to whichever the lambda needs the message.

Alternatively, you can use Kinesis to deliver it to Lambda.

SQS --> Cloudwatch (Trigger Lambda) --> Lambda(Reads Messages) --> Kinesis (Set Batch Size) --> Lambda (Handle Actual Message)

You can also use Kinesis directly but there is no delayed delivery.

Hope it helps.



来源:https://stackoverflow.com/questions/52904643/aws-lambda-triggered-by-sqs-increases-sqs-request-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!