AWS Lambda 和 API 网关 - 闲置;需要 "wake up"/第一次请求没有响应?

AWS Lambda and API Gateway - goes idle; needs to "wake up"/no response on first request?

我一直在修改 AWS Lambda 中的 nodejs 代码,由一些 API 网关端点调用。我注意到在一段时间后没有任何 API 网关调用,下一个 API 网关请求将超时。我将收到标准的 Lambda 错误消息,说明函数超时。但是,触发我的 Lambda 的后续 HTTP 请求工作正常。

从表面上看,似乎有些东西正在进入 "idle" 模式,需要在 API Gateway-Lambda 请求正常工作之前充电。我考虑过设置一个 wget cron 来保持非空闲状态,但是否有真正的解决方法,我如何才能更好地了解正在发生的事情?

Lambda 使工作人员保持活跃一段时间,并会(正如您所注意到的那样)在一段时间不活动后删除该工作人员。以下是 our forums 上发布的一组建议的副本:

A few suggestions:

  1. Keep your Lambda function "warm". If it's invoked infrequently you will incur an overhead "cold start" cost as Lambda needs to allocate resources to serve your request. See this post for more details.
  2. Invoke your Lambda function with resource-based permissions as opposed to role-based. This is to avoid the overhead of API Gateway needing to make an assumeRole() request to STS. Resource-based invocation is default if you set it up in the console.
  3. If appropriate, consider turning on caching for your API.
  4. Is your API doing any transformations of the request or response via mapping templates? This will obviously incur overhead linear with the complexity of the transformation.

请注意,假设 none 其他选项对您有用,#1 实际上只应用作 最后手段