Search code examples
amazon-web-servicesterraformamazon-ecsaws-fargate

Run an AWS ECS task


I have an ECS fargate task defined in aws. I would like to run it occasionally as needed.

Is there an easy way to do this?

I have terraform code that defines it as a scheduled task that is disabled. Whenever I want to run it, my procedure is:

  1. Modify the terraform file to enable the task and set the scheduled execution time for five minutes from now.
  2. Deploy the terraform and wait for the task to run.
  3. Undo the terraform changes and redeploy.

This procedure works, but is quite inconvenient. Surely there is a better way to run one-off tasks? I've tried going through the aws web console but it's even worse.


Solution

  • Update:

    You can now run ECS tasks via Terraform with the aws_ecs_task_execution data source.


    Old answer:

    If you want to stick with using the scheduler to run the task, then something like your current process is the only way to achieve that. However it sounds like you don't really want to have the task run on a set schedule at all, instead you only want to run it when needed.

    The most direct way to trigger an ECS task to run, is via the RunTask API, which you can trigger from the AWS CLI (which you could wrap in a shell script), or one of the AWS SDKs.