Search code examples
amazon-web-servicesamazon-ecsamazon-cloudwatch

Self destruct AWS ECS tasks after reaching RUNNING state


I have ECS Task set as a target on a CloudWatch Event rule that invokes on the below S3 Event Pattern.

The rule invokes OK on a PUT operation in a S3 bucket, and starts the ECS Task that is set as its target.

The Task reaches RUNNING state... and remains in RUNNING state until it is stopped. I use the CLI to stop the task. Also, this task is not part of a ECS Service, but a stand-alone task intended to do a specific task.

Is there a way to self-destruct the Task after it reaches the RUNNING state and does the intended work? I could wait for 30mins or even a few hours... but ultimately the tasks needs to STOP by itself.

This becomes particularly difficult to manage when there are 1000s of S3 PUT operations that invoke the CloudWatch rule that in-turn starts 1000s of tasks. I am looking for somehow stopping these tasks after they reach the RUNNING state and finish the intended work.

Any suggestions?

Cloud Watch Event Rule that has teh ECS Task set as a target


Solution

  • If you have to really have to stick at what you are doing, then you should invoke another lambda function to stop the task once a certain stage is reach in your application which is running as the docker container. Beware of integration hell though!

    What you are trying to do should be better handled by the AWS Lambda and Batch service. You can specify a docker image, to run and once the operation is done, exit the docker process.

    Refer this: https://medium.com/swlh/aws-batch-to-process-s3-events-388a77d0d9c2