Repository navigation
New serverless pattern - eventbridge-scheduler-cron-migration - #3570
Open
rextheking wants to merge 1 commit into
Open
rextheking wants to merge 1 commit into
rextheking wants to merge 1 commit into
Conversation
Adds eventbridge-scheduler-cron-migration/python/sam: migrating cron on a VM to Amazon EventBridge Scheduler. An import script converts a crontab into one schedule per entry targeting a Lambda dispatcher, each run is recorded in DynamoDB keyed on the scheduled window, a CloudWatch alarm treats a missing completion as breaching, and a catch-up function idempotently replays windows missed during a deployment.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Submission issue: #3571
What this adds
A pattern for migrating cron on a VM to Amazon EventBridge Scheduler, rather than wiring up a single schedule.
An import script reads an existing crontab (or a
schedule.json), converts each entry, and creates one schedule per line targeting a single Lambda dispatcher. The dispatcher records every run in a DynamoDB table keyed on the scheduled window, publishes a completion metric, and a CloudWatch alarm withTreatMissingData: breachingfires when a job does not report completion. A catch-up function replays windows missed while no schedule was running, for example during a deployment, idempotently.Why a new family rather than a variant
The repo asks contributors to add a variant to an existing family when the services match, so here is the reasoning in case you would rather I move it.
There are 65 pattern folders with
scheduleorschedulerin the name, and I went through the closest ones.eventbridge-schedule-to-lambda(10 variants),eventbridge-schedule-to-lambda-dlq/python/sam,eventbridge-schedule-lambda-to-dynamodb/dotnet/cdk,lambda-durable-eventbridge-cronandlambda-durable-scheduled-tasksall wire up one schedule and invoke a target. None of them imports a crontab, keeps run history, alarms on a missing completion, or replays a missed window. Searching the repo forcrontab,catch-upandrun-historyreturns no pattern content at all.The closest service match is
eventbridge-schedule-lambda-to-dynamodb, and I am happy to move this under it aspython/samif you prefer. I kept it separate because the pattern is about the migration, and because the folder name is what someone searching for how to replace cron will actually find. If you would rather it live in the existing family, say so and I will move it in this PR.Scope
Four services (EventBridge Scheduler, Lambda, DynamoDB, CloudWatch) and two Lambda functions. I deliberately kept this smaller than it could have been: one dispatcher with the example jobs as functions inside it rather than one Lambda function per job, and the crontab importer as a local script rather than deployed infrastructure. Happy to trim further if it reads as too much for a pattern.
Verified
Deployed to my own account in
us-east-1, tested, and torn down.cron(0 * * * ? *)schedule fired on its own and invoked the dispatcher 21 seconds into its window, inside the documented 60 second precision.run#<scheduled-time>item and themarker, with the execution ID and schedule ARN substituted by Scheduler.SKIPPED; the conditional write refused the duplicate.ALARMwith "1 missing datapoint was treated as [Breaching]".America/New_York(02:00 New York resolved to 07:00Z).ActionAfterCompletion=DELETEremoved the one-time catch-up schedule after it fired.sam validate --lint,sam build,node _scripts/validate.jsandnode _scripts/check-folder.jsall pass.cd tools && python3 -m unittest test_cron_to_schedulercovers 28 known-answer conversions, 12 refusals and the field-count and?invariants.Three things that look odd but are deliberate
The Scheduler trust policy scopes
aws:SourceArnto the schedule group, not to the schedules. Scoping it toschedule/<group>/*makesCreateSchedulefail with "The execution role you provide must allow AWS EventBridge Scheduler to assume the role". The User Guide's confused deputy page calls this out: the ARN must be a schedule group. I hit this during testing and left a comment in the template so the next person does not.scheduler:ListSchedulesis granted onschedule/*/*whileGetScheduleis scoped to the group.ListSchedulesis authorised againstschedule/*/*even when the call filters byGroupName, so it cannot be scoped to one group. Also found during testing, also commented.The one-time catch-up schedule is created through the API, not in
template.yaml.AWS::Scheduler::Schedulehas noActionAfterCompletionproperty, and the quotas page notes that the schedule count includes completed one-time schedules. Creating it through the API withActionAfterCompletion=DELETElets it clean itself up.Notes
template.yaml.boto3to create schedules, but--dry-runconverts and checks a crontab with no dependencies, which is the part worth running first.