Attach Policy to EventBus using CDK and send cross-account events to Eventbus
Asked Answered
S

3

5

What I am trying to do is send an event from a different AWS account to my account which contains the eventbus.

For that I am trying to attach a role/policy to EventBus but I am not able to. I tried to use grantPutEvents but no luck there too. How to do this? (add/attach a Policy)

Also if I attach policy with Principal as account ID of the other AWS account and resource as the ARN of the EventBus, Will this allow me to send events ? Or do I need to do something more?

Supportable answered 12/8, 2020 at 0:13 Comment(0)
S
9

I was able to add a "Resource-based policy" entry by using the base CfnEventBusPolicy class and referencing the corresponding bus by its name:

const defaultBus = event.EventBus.fromEventBusName(this, 'default-bus', 'default');
new event.CfnEventBusPolicy(this, 'xaccount-policy', {
        statementId: 'AllowXAccountPushEvents',
        action: 'events:PutEvents',
        eventBusName: defaultBus.eventBusName,
        principal: 'account-id-goes-here',
});
Screw answered 30/3, 2022 at 13:56 Comment(1)
I tested exactly the same thing. It works for me with the L1 construct (this reply) but not with defaultBus.grantPutEventsTo. Weird but thanks 👍🏽Wavelet
F
1

grantPutEventsTo will not work in this scenario, but you can use addToResourcePolicy - no need to use the Cfn constructs:

bus.addToResourcePolicy(
  new iam.PolicyStatement({
    sid: "AllowXAccountPutEvents",
    actions: ["events:PutEvents"],
    resources: [bus.eventBusArn],
    principals: [new iam.AccountPrincipal("xxxxxxxxxxxx")],
  }),
);

This will create an AWS::Events::EventBusPolicy resource in the CloudFormation.

Forsook answered 20/5, 2024 at 1:40 Comment(1)
Excellent, thank youWeisshorn
B
-1

You need:

  • sender account: an EventBridge rule for the sender event bus. rule's target is the event bus in the receiver account
  • receiver account: update receiver event bus resource-based policy, to allow sender account to put events

this link https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-cross-account.html should help you.

Biphenyl answered 11/9, 2021 at 11:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.