user1751825
user1751825

Reputation: 4309

AWS ECS. How to ensure only one instance of a task is running?

I'm wanting to setup an ECS task to schedule various other application tasks.

The "tasks" this task will schedule will mostly involve calling restful endpoints in another load balanced service.

I know there are other ways to do this, using cloudwatch to trigger a lambda etc. However this seems overly complex for what I need.

I was planning to just make a very simple, light-weight apline based image with a crontab to do the triggering of the restful calls.

This all seems easy enough. The only concern I have is that I would want to prevent, as far as possible, having multiple instances of this task running, even if only for a short period of time.

If my CI/CD pipeline triggers an update to this cron task, then there may be a short period of time, where the old and new task will be running simultaneously.

There may therefore be a small chance that a cron task could be triggered twice.

What I would like to do, is to have ECS stop the currently running task completely, before attempting to start the new one.

This seems to be contrary to the normal way it wants to work, where it will ensure the new task is up, and healthy before stopping the old one.

Is this possible, and if so, how do I configure it?

It's not a problem if my crons don't run for a period of time, but it could be a problem if any get triggered more than once.

Upvotes: 6

Views: 10208

Answers (3)

Mark Sowul
Mark Sowul

Reputation: 10610

If you are creating an ECS service, there is a maximumPercent parameter, which if you set it to "100%" should prevent it from starting a new task while the old one is still running:

If a service is using the rolling update (ECS) deployment type, the maximumPercent parameter represents an upper limit on the number of your service's tasks that are allowed in the RUNNING or PENDING state during a deployment, as a percentage of the desiredCount (rounded down to the nearest integer). This parameter enables you to define the deployment batch size. For example, if your service is using the REPLICA service scheduler and has a desiredCount of four tasks and a maximumPercent value of 200%, the scheduler may start four new tasks before stopping the four older tasks (provided that the cluster resources required to do this are available). The default maximumPercent value for a service using the REPLICA service scheduler is 200%.

If a service is using either the blue/green (CODE_DEPLOY) or EXTERNAL deployment types and tasks that use the EC2 launch type, the maximum percent value is set to the default value and is used to define the upper limit on the number of the tasks in the service that remain in the RUNNING state while the container instances are in the DRAINING state. If the tasks in the service use the Fargate launch type, the maximum percent value is not used, although it is returned when describing your service.

https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_DeploymentConfiguration.html

Upvotes: 0

shariqmaws
shariqmaws

Reputation: 8890

Instead of using ECS Service (which makes sure a particular number of tasks is always running and deploys via rolling or B/G deploy strategy which is not you desire) - how about using StopTask and RunTask api to control when a task is stopped and started - gives you complete control.

Upvotes: 2

Nick C
Nick C

Reputation: 514

Instead of using scheduled tasks, you could create an ECS service and use scheduled scaling to scale the desired service count to 1 and back down to zero.

Upvotes: 3

Related Questions