You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Lambda Node.js 20.x初始化失败:Runtime.Unknown错误求助

Node.js 20.x Lambda升级后云端运行异常排查求助

问题概述

将原本在Node.js 18.x下正常运行的Lambda函数升级至20.x,已解决Node.js 20的破坏性变更,函数可在基于public.ecr.aws/lambda/nodejs:20的本地Docker容器中正常执行。项目采用TypeScript+ESM模块解析,编译生成index.js,通过Shell脚本完成Docker构建、文件提取、打包上传至AWS Lambda的流程,AWS控制台显示代码已正确上传,但云端Lambda出现以下模糊错误:

2025-01-11T21:12:47.481Z    undefined   ERROR   Uncaught Exception  
{
    "errorType": "Error",
    "errorMessage": "[object Object]",
    "stack": [
        "Error: [object Object]",
        "    at Object.intoError (file:///var/runtime/index.mjs:46:16)",
        "    at Object.textErrorLogger [as logError] (file:///var/runtime/index.mjs:684:56)",
        "    at process.<anonymous> (file:///var/runtime/index.mjs:1272:32)",
        "    at process.emit (node:events:519:28)",
        "    at process._fatalException (node:internal/process/execution:188:25)",
        "    at asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:129:5)"
    ]
}

同时伴随提示:Phase: init Status: error Error Type: Runtime.Unknown

环境配置

Lambda及Docker环境的package.json配置如下:

{
  "name": "backend",
  "version": "1.0.0",
  "description": "Served by AWS Infrastructure as outlined in [infrastructure](../infrastructure/README.md)",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "build": "tsc",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "Mike Coxon (ZGF)",
  "license": "ISC",
  "devDependencies": {
    "@types/jest": "^29.5.14",
    "jest": "^29.7.0",
    "ts-jest": "^29.2.5",
    "typescript": "^5.7.2"
  },
  "dependencies": {
    "@aws-sdk/client-api-gateway": "^3.699.0",
    "@aws-sdk/client-dynamodb": "^3.699.0",
    "@aws-sdk/client-lambda": "^3.699.0",
    "@aws-sdk/client-s3": "^3.701.0",
    "@aws-sdk/client-ses": "^3.699.0",
    "@aws-sdk/lib-dynamodb": "^3.699.0",
    "@aws-sdk/types": "^3.696.0",
    "@aws-sdk/util-dynamodb": "^3.699.0",
    "@mikeycoxon/aws-lambda-event-http-router": "^0.1.0",
    "@paypal/paypal-js": "^8.1.2",
    "@types/aws-lambda": "^8.10.146",
    "@types/node": "^22.10.1",
    "@types/uuid": "^10.0.0",
    "aws-jwt-verify": "^4.0.1",
    "http-status-codes": "^2.3.0",
    "uuid": "^11.0.3"
  }
}

关键排查发现

  • 编辑1:原Node.js 18.x版本采用CommonJS编译格式,当前升级后改为ESM,这是核心差异点
  • 编辑2:云端Lambda中process.env.<ANYTHING>始终返回undefined(Node.js 18.x无此问题);尝试添加自定义.env文件后,出现初始化超时:INIT_REPORT Init Duration: 3013.19 ms Phase: invoke Status: timeout
  • 编辑3:将Lambda控制台超时时间调整至10秒后,函数仍在3秒左右触发初始化超时

求助内容

  1. 分析云端Lambda运行异常的可能原因
  2. 获取Lambda执行环境更多诊断信息的方法

内容的提问来源于stack exchange,提问作者Michael Coxon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 02:50:12