Skip to content

Commit

Permalink
fix(aws-events) account aware event bus import
Browse files Browse the repository at this point in the history
Parses the event bus arn to give the account and region to the underlying Resource.

fixes #13469
  • Loading branch information
thantos committed Mar 9, 2021
1 parent e09250b commit 15b86e2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
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

0 comments on commit 15b86e2

Please sign in to comment.