DEV Community

loading...

How to schedule serverless lambda execution with Github Actions

virtualkirill profile image Kirill Vasiltsov ・1 min read

My Workflow

This workflow allows to schedule execution of serverless lambda functions. Some services like Vercel make deployment of lambdas very easy, but do not give you an option to execute them on schedule. I needed it in my own app (link below) to regularly cleanup a database of items that users save.

The trick is the cron jobs. For example, by adding this to your action:

on:
  schedule:
    - cron: "0 */1 * * *"

you can schedule your workflow to be run every 1 hour.

The cool thing is that you can manage authentication and all kinds of environment variables INSIDE your lambda and turn in into a simple API for the workflow to call: no credentials and arguments needed.

curl is accessible out-of-the-box so you can just run the command using the run field of the job.

Submission Category:

  • Maintainer Must-Haves

Yaml File or Link to Code

This is a relatively simple workflow so I'm going to just share YAML code here:

name: lambda-scheduler

on:
  schedule:
    - cron: "0 */1 * * *"

jobs:
  request:
    name: Request
    runs-on: ubuntu-latest
    steps:
      - name: Cleanup
        run: curl https://yourcoolwebsite.com/api/cleanup

Additional Resources / Info

The workflow is used in this repository. This is a Pocket-like app that allows you to save links for further reading without keeping them as open tabs. Any link older than 24 hours is automatically removed.

Discussion

pic
Editor guide
Collapse
thehanimo profile image
Hani

Wow that's nice!
Btw, you might wanna add some sort of secret token so that only authorised cleanup calls can be made! πŸ˜…