21

I am trying to create an event rule that is triggered by a change in a file in S3 bucket in different AWS account. Detail description is here

So far the rule works fine with exact file names, but I need to make it work with filename prefixes. In the working example, the file name is an exact string in the non-working example the file name is a wildcard. Does CloudWatch Events Rule JSON pattern supports wildcards?

Working configuration:

{
  "source": ["aws.s3"],
  "account": ["1111111xxxxx"],
  "detail": {
    "eventSource": ["s3.amazonaws.com"],
    "eventName": ["PutObject"],
    "requestParameters": { "bucketName": ["mybucket"], "key": ["myfile-20180301.csv"] }
  }
}

Non-working configuration:

{
  "source": ["aws.s3"],
  "account": ["1111111xxxxx"],
  "detail": {
    "eventSource": ["s3.amazonaws.com"],
    "eventName": ["PutObject"],
    "requestParameters": { "bucketName": ["mybucket"], "key": ["myfile-*"] }
  }
}
k1r0
  • 542
  • 1
  • 5
  • 12

4 Answers4

15

I found a fancy solution for this using Content-based filtering (released in February 2020) like prefix for example.

So in your case, the solution should be:

{
  "source": ["aws.s3"],
  "account": ["1111111xxxxx"],
  "detail": {
    "eventSource": ["s3.amazonaws.com"],
    "eventName": ["PutObject"],
    "requestParameters": {
      "bucketName": ["mybucket"],
      "key": [{ "prefix": "myfile-" }]
    }
  }
}
Jeff
  • 98
  • 4
Marto
  • 214
  • 2
  • 6
0

A workaround will be to have a separate bucket where you PUT/COPY the *.csv files and remove "key" parameter. This way Cloud Watch will be triggered at any *.csv file operation on that bucket. Another this I don't know why you are seeting the key in cloud watch event pattern if the key was already set in cloud trail.

Vlad Cenan
  • 172
  • 3
  • 11
0

If you log events of interest to Cloudwatch via CloudTrail, then you can use a Cloudwatch metric filter with wildcard matching and create a Cloudwatch Event on that filter.

skeller88
  • 4,276
  • 1
  • 32
  • 34
0

The template code gave by Marto was not working for me, however the doc led to a solution:

{
  "source": ["aws.s3"],
  "account": ["1111111xxxxx"],
  "detail": {
    "eventSource": ["s3.amazonaws.com"],
    "eventName": ["PutObject"],
    "requestParameters": {
      "bucketName": ["mybucket"],
      "key": [{"prefix": "myfile-*"}]
    }
  }
}

Hope it helps.

dijkstraman
  • 166
  • 2
  • 11