DEV Community

Kirill Vasiltsov
Kirill Vasiltsov

Posted on

How to schedule serverless lambda execution with Github Actions

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 * * *"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

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! πŸ˜