Tutorial: Create a scheduled Amazon Batch job
You can use Amazon EventBridge Scheduler to submit an Amazon Batch job on a recurring schedule, such as every hour, or once at a specific date and time. EventBridge Scheduler invokes the Amazon Batch SubmitJob API operation on the schedule that you define, using an IAM execution role that grants EventBridge Scheduler permission to submit jobs on your behalf.
This procedure works for all Amazon Batch jobs on Amazon ECS, Amazon EKS, and Amazon Fargate. You can also use it to schedule service jobs, such as SageMaker Training jobs, by targeting the SubmitServiceJob API operation instead.
Note
Scheduled rules are a legacy Amazon EventBridge feature. If you have existing scheduled rules that submit Amazon Batch jobs, they will continue to work. For new schedules, we recommend Amazon EventBridge Scheduler.
Important
The resources that you create in this tutorial might result in charges to your Amazon
account. A recurring schedule continues to submit jobs, and each submitted job uses compute
resources, until you delete the schedule. When you finish, delete the resources that you
created to stop incurring charges. For more information, see the pricing pages for Amazon EventBridge
Prerequisites
Before you begin, you need the following:
-
A job queue and a valid job definition. To create these, see Getting started with Amazon Batch tutorials. To schedule a service job, you need a service environment and a SageMaker job queue instead. To create these, see Getting started with Amazon Batch on SageMaker AI.
-
An IAM execution role that EventBridge Scheduler assumes to call
SubmitJobon your behalf. The role must include a policy that grants thebatch:SubmitJobpermission. To schedule a service job, grant thebatch:SubmitServiceJobpermission instead. As a security best practice, scope the policy to the job queue and job definition that the schedule uses:{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "batch:SubmitJob", "Resource": [ "arn:aws:batch:us-east-1:111122223333:job-queue/my-job-queue", "arn:aws:batch:us-east-1:111122223333:job-definition/my-job-definition:*" ] } ] }The role must also have a trust policy that allows the
scheduler.amazonaws.comservice principal to assume it. For more information, see Set up an execution role in the Amazon EventBridge Scheduler User Guide.
Step 1: Define the schedule
Open the Amazon EventBridge console at https://console.amazonaws.cn/events/
. -
From the navigation bar, select the Amazon Web Services Region to use. Choose the same Region as the job queue that you want to submit jobs to.
-
In the navigation pane, under Scheduler, choose Schedules.
-
Choose Create schedule.
-
For Schedule name, enter a name for your schedule. The name can contain up to 64 characters, and can include letters, numbers, hyphens (-), underscores (_), and periods (.). The name must be unique within the schedule group in the selected Region.
-
(Optional) For Description, enter a description for the schedule.
-
For Schedule group, keep default, or choose a schedule group that you have created.
-
For Occurrence, choose one of the following:
-
Recurring schedule – Runs the job repeatedly. For Schedule type, choose Cron-based schedule and enter a cron expression to run the job at a specific time, such as 8:00 a.m. on the first Monday of every month, or choose Rate-based schedule and enter a rate expression to run the job at a regular rate, such as every 10 minutes. For more information, see Schedule types on Amazon EventBridge Scheduler in the Amazon EventBridge Scheduler User Guide.
-
One-time schedule – Runs the job once at the date and time that you specify.
-
-
For Flexible time window, choose Off to run the job exactly on schedule, or choose a time window to allow EventBridge Scheduler to invoke the job within that window after the scheduled time.
-
Choose Next.
Step 2: Select the target
-
On the Select target page, choose All APIs, and then search for Batch.
-
Choose Amazon Batch, and then choose the API operation that matches your job type:
-
SubmitJob – For Amazon Batch jobs on Amazon ECS, Amazon EKS, and Amazon Fargate.
-
SubmitServiceJob – For service jobs, such as SageMaker Training jobs.
-
-
For Input, provide the request parameters that EventBridge Scheduler passes to the API operation as a JSON object.
Important
Parameter names in the input JSON must be in PascalCase (for example,
JobQueue), not the camelCase shown in the request syntax of the Amazon Batch API Reference (for example,jobQueue). -
Choose Next.
Step 3: Configure schedule settings
-
(Optional) On the Settings page, configure the following:
-
Schedule state – Turn the schedule off if you don't want it to start running as soon as it's created.
-
Action after schedule completion – Choose whether EventBridge Scheduler deletes the schedule after its last invocation.
-
Retry policy – Configure the maximum age of an unprocessed event and the number of times EventBridge Scheduler retries the invocation if it fails.
-
Dead-letter queue (DLQ) – Choose an Amazon SQS queue to receive invocations that can't be delivered to the target.
-
-
For Permissions, choose Use existing role, and then select the execution role that you created as a prerequisite.
-
Choose Next.
Step 4: Review and create
-
Review the schedule details. To make changes to any section, choose Edit next to that section. When you're finished, choose Create schedule.
After the schedule runs, you can verify that the job was submitted on the Amazon Batch console Jobs page.
Step 5: Clean up resources
When you no longer need the schedule, delete it to stop submitting jobs and to avoid incurring charges.
-
In the EventBridge console navigation pane, under Scheduler, choose Schedules.
-
Select the schedule that you created, choose Delete, and then confirm the deletion.
-
(Optional) If you no longer need the execution role that you created as a prerequisite, delete it. For more information, see Delete an IAM role in the IAM User Guide.
Note
Deleting the schedule doesn't affect jobs that were already submitted. To stop a job that is already submitted or running, use the TerminateJob API operation. To stop a service job, use the TerminateServiceJob API operation.
For more information about creating schedules, see Getting started with Amazon EventBridge Scheduler in the Amazon EventBridge Scheduler User Guide.