# Run an ECS Task When a File is Uploaded to an S3 Bucket using CloudWatch Events

* You can **use CloudWatch Events to run ECS tasks when certain AWS events occur**.<br>
* You can **set up a CloudWatch Events rule that runs an ECS task whenever a file is uploaded to a certain S3 bucket using the S3 PUT operation**.<br>
* You can also **declare a reduced number of ECS tasks whenever a file is deleted on the S3 bucket using the DELETE operation**.<br>
* First, you **must create a CloudWatch Events rule for the S3 service that will watch for object-level operations – PUT and DELETE objects**.<br>
* **For object-level operations, it is required to create a CloudTrail trail first**.<br>
* On the **Targets section, select the “ECS task”** and input the needed values such as the cluster name, task definition and the task count.<br>
* You **need two rules - one for the scale-up and another for the scale-down of the ECS task count**.
