Reputation: 2044
I am having a use case where I have to run a lambda function daily between 1am -5am every 5 minutes & then 8pm-11pm every 10 mins.
What should be the cron expression for this ? Or how can we achieve this behavior.
Upvotes: 2
Views: 2705
Reputation: 3480
As mentioned in the answer above, you can create an AWS Events Bridge Rule and set your Lambda function as the target. You can specify a rate or cron schedule expression. For example, the following expression will run your Lambda function after every ten minutes on all weekdays.
schedule = "cron(0/10 * ? * MON-FRI *)"
Note that you will also need to give your role the lambda:InvokeFunction
permission so EventsBridge can trigger your Lambda function.
If you are using Terraform, here's a good tutorial for the Terraform setup for this architecture: https://medium.com/geekculture/terraform-setup-for-scheduled-lambda-functions-f01931040007
Upvotes: 1
Reputation: 815
You can achieve this by using AWS CloudWatch Events or AWS CloudWatch EventBridge.
Note that CloudWatch Events and EventBridge are the same underlying service and API, but EventBridge provides more features.
You may need to define two cron expressions for your use case. Cron expression can be as following,
cron(0/5 1-5 ? * ? *) # 1am-5am every 5 minutes
cron(0/10 20-23 ? * ? *) # 8pm-11pm every 10 minutes
Click on the links below to know more about cron expression. https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents-expressions.html
https://docs.aws.amazon.com/eventbridge/latest/userguide/scheduled-events.html
Upvotes: 3