I know that I can trigger a task when a file is uploaded (per https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/CloudWatch-Events-tutorial-ECS.html) however, how can I trigger a task when a specific file is uploaded?
Amazon seems not to have anticipated people having multiple jobs watching the same bucket for different files :(
You can accomplish this with CloudWatch Events from CloudTrail Data Events.
Head over to CloudTrail, and create a Trail for your account.
Next, create a CloudWatch Event rule that targets your ECS Task when the CloudTrail Data Event happens.
Head over to CloudWatch and Create a new Event rule.
{
"source": [
"aws.s3"
],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail": {
"eventSource": [
"s3.amazonaws.com"
],
"eventName": [
"PutObject"
],
"requestParameters": {
"bucketName": [
"your-bucket-name" // this is the bucket where your events are happening
],
"key": [
"your-object-key" // this is the object key you want to trigger starting your ECS task, note that it's an array.
]
}
}
}
bucketName
and key
above as appropriate for your use. Configure details
, give the rule a name and set the State to Enabled
, and click Create rule
.Now that your rule is enabled, when you upload an object with the specified key to the specified bucket, CloudWatch Events will trigger the ECS Task you specified.
Looks like you have a wildcard in your comment. To add to the event pattern from hephalump, you can indicate a prefix for the key as well, which will match to any key with that prefix, not just a specific key:
{
"source": [
"aws.s3"
],
"detail-type": [
"AWS API Call via CloudTrail"
],
"detail": {
"eventSource": [
"s3.amazonaws.com"
],
"eventName": [
"PutObject"
],
"requestParameters": {
"bucketName": [
"your-bucket-name" // this is the bucket where your events are happening
],
"key": [
{"prefix": "path/to/key"}
]
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With