How do you implement a CRON job with serverless functions?

Answer

Scheduled serverless jobs use Amazon EventBridge Scheduler (formerly CloudWatch Events) to invoke Lambda functions on a schedule. Define a rule with a cron or rate expression: rate(5 minutes), cron(0 12 * * ? *) (noon UTC daily). In Serverless Framework: functions: dailyReport: handler: handler.dailyReport events: - schedule: cron(0 8 * * ? *). Best practices: (1) Idempotency — design CRON jobs to be safely re-runnable (in case of retry or duplicate invocation); (2) Timeout handling — set Lambda timeout slightly less than the schedule interval; (3) Error alerting — configure CloudWatch Alarms on Lambda error metrics; set up DLQ (Dead Letter Queue) for failed async invocations; (4) Distributed lock for critical jobs — use DynamoDB conditional writes or ElastiCache SETNX to prevent duplicate execution in multi-region deployments; (5) Long-running scheduled jobs — if the job exceeds 15 minutes, trigger a Step Functions workflow or kick off an ECS Fargate task from the Lambda.