Scheduled tasks are common in backend workflows — for example, cleaning up expired sessions, sending reminders, or syncing data. In AWS, you can set up serverless cron jobs using CloudWatch Events (or EventBridge) to invoke Lambda functions on a schedule. Here’s how to do it.

Step 1: Create a Lambda Function

Go to the AWS Lambda Console and create a new function with your preferred runtime (e.g., Python 3.11 or Node.js 18.x). Here’s a basic example in Node.js:

exports.handler = async (event) => {
  console.log("Scheduled Lambda triggered at", new Date().toISOString());
  // Add your logic here
  return {
    statusCode: 200,
    body: JSON.stringify("Task complete"),
  };
};

Step 2: Add a CloudWatch Event Rule

1. Go to the Amazon EventBridge (or CloudWatch Events) console.

2. Choose Create Rule.

3. Under “Rule type,” choose Event Source → Schedule.

4. Use a cron expression like this for every 5 minutes:

cron(0/5 * * * ? *)

5. In “Target,” choose your Lambda function.

Step 3: Add Permissions to Lambda

When you attach the rule, AWS will automatically create a permission that allows EventBridge to invoke your Lambda function. You can double-check this in the Lambda’s “Permissions” tab.

Step 4: Monitor and Debug

Use CloudWatch Logs to see each run and confirm it’s executing as scheduled. You can also manually trigger the Lambda to test your logic.

Use Cases for Scheduled Lambdas

  • Nightly data backups
  • Email digests or reminders
  • Cleaning up expired tokens/sessions
  • Periodic API polling

Conclusion

Setting up cron jobs in AWS is simple and serverless when you use EventBridge and Lambda together. This approach eliminates the need for managing cron daemons or EC2 instances and scales automatically.

If you found this helpful and want to support more content like this, you can do so here: buymeacoffee.com/hexshift