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

fix(events): imported EventBus does not correctly register source account #13481

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/@aws-cdk/aws-events/lib/event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,11 @@ class ImportedEventBus extends EventBusBase {
public readonly eventBusPolicy: string;
public readonly eventSourceName?: string;
constructor(scope: Construct, id: string, attrs: EventBusAttributes) {
super(scope, id);
const arnParts = Stack.of(scope).parseArn(attrs.eventBusArn);
super(scope, id, {
account: arnParts.account,
region: arnParts.region,
});

this.eventBusArn = attrs.eventBusArn;
this.eventBusName = attrs.eventBusName;
Expand Down
61 changes: 60 additions & 1 deletion packages/@aws-cdk/aws-events/test/test.event-bus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, haveResource } from '@aws-cdk/assert';
import * as iam from '@aws-cdk/aws-iam';
import { Aws, CfnResource, Stack } from '@aws-cdk/core';
import { Aws, CfnResource, Stack, Arn } from '@aws-cdk/core';
import { Test } from 'nodeunit';
import { EventBus } from '../lib';

Expand Down Expand Up @@ -55,6 +55,65 @@ export = {
test.done();
},

'imported event bus'(test: Test) {
const stack = new Stack();

const eventBus = new EventBus(stack, 'Bus');

const importEB = EventBus.fromEventBusArn(stack, 'ImportBus', eventBus.eventBusArn);

// WHEN
new CfnResource(stack, 'Res', {
type: 'Test::Resource',
properties: {
EventBusArn1: eventBus.eventBusArn,
EventBusArn2: importEB.eventBusArn,
},
});

expect(stack).to(haveResource('Test::Resource', {
EventBusArn1: { 'Fn::GetAtt': ['BusEA82B648', 'Arn'] },
EventBusArn2: { 'Fn::GetAtt': ['BusEA82B648', 'Arn'] },
}));

test.done();
},

'same account imported event bus has right resource env'(test: Test) {
const stack = new Stack();

const eventBus = new EventBus(stack, 'Bus');

const importEB = EventBus.fromEventBusArn(stack, 'ImportBus', eventBus.eventBusArn);

// WHEN
test.deepEqual(stack.resolve(importEB.env.account), { 'Fn::Select': [4, { 'Fn::Split': [':', { 'Fn::GetAtt': ['BusEA82B648', 'Arn'] }] }] });
test.deepEqual(stack.resolve(importEB.env.region), { 'Fn::Select': [3, { 'Fn::Split': [':', { 'Fn::GetAtt': ['BusEA82B648', 'Arn'] }] }] });

test.done();
},

'cross account imported event bus has right resource env'(test: Test) {
const stack = new Stack();

const arnParts = {
resource: 'bus',
service: 'events',
account: 'myAccount',
region: 'us-west-1',
};

const arn = Arn.format(arnParts, stack);

const importEB = EventBus.fromEventBusArn(stack, 'ImportBus', arn);

// WHEN
test.deepEqual(importEB.env.account, arnParts.account);
test.deepEqual(importEB.env.region, arnParts.region);

test.done();
},

'can get bus name'(test: Test) {
// GIVEN
const stack = new Stack();
Expand Down