是否可以让 AWS Lambda 函数保持温暖?

Is it possible to keep an AWS Lambda function warm?

我的应用程序中有一些部分无法承受由 "freeze-thaw" 周期引起的额外 1-2 秒延迟,Lambda 函数在新函数或一段时间内未使用时会经历这些周期。

我如何才能让这些 Lambda 函数保持温暖,这样 AWS 就不必一直重新配置它们?这适用于 1) 不常用的功能和 2) 最近部署的功能。

理想情况下,我错过了一个名为 "keep warm" 的设置,它增加了 Lambda 函数的成本,但始终保持函数温暖并准备好响应,但我很确定这不存在.

我想一个选择是使用 CloudWatch 计时器每隔一段时间对函数执行 ping 操作...但这对我来说感觉不对。另外,我不知道AWS用于将Lambda函数置于冰上的时间间隔。

BBC 在 iPlayer engineering 上发表了一篇不错的文章,其中描述了类似的问题。

他们选择每隔几分钟使用 CloudWatch Scheduled Events 调用一次函数。

So in theory, it should just stay there, except it might not. So we have set up a scheduled event to keep the container ‘warm’. We invoke the function every couple of minutes not to do any processing, but to make sure we’ve got the model ready. This accounts for a very small percentage of invocations but helps us mitigate race conditions in the model download.(We also limit artificially how many lambdas we invoke in parallel as an additional measure).

2019 年 12 月更新

AWS 现在还提供 'Provisioned Concurrency'。 https://aws.amazon.com/blogs/aws/new-provisioned-concurrency-for-lambda-functions/

基本上,您要为每个要保留的实例支付大约 10 美元/月(对于 1GB Lambda)'warm'。