Node.js中如何设置AWS EventBridge规则排除带指定前缀的detail.name值
实现方案
不能直接在括号前加!实现反向匹配,AWS EventBridge 有专属的否定匹配语法,正确的事件模式配置如下:
eventPattern: { source: ['sourceSample'], detail: { name: [{ 'anything-but': { prefix: 'notWantedPrefixSample' } }] } }
说明
- 你原有写法的逻辑是匹配所有
detail.name带notWantedPrefixSample前缀的事件,反向过滤需要使用官方提供的anything-but运算符包裹前缀匹配规则 - 该语法是EventBridge标准事件模式规则,所有公开AWS商业化区域均已支持
完整Node.js示例(基于AWS SDK v3)
如果你需要完整的创建规则代码,可以参考以下写法:
import { EventBridgeClient, PutRuleCommand } from "@aws-sdk/client-eventbridge"; const client = new EventBridgeClient({ region: "你的AWS区域" }); const run = async () => { const command = new PutRuleCommand({ Name: "你的规则名称", EventPattern: JSON.stringify({ source: ['sourceSample'], detail: { name: [{ 'anything-but': { prefix: 'notWantedPrefixSample' } }] } }), State: "ENABLED" }); const response = await client.send(command); console.log("规则创建成功,ARN为:", response.RuleArn); }; run();
内容的提问来源于stack exchange,提问作者mafehx
相关产品推荐
相关产品推荐

