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

使用Serverless配置现有Python3.7 Lambda的S3事件时遇CloudFormation错误

问题与解决方案:Serverless配置已有S3触发Python3.7 Lambda时的NodeJS12.x报错

问题描述

用Serverless框架给Python3.7版本的Lambda函数配置外部创建的S3桶的s3:ObjectCreated:*事件时,AWS CloudFormation报错说nodejs12.x运行时已不再受支持。报错指向CustomDashresourceDashexistingDashs3LambdaFunction资源,但目标Lambda明明用的是Python3.7(因为依赖限制暂时没法升级)。就算把事件移除重新部署,问题还是没解决。

用到的Serverless配置片段:

provider:
  name: aws
  runtime: python3.7
 .....
functions:
  antivirus-scan:
    handler: src/scan.lambda_handler
    awsKmsKeyArn: ${opt:kms_arn, ""}
    role: antivirusFunctionRole
    memorySize: 2048
    timeout: 300  # 5mins
    events:
      - s3:
          bucket: ${self:custom.antivirus_files_bucket}
          event: s3:ObjectCreated:*
          existing: true

解决方案

这个问题的根源是:Serverless处理已有S3桶的事件触发时,会自动创建一个自定义Lambda资源(就是报错里的那个),这个资源默认用的nodejs12.x已经被AWS淘汰了。解决办法很简单,在Serverless配置里手动指定自定义资源的运行时就行:

  1. 在provider区块里添加customResources配置,指定一个AWS当前支持的NodeJS版本,比如nodejs18.x:
provider:
  name: aws
  runtime: python3.7
  # 保留你原来的其他配置
  customResources:
    runtime: nodejs18.x
  1. 重新部署服务:
serverless deploy

如果你的Serverless版本比较老,可能不支持这个配置,先把Serverless升级到最新稳定版:

npm install -g serverless

另外,之前移除事件后问题还存在,是因为之前部署时创建的旧自定义Lambda资源还在AWS账户里,用上面的配置重新部署后会自动替换掉旧的运行时,问题就能解决了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 06:54:58