Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[aws-events-targets] add a target for a cross-account eventbus #9473

Assignees
Labels
@aws-cdk/aws-events-targets effort/small Small work item – less than a day of effort feature-request A feature should be added or improved. good first issue Related to contributions. See CONTRIBUTING.md p2

Comments

@jacques-
Copy link

jacques- commented Aug 5, 2020

I would like a "CrossAccountEventBus" target in aws_events_targets that allows me to forward events to the default eventbus of a different account.

I did see https://docs.aws.amazon.com/cdk/api/latest/docs/aws-events-readme.html#cross-account-targets - but this seems to make changes in the target account which doesn't work in my case.

Use Case

I have a target account where the default eventbus has already been configured with a policy accept forwarded events, I would like to create a rule that targets this cross-account eventbus.

Proposed Solution

A target in aws_events_targets (like https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-events-targets.SnsTopic.html) but that only has an ARN property.

Other

I'm opening this feature request as was recommended to another commenter here: #2850

I'm currently working around this by using the CfnRule, but this is a bit of a bummer as I can use the very handy .on_xxx methods.

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

@jacques- jacques- added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Aug 5, 2020
@SomayaB SomayaB changed the title [aws_events_targets] add a target for a cross-account eventbus [aws-events-targets] add a target for a cross-account eventbus Aug 6, 2020
@rix0rrr
Copy link
Contributor

rix0rrr commented Aug 10, 2020

Yep, this would probably be a good idea. Thanks for the request.

It's unlikely we will take this on quickly but you should be able to add it yourself in a PR if you want.

@rix0rrr rix0rrr added effort/small Small work item – less than a day of effort good first issue Related to contributions. See CONTRIBUTING.md p2 and removed needs-triage This issue or PR still needs to be triaged. labels Aug 10, 2020
@SomayaB SomayaB assigned shivlaks and unassigned rix0rrr Aug 20, 2020
@alanraison
Copy link
Contributor

I would like to have a go at this, if that's ok? I have a similar requirement.

@gabrielbastien
Copy link

Please add this feature! It's important!

@hlascelles
Copy link

hlascelles commented Jan 20, 2021

I have implemented this as follows. It works well for me...

import { RuleTargetConfig } from "@aws-cdk/aws-events";
import { IRule } from "@aws-cdk/aws-events/lib/rule-ref";
import { Effect, PolicyStatement, Role, ServicePrincipal } from "@aws-cdk/aws-iam";
import { ServicePrincipals } from "cdk-constants";

const targetAccountId = "123456789009";
const targetAccountDefaultBus = `arn:aws:events:eu-west-1:${targetAccountId}:event-bus/default`;
const publishingRole = new Role(this, "PublishingRole", {
  assumedBy: new ServicePrincipal(ServicePrincipals.EVENTS)
});
publishingRole.addToPolicy(
  new PolicyStatement({
    effect: Effect.ALLOW,
    resources: [targetAccountDefaultBus],
    actions: [
      "events:PutEvents"
    ]
  })
);

// This rule captures authentication events and sends it to the 
// default EventBridge bus in the other account
const rule = new Rule(this, "EventRule", {
  description: "Captures events",
  ruleName: "EventRule",
  enabled: true,
  eventPattern: {
    source: ["aws.someservice"]
  }
});

rule.addTarget({
  bind(_rule: IRule, generatedTargetId: string): RuleTargetConfig {
    return {
      arn: targetAccountDefaultBus,
      id: generatedTargetId,
      role: publishingRole
    };
  }
});

Don't forget to also log into the target account and set the default bus to permit submissions from the origin account (or Organisation as a whole).

Have a go, see if it works out for you. Be good to get a formal version though too...

@alanraison
Copy link
Contributor

Sorry I'm having a lot of trouble getting the project to build 🙁

@NGL321 NGL321 assigned rix0rrr and unassigned shivlaks Jan 25, 2021
@strannik19
Copy link

thanks @hlascelles for the inspiration. dirty python code that works for me:

@jsii.implements(events.IRuleTarget)
class EventBridgeEventTarget:
    def __init__(
        self,
        id: str,
        target_account_id: str,
        role: iam.Role,
        target_region: str = "eu-west-1",
        target_topic: str = "default",
    ) -> None:
        self.id = id
        self.role = role
        self.target_account_id = target_account_id
        self.target_region = target_region
        self.target_topic = target_topic

    def bind(self, rule, id=None):
        return events.RuleTargetConfig(
            arn=f"arn:aws:events:{self.target_region}:{self.target_account_id}:event-bus/{self.target_topic}",
            role=self.role,
            id=self.id,
        )

@sblackstone
Copy link
Contributor

sblackstone commented Feb 5, 2021

@alanraison see if this helps, its basically the same thing that @hlascelles posted but a bit more stand-alone (they're referencing a few things that are specific to they're code base)

const core = require('@aws-cdk/core');
const lambda_event_sources = require("@aws-cdk/aws-lambda-event-sources");
const events = require('@aws-cdk/aws-events');
const targets = require("@aws-cdk/aws-events-targets");
const cfn = require('@aws-cdk/aws-cloudformation');

const TARGET_ACCOUNT_ID="123456678";
const TARGET_ACCOUNT_REGION='us-east-1';

const publishingRole = new iam.Role(this, "PublishingRole", {
  assumedBy: new iam.ServicePrincipal('events.amazonaws.com'),
});

const targetArn = `arn:aws:events:${TARGET_ACCOUNT_REGION}:${TARGET_ACCOUNT_ID}:event-bus/default`;

publishingRole.addToPolicy(
  new iam.PolicyStatement({
    effect: iam.Effect.ALLOW,
    resources: [targetArn],
    actions: [
      "events:PutEvents"
    ]
  })
);

const rule = new events.Rule(this, "forwardingRule", {
  description: "Forwards Events to Another Bus",
  ruleName: "forwardingRule",
  eventBus: this.eventBus,
  enabled: true,
  eventPattern: {
    source: ["your-namespace"] // <--- update this
  }
});

rule.addTarget({
  bind: (rule, id)  => ({
    arn: targetArn,
    id: id,
    role: publishingRole
  })
});

@shaleenmundra
Copy link

shaleenmundra commented Feb 5, 2021

I used the below to send event to a different account's default bus. I was setting up the event on step function status change to SUCCEEDED

import {CfnRule} from "@aws-cdk/aws-events";
import TargetProperty = CfnRule.TargetProperty;

const targetProperty: TargetProperty = {
            id: `CrossAccountTarget`,
            arn: `arn:aws:events:<region>:<targetAccountId>:event-bus/default`,
        }
const cfnRuleCrossAccount = new CfnRule(this, `CrossAccountRule`, {
            description: "Cross Account rule to send event to different AWS Account",
            state: "ENABLED",
            targets: [targetProperty],
            eventPattern: {
                "source": [
                    "aws.states"
                ],
                "detail-type": [
                    "Step Functions Execution Status Change"
                ],
                "detail": {
                    "status": [
                        "SUCCEEDED"
                    ]
                }
            }
        });

You just have to create a targetProperty and give the arn of target event bus.
Then create a CfnRule and supply it the targetProperty as targets prop

@mergify mergify bot closed this as completed in #12926 Feb 26, 2021
mergify bot pushed a commit that referenced this issue Feb 26, 2021
Closes #9473 
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

This was referenced Mar 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment