是否有明确的方法来识别“冷启动”?是在 Lambda 本身的运行时,还是通过日志?我知道冷启动的特点是运行时间更长,我实际上可以看到,但我正在寻找一种明确的方法。
如果这很重要,我正在使用 Node.js。
更新:下面有两个很好的答案,适用于两个用例:
- 在 lambda 运行时识别冷启动。
- 从 CloudWatch 日志中识别冷启动。
最佳答案
如果您在 NodeJS 脚本的顶部添加一些初始化代码,您将能够在代码中指出这是一个冷启动,然后如果您想在日志中看到它,您将能够记录它。例如:
var coldStart = true;
console.log("This line of code exists outside the handler, and only executes on a cold start");
exports.myHandler = function(event, context, callback) {
if (coldStart) {
console.log("First time the handler was called since this function was deployed in this container");
}
coldStart = false;
...
callback(...);
}
更新:
如果您只关心在日志中看到冷启动,Lambda 现在会记录额外的 “初始化持续时间” CloudWatch Logs 中冷启动的值。
关于amazon-web-services - AWS Lambda : Identifying cold starts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47061146/