Member-only story
Creating an ECS Scheduled Task Using AWS CDK

ECS Scheduled Tasks
Amazon ECS supports creating scheduled tasks. Scheduled tasks use Amazon EventBridge rules to run tasks.
In this example, I will show you how we can run a simple cron job using the ECS schedule task.
Steps
- Creating a VPC for running the ECS cluster
- Creating ECS cluster
- Creating scheduled task
- Running the project
Creating a VPC for Running the ECS Cluster
In this step, I create a VPC with two public and private subnets. Since resources in the private subnet need to communicate with the outside, we need to add a NAT Gateway or NAT instance into the public subnet.
By default, AWS CDK adds a NAT Gateway in each public subnet. NAT Gateway will cost you some dollars, so don’t forget to delete the stack after testing.
Creating ECS Cluster
Creating Scheduled Task
As shown below, “ScheduledFargateTask” creates a Fargate scheduled task. In this example, this task runs inside a private subnet.
schedule: The schedule or rate (frequency) that determines when CloudWatch Events runs the rule. In this example, I use a normal cron expression that runs the jobs every 10 minutes.