Search code examples
typescriptamazon-web-servicesaws-cdkaws-event-bridge

CDK: How to add custom event bus (in another account) to event rule (in another account)?


I have a account A where event rule is present. I have account B where I have custom event bus which should act as target to event rule in account A.

I did refer this but this is in cloudformation.

I am passing another account custom event bus arn as props.

Here is my code, upon running cdk deploy it shows error of stating cannot find resource of undefined length

const eventRule = new events.Rule(this, 'event-rule', {
  ruleName: getResourceName(this, 'event-rule', 'rule-name', props.envName),
  description: 'This rule will be used to capture events',
  eventPattern: {"source": ['source']},
})
    
    
eventRule.addTarget(new eventTarget.EventBus(props.anotherAccountEventBusArn))

Solution

  • The event_targets.EventBus constructor takes an argument of type IEventBus, but you are passing a string.

    To create a construct representing a reference to an actual event bus, use EventBus.fromEventBusArn. Here is an example from the docs:

    rule.addTarget(new targets.EventBus(
      events.EventBus.fromEventBusArn(
        this,
        'External',
        `arn:aws:events:eu-west-1:999999999999:event-bus/test-bus`,
      ),
    ));
    

    Your target Event Bus must have a resource policy that allows the source account to put events into it.