Schedule an ECS Task with EventBridge
Run a periodic containerised job on a schedule with EventBridge rules and verify retries and logging.
Task
Wire a scheduled ECS task via EventBridge rules, including retries and CloudWatch logging.
When to use this
A nightly batch job currently runs on a hand-managed server and needs to move to a scheduled ECS task.
Prerequisites
- AWS account with ECS and EventBridge
- A task definition
- IAM roles for events and execution
Steps
1. Create a scheduled rule targeting the ECS task.
aws events put-rule --name nightly-job --schedule-expression 'cron(0 2 * * ? *)'
aws events put-targets --rule nightly-job \
--targets "Id"="1","Arn"="arn:aws:ecs:region:acct:cluster/main","RoleArn"="arn:aws:iam::acct:role/events-ecs","EcsParameters"={"TaskDefinitionArn"="arn:aws:ecs:region:acct:task-def/job:3","TaskCount"=1}Expected output: The rule triggers the ECS task at 02:00 daily using the events role.
2. Confirm the run and inspect logs.
aws logs tail /ecs/nightly-job --since 1h --format shortExpected output: Log output from the task appears, confirming the schedule fired.
Troubleshooting
Problem
Task never starts
Solution
Check the events role trust policy and that the task role can be assumed by events.
Problem
Duplicate runs
Solution
EventBridge may retry on delivery issues; make the task idempotent.
Limitations
- Not a workflow engine; complex dependencies need Step Functions.
- Schedule timezone is UTC.
- Task definitions must be versioned explicitly.
What this recipe does
Wire a scheduled ECS task via EventBridge rules, including retries and CloudWatch logging.
A nightly batch job currently runs on a hand-managed server and needs to move to a scheduled ECS task.
Prerequisites
- AWS account with ECS and EventBridge
- A task definition
- IAM roles for events and execution
Procedure
- Create a scheduled rule targeting the ECS task.
aws events put-rule --name nightly-job --schedule-expression 'cron(0 2 * * ? *)'
aws events put-targets --rule nightly-job \
--targets "Id"="1","Arn"="arn:aws:ecs:region:acct:cluster/main","RoleArn"="arn:aws:iam::acct:role/events-ecs","EcsParameters"={"TaskDefinitionArn"="arn:aws:ecs:region:acct:task-def/job:3","TaskCount"=1}
Expected result: The rule triggers the ECS task at 02:00 daily using the events role.
- Confirm the run and inspect logs.
aws logs tail /ecs/nightly-job --since 1h --format short
Expected result: Log output from the task appears, confirming the schedule fired.
How to confirm it worked
Run the commands above and confirm every expected result in order. If a step does not produce the stated output, use the troubleshooting notes before continuing. A passing run shows the task runs at the scheduled time, produces logs, and a rerun of the same job has no adverse effect.
Troubleshooting
Task never starts — Check the events role trust policy and that the task role can be assumed by events.
Duplicate runs — EventBridge may retry on delivery issues; make the task idempotent.
Limitations
- Not a workflow engine; complex dependencies need Step Functions.
- Schedule timezone is UTC.
- Task definitions must be versioned explicitly.