AWS Cron Parser
pyawscron is a Python library that parses and interprets AWS EventBridge (formerly CloudWatch Events) cron expressions. It supports all AWS-specific cron syntax, including special wildcards like 'L', 'W', and '#', and can calculate next, previous, or all occurrences within a date range. The current version is 1.0.7, with releases addressing compatibility and parsing nuances.
Warnings
- gotcha AWS cron expressions differ significantly from standard Unix cron. Be aware of unique wildcards like '?', 'L', 'W', '#' and different field order/interpretation (e.g., year field is optional in AWS).
- gotcha You cannot use the '*' wildcard in both the Day-of-month and Day-of-week fields simultaneously. If one is specified with '*', the other must use '?' (question mark).
- gotcha The 'W' wildcard in the Day-of-month field specifies the weekday closest to the given day. For example, '3W' means the weekday closest to the 3rd of the month. Issue 56 (fixed in 1.0.6) addressed cases where 'W' interacted incorrectly with non-existent dates.
- gotcha The `get_all_schedule_bw_dates` method returns a list of all matching datetimes within a range and has no built-in limit. For large or long date ranges, this can consume significant memory.
- gotcha Older versions of `pyawscron` (prior to 1.0.7) had stricter dependency constraints on `python-dateutil`, potentially causing conflicts when used in environments with newer `python-dateutil` versions.
Install
-
pip install pyawscron
Imports
- AWSCron
from pyawscron import AWSCron
Quickstart
import datetime
from pyawscron import AWSCron
def main():
# An AWS cron expression: at 05:00 AM (UTC) on the 4th of every month.
# '?' in day-of-week indicates "no specific day of the week" to avoid conflict
aws_cron = AWSCron("0 5 4 * ? *")
# Get the next 5 schedule occurrences from a specific start time
start_dt = datetime.datetime.utcnow().replace(second=0, microsecond=0, tzinfo=datetime.timezone.utc)
print(f"Next 5 occurrences from {start_dt.isoformat()}:")
for dt in aws_cron.get_next_n_schedule(start_dt, 5):
print(dt)
if __name__ == "__main__":
main()