Home > Mobile >  Serverless framework - can't use cron in docker image lambda
Serverless framework - can't use cron in docker image lambda

Time:08-02

I'm building a container to hold multiple functions using the template here https://www.serverless.com/blog/container-support-for-lambda

The issue I'm finding is that I can't find a way to cron to schedule the functions in the container.

This is the Dockerfile

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

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

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

and this is the 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 * * * ? *)

The function works absolutely fine without the 'events' bit but as soon as I insert that, the error on deployment is

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

Any thoughts what's causing this?

CodePudding user response:

You're missing a space between the hypen and schedule in the events block. This causes the array of events to instead be interpreted as an object, which breaks the framework in a non-obvious way.

Try:

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