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.
Previous
What is Lambda@Edge and what are its use cases?
Next
How do you handle database connections in serverless functions?
More Serverless Architecture Questions
View all →- Intermediate How do you reduce cold start latency in serverless functions?
- Intermediate What is AWS Step Functions and when would you use it?
- Intermediate How do you handle state in serverless applications?
- Intermediate What is the Serverless Framework and how does it work?
- Intermediate How do you implement authentication in a serverless API?