如何修改AWS EventBridge规则以使用AND而不是OR过滤逻辑?

5
每当在名为“mybucket”的S3存储桶中创建一个以“.csv”结尾的对象,且该对象是在“in”文件夹中创建的时,我希望通过EventBridge触发AWS Lambda函数。 目前,我拥有的EventBridge规则如下:
{
  "detail-type": ["Object Created"],
  "source": ["aws.s3"],
  "detail": {
    "bucket": {
      "name": ["mybucket"]
    },
    "object": {
      "key": [{
        "suffix": ".csv"
      }, {
        "prefix": "in/"
      }]
    }
  }
}

我本来希望这个规则能奏效,但实际上却没有,它的表现就像后缀和前缀条件之间有一个或关系。根据我对AWS文档(https://docs.aws.amazon.com/zh_cn/eventbridge/latest/userguide/eb-event-patterns-content-based-filtering.html#eb-filtering-complex-example)的理解,上述规则应该定义后缀和前缀过滤条件之间的与关系,类似于文档中给出的下面这个示例:

{
  "time": [ { "prefix": "2017-10-02" } ],
  "detail": {
    "state": [ { "anything-but": "initializing" } ],
    "c-count": [ { "numeric": [ ">", 0, "<=", 5 ] } ],
    "d-count": [ { "numeric": [ "<", 10 ] } ],
    "x-limit": [ { "anything-but": [ 100, 200, 300 ] } ]
  }
}

相比之下,使用OR逻辑关系需要额外的$or语法,就像文档中给出的这个例子一样:
{
  "detail": {
    "$or": [
      { "c-count": [ { "numeric": [ ">", 0, "<=", 5 ] } ] },
      { "d-count": [ { "numeric": [ "<", 10 ] } ] },
      { "x-limit": [ { "numeric": [ "=", 3.018e2 ] } ] }
    ]
  }
}

那么,为什么我的规则表现出后缀和前缀条件之间似乎存在 OR 关系?我需要做哪些更改才能使它按照我想要的方式工作呢?

S3对象键以前导斜杠开头,请尝试将in/更改为/in/ - Ankush Jain
@AnkushJain,S3密钥和前缀通常不以斜杠开头。 - jarmod
@jarmod 可能我错了。谢谢确认。 - Ankush Jain
就像@jarmod所说的那样,S3对象密钥不是以正斜杠开头。我实际上可以在记录的事件中看到对象密钥,它们看起来像:“in/ipsumlorem...”。我的问题不是“对象创建”事件的前缀或后缀过滤规则根本不起作用,而是它们在组合时不正确地起作用了。我得到了一个或的行为,而我希望得到一个与的行为。 - jimmyorpheus
@jimmyorpheus,您能否尝试使用[{ "suffix": ".csv" , "prefix": "in/" }]?只需一个数组项而不是两个。 - Ankush Jain
1
@AnkushJain 我正在使用AWS Cloudformation 部署基础设施。我尝试了你的解决方案,但是Cloudformation失败了,并显示以下错误:事件模式无效。原因:匹配表达式中只允许一个键。在[Source: (String)"{"detail-type":["Object Created"],"source":["aws.s3"],"detail":{"bucket":{"name":["mybucket"]},"object":{"key":[{"prefix":"in/","suffix":".csv"}]}}}"; line: 1, column: 151] (Service: AmazonCloudWatchEvents; Status Code: 400; Error Code: InvalidEventPatternException; Request ID: ...; Proxy: null) - jimmyorpheus
2个回答

2

目前这是不可能的

有两种方法可以设置类似于“and运算符”的东西,并且不会抛出语法错误,但它们的工作方式不同:

  1. 两个具有不同过滤器的键(由Peter Bouwdewijn提出)- 后一个过滤器将覆盖前一个,因此它只会按后缀过滤,前缀将被完全忽略:
    "key": [{"prefix": "example/directory/"}],
    "key": [{"suffix": ".png"}]
    
    输入"key": "other/directory/image.png"将是有效的
  2. 在同一数组中提供两个过滤对象-它们将作为OR运算符:
    "key": [{"prefix": "example/directory/"}, {"suffix": ".png"}]
    
    两个输入"key": "other/directory/image.png""key": "example/directory/not_image.txt"都将是有效的
请参阅 AWS EventBridge 文档中的 基于内容过滤数组 页面以获取更多信息。

-1

听起来就像我正在面临的确切问题。我在IBM文档中找到了一些东西:https://www.ibm.com/docs/en/dsm?topic=csqcson-forwarding-objectcreated-notifications-sqs-queue-by-using-amazon-eventbridge

他们在那里说明要重复键。

{
  "source": ["aws.s3"],
  "detail-type": ["Object Created"],
  "detail": {
    "bucket": {
      "name": ["<example-bucket>"]
    },
    "object": {
      "key": [{
        "prefix": "example/directory/"
      }],
      "key": [{
        "suffix": ".png"
      }]
    }
  }
}

这非常反直觉,甚至违背了AWS的文档。我还没有尝试过。


有没有办法在“anything-but”中过滤掉后缀? "SourceIdentifier": [{ "anything-but": { "suffix": "-test" } }] - John Pham

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接