Deploy a serverless Cron job

Last updated 4 months ago

Koyeb allows you to execute functions at regular time intervals using cron job. This lets you easily deploy short-run and long-run scheduled jobs with minimal effort. A function can be a Docker image, a Node.js, or a Python function.

When using a cron job on your Koyeb functions, an event is emitted to your Stack event-bus at the interval you defined. Your function is then invoked by this scheduled event.

To deploy a cron job function, you need to define the interval your function is triggered in the events section of the koyeb.yaml file.

To define the period your function will be invoked, you will use Unix-like CRON syntax. If you are not familiar with this syntax, you can look at crontab guru to help you figure out how to set the schedule period that will invoke your function.

Below is a minimal koyeb.yaml configuration example to invoke a function every one minute:

1functions:
2  - name: "hello-world" # The function name
3    image: koyeb/cowsay # The Docker image 
4    events:
5      - schedule: "* * * * *"
6

You can also add user-data to your scheduled event by adding the data attribute. You then receive this data as a JSON payload in your event and can use it in your function.

1  - name: display-koyeb-event
2    image: stedolan/jq
3    args: [".", "/koyeb/events/in/raw"]
4    events:
5      - schedule: "* * * * *"
6        data:
7          key: value
8

Scheduled Python & Node.js function invocation

To deploy a Python or Node.js function at a regular interval, the procedure is similar to a Docker container. You define the period to invoke your functions in the events attribute.

Python example

1functions:
2  - name: hello-python-world
3    runtime: python3.8
4    handler: hello_world.handler
5    events:
6      - schedule: "*/5 * * * *"
7

Node.js example

1functions:
2  - name: hello-python-world
3    runtime: nodejs14
4    handler: hello_world.handler
5    events:
6      - schedule: "*/5 * * * *"
7

To learn more about how to deploy Python and Node.js functions on Koyeb, please refer to this documentation page.


Ready to get started?

Get in touch or create an account and deploy your serverless stack in minutes.

footer-frame