Pulumi使用Node.js14报错,切换Node.js21后TypeScript编译失败
解决Pulumi SaaS模板中Lambda运行时版本兼容问题
问题背景
使用SaaS模板部署时,首次执行pulumi up遇到AWS Lambda运行时报错:
aws:lambda:Function (post-confirmation-fn): error: 1 error occurred: * error creating Lambda Function (1): InvalidParameterValueException: The runtime parameter of **nodejs14.x** is no longer supported for creating or updating AWS Lambda functions. We recommend you use the new runtime (nodejs20.x) while creating or updating functions. { RespMetadata: { StatusCode: 400, RequestID: "bd01d1cb-9897-456f-89ab-2e75ea03f432" }, Message_: "The runtime parameter of nodejs14.x is no longer supported for creating or updating AWS Lambda functions. We recommend you use the new runtime (nodejs20.x) while creating or updating functions.", Type: "User" }
手动替换为nodejs21.x后,又触发TypeScript类型不兼容错误:
Type Name Plan Info pulumi:pulumi:Stack serverless-aws-react-saas-saasTemplateBackend 1 error Diagnostics: pulumi:pulumi:Stack (serverless-aws-react-saas-saasTemplateBackend): error: Running program 'C:\GitHub\SimonHoiberg-Saas-Template\saas-template-main\back-end/' failed with an unhandled exception: TSError: ⨯ Unable to compile TypeScript: resources/pulumi/resolvers/get-cognito-user/source.ts(12,5): error TS2322: Type "nodejs21.x" is not assignable to type "dotnetcore2.1" | "dotnetcore3.1" | "dotnet6" | "go1.x" | "java8" | "java8.al2" | "java11" | "ruby2.5" | "ruby2.7" | "nodejs10.x" | "nodejs12.x" | "nodejs14.x" | "python2.7" | ... 5 more ... | undefined'.
解决方案
1. 替换为AWS官方支持的稳定运行时
AWS Lambda目前不支持nodejs21.x(非长期支持版本),且Pulumi的AWS TypeScript类型定义中未包含该运行时枚举值。需使用AWS推荐的nodejs20.x:
- 全局替换项目中所有
nodejs14.x字符串为nodejs20.x
2. 匹配本地Node.js版本(可选)
为避免本地开发与Lambda运行时的兼容问题,建议用nvm切换到nodejs20.x:
nvm install 20 nvm use 20
3. 重新执行部署
运行pulumi up即可完成部署,上述两个错误会同时解决。
内容的提问来源于stack exchange,提问作者NealWalters
相关产品推荐
相关产品推荐

