Search code examples
aws-cliamazon-ecs

How to generate cluster scheduled task in AWS CLI?


AWS ECS allows you to create scheduled tasks on clusters. I can easily do this through the UI but want to add some automation steps in my Continuous Deployment configuration. While I am able to find the basic documentation provided by Amazon, I cannot find any documentation on how to do this via the CLI nor via the AWS APIs.

I see that they have StartTask API data listed, but it only states:

Custom schedulers use the StartTask API operation to place tasks on specific container instances within your cluster.

They also list Blox in the documentation, but that doesn't appear to have what I want.

Does anyone have any CLI or API examples (any language) that illustrate the programmatic creation of cron-based Scheduled Tasks in an ECS cluster?

where this is in the AWS Console


Solution

  • ECS Scheduled Tasks are backed by CloudWatch Event rules, so during the deployment you can use CLI to update CloudWatch Event Rule.

    Actual payload of the event is name of the ECS cluster, Task Definition and Task Definition revision. If you try to edit Event via UI you can see that there is an option to define exact revision or to use "latest" option, which is basically not defining revision at all.

    CloudWatch Event Edit

    So solutions are:

    • During the deploy update CloudWatch event rule via CLI
    • Initially create Scheduled Task which uses latest revision of task (omit revision in task name eg. "my-task" instead of "my-task:123") and during the deploy only create new task revision as you would do anyway