【问题标题】:Convert IEventBus to EventBus for Rule.addTarget() - Sending Event from EventBus to Event Bus in Same Account为 Rule.addTarget() 将 IEventBus 转换为 EventBus - 将 EventBus 中的事件发送到同一帐户中的事件总线
【发布时间】:2021-10-03 10:58:23
【问题描述】:

我正在尝试设置规则以使用 CDK 将事件从一个事件总线发送到同一帐户中的另一个。

我遇到的问题是 Rule() 只接受实现 IRuleTarget 的东西。

创建新的事件总线会返回实现 IRuleTarget 的 EventBus 类。 但是查找 EventBus 会返回没有实现 IRuleTarget 的 IEventBus。

有没有办法可以查找我的事件总线并将其作为目标应用到规则?

// Define EventBus1
const eventBus1 = new EventBus(this, 'eventBus1', {
  eventBusName: 'eventBus1Name',
});

// Look up my Event Bus in the other account, this method returns an IEventBus
const iEventBus2 = EventBus.fromEventBusArn(this, 'eventBus2', 'eventBus2Arn');

// My bad attempt to convert it, this didn't seem to work
const eventBus2 = EventBus.bind(iEventBus2);

// Create Rule to send event from eventBus1 to eventBus2
const eventBus1ToEventBus2Rule = new Rule(this, 'eventBus1ToEventBus2Rule', {
  eventBus: eventBus1,
  eventPattern: {
    'detailType': ['eventDetailType'],
  },
});

// Fails with in error I'll copy in below
eventBus1ToEventBus2Rule.addTarget(eventBus2);

错误:解决错误:提供的属性不正确 “CfnRuleProps”目标:元素 0:提供的属性不正确 对于“目标属性” arn:必需但缺少。

这是使用 1.115.0 版本的 aws-cdk。 一切都是通过 EventBridge 构建的。

【问题讨论】:

    标签: typescript amazon-web-services aws-cdk aws-event-bridge


    【解决方案1】:

    fromEventBusArn 属于 EventBus 类。 EventBus 扩展(继承)BaseEventBus,它实现了IEventBus,因此可以在任何需要IEventBus 的地方使用。所以,我认为你可以简单地将fromEventBusArn 的返回值转换为EventBus

    而不是这个:

    const iEventBus2 = EventBus.fromEventBusArn(this, 'eventBus2', 'eventBus2Arn');
    const eventBus2 = EventBus.bind(iEventBus2);
    

    这样做:

    const eventBus = EventBus.fromEventBusArn(this, 'eventBus2', 'eventBus2Arn') as EventBus;
    

    但是,生成的变量 eventBus 不能像你正在做的那样传递给 eventBus1ToEventBus2Rule.addTarget(),因为它失败并出现以下错误,我不确定它最初是如何工作的......

    Argument of type 'EventBus' is not assignable to parameter of type 'IRuleTarget'.
      Property 'bind' is missing in type 'EventBus' but required in type 'IRuleTarget'.
    

    【讨论】:

    • 我猜调用 .bind() 以某种方式欺骗了它。我觉得必须有一种简单的方法来使用规则来定位事件总线。它显然可以作为控制台中的一个选项使用。
    • 我幸运地获得了创建新 EventBus(eventBus2) 的规则,其中这个 EventBus 来自 @aws-cdk/aws-events-targets docs.aws.amazon.com/cdk/api/latest/docs/…
    猜你喜欢
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多