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

Serverless Framework:Docker镜像Lambda无法配置Cron调度求助

容器化Lambda函数配置Cron调度失败排查

我基于Serverless官方容器模板构建了承载多函数的容器,目前无法为容器内的函数配置Cron调度。无events配置时函数运行正常,添加配置后部署报错,相关代码及错误信息如下:

Dockerfile

FROM public.ecr.aws/lambda/python:3.8

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY handler.py .
CMD [ "handler.hello"]

原始serverless.yml

frameworkVersion: '3'

provider:
  name: aws
  runtime: python3.8
  ecr:
    images:
      appimage:
        path: ./

functions:
  hello:
    image:
      name: appimage
      command:
        - handler.hello
  test_email:
    image:
      name: appimage
      command:
        - handler.test_email
    events:
      -schedule: cron(0,15,30,45 * * * ? *)

部署错误信息

Warning: Invalid configuration encountered at 'functions.test_email.events': must be array

Error:
TypeError: events.some is not a function
    at /usr/local/lib/node_modules/serverless/lib/plugins/aws/package/compile/events/alexa-skill.js:30:20
    at Array.some (<anonymous>)
    at initialize (/usr/local/lib/node_modules/serverless/lib/plugins/aws/package/compile/events/alexa-skill.js:29:60)
    at PluginManager.run (/usr/local/lib/node_modules/serverless/lib/classes/plugin-manager.js:598:65)
    at async Serverless.run (/usr/local/lib/node_modules/serverless/lib/serverless.js:174:5)
    at async /usr/local/lib/node_modules/serverless/scripts/serverless.js:771:9

问题原因及解决方案

错误提示明确指出functions.test_email.events必须是数组,问题出在YAML语法上:-schedule之间没有空格,导致YAML解析时没有将其识别为数组的一项,进而引发后续类型错误。

修正后的test_email函数events配置如下:

functions:
  test_email:
    image:
      name: appimage
      command:
        - handler.test_email
    events:
      - schedule: cron(0,15,30,45 * * * ? *)

同时确认你的Cron表达式符合AWS Lambda格式要求(6个字段:秒 分 时 日 月 周),当前表达式cron(0,15,30,45 * * * ? *)合法,会每15分钟触发一次函数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 17:39:40