Task Scheduling / CRON Jobs on NestJS + Serverless / Lambda

2.6k views Asked by At

We are looking to deploy a NestJS application (using Express) to AWS Lambda (or similar) using the Serverless framework (or similar).

How should we handle scheduled tasks / cron jobs?

I notice in the NestJS documentation that NestJS has an official package that integrates node-cron.

Could you use this package in a serverless environment? If not, what is more appropriate?

1

There are 1 answers

2
Jesse Carter On

If you're running the app in a serverless environment traditional CRON isn't going to be a good approach. You should instead look into using something like AWS Cloudwatch to trigger recurring events on a timer through a webhook/API endpoint. This could trigger a Lambda which sends a payload to your API with some secure headers set so that you can verify that the request should actually be executed

Alternatively, you can set your serverless lambda to be triggered by external events besides just HTTP as well, for example having Cloudwatch push a message onto EventBridge which in turn could safely trigger an execution of your lambda with some payload. It would be up to you to configure an additional entry point for this in your NestJS app though