Skip to content

Commit 3890c03

Browse files
committed
feat(aws-events-targets): external event bus event target
1 parent 6de792c commit 3890c03

File tree

6 files changed

+257
-0
lines changed

6 files changed

+257
-0
lines changed

packages/@aws-cdk/aws-events-targets/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Currently supported are:
2727
* Put a record to a Kinesis stream
2828
* Log an event into a LogGroup
2929
* Put a record to a Kinesis Data Firehose stream
30+
* Put an event on an EventBridge bus
3031

3132
See the README of the `@aws-cdk/aws-events` library for more information on
3233
EventBridge.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import * as events from '@aws-cdk/aws-events';
2+
import * as iam from '@aws-cdk/aws-iam';
3+
import { singletonEventRole } from './util';
4+
5+
/**
6+
* Configuration properties of an Event Bus event
7+
*/
8+
export interface EventBusProps {
9+
/**
10+
* The target event bus
11+
*/
12+
readonly eventBus: events.IEventBus;
13+
14+
/**
15+
* Role to be used to publish the event
16+
*
17+
* @default a new role is created.
18+
*/
19+
readonly role?: iam.IRole;
20+
}
21+
22+
/**
23+
* Notify an existing Event Bus of an event
24+
*/
25+
export class EventBus implements events.IRuleTarget {
26+
private readonly eventBus: events.IEventBus;
27+
private readonly role?: iam.IRole;
28+
29+
constructor(props: EventBusProps) {
30+
this.eventBus = props.eventBus;
31+
this.role = props.role;
32+
}
33+
34+
bind(rule: events.IRule, id?: string): events.RuleTargetConfig {
35+
if (this.role) {
36+
this.role.addToPrincipalPolicy(this.putEventStatement());
37+
}
38+
const role = this.role ?? singletonEventRole(rule, [this.putEventStatement()]);
39+
return {
40+
id: id ? id : '',
41+
arn: this.eventBus.eventBusArn,
42+
role,
43+
};
44+
}
45+
46+
private putEventStatement() {
47+
return new iam.PolicyStatement({
48+
actions: ['events:PutEvents'],
49+
resources: [this.eventBus.eventBusArn],
50+
});
51+
}
52+
}

packages/@aws-cdk/aws-events-targets/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export * from './aws-api';
77
export * from './lambda';
88
export * from './ecs-task-properties';
99
export * from './ecs-task';
10+
export * from './event-bus';
1011
export * from './state-machine';
1112
export * from './kinesis-stream';
1213
export * from './log-group';
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import '@aws-cdk/assert/jest';
2+
import * as events from '@aws-cdk/aws-events';
3+
import * as iam from '@aws-cdk/aws-iam';
4+
import { Stack } from '@aws-cdk/core';
5+
import * as targets from '../../lib';
6+
7+
test('Use EventBus as an event rule target', () => {
8+
const stack = new Stack();
9+
const rule = new events.Rule(stack, 'Rule', {
10+
schedule: events.Schedule.expression('rate(1 min)'),
11+
});
12+
13+
rule.addTarget(new targets.EventBus({
14+
eventBus: events.EventBus.fromEventBusArn(
15+
stack,
16+
'External',
17+
'arn:aws:events:us-east-1:111111111111:default'
18+
),
19+
}));
20+
21+
expect(stack).toHaveResource('AWS::Events::Rule', {
22+
Targets: [
23+
{
24+
Arn: 'arn:aws:events:us-east-1:111111111111:default',
25+
Id: 'Target0',
26+
RoleArn: {
27+
'Fn::GetAtt': [
28+
'RuleEventsRoleC51A4248',
29+
'Arn',
30+
],
31+
},
32+
},
33+
],
34+
});
35+
expect(stack).toHaveResource('AWS::IAM::Policy', {
36+
PolicyDocument: {
37+
Statement: [{
38+
Effect: 'Allow',
39+
Action: 'events:PutEvents',
40+
Resource: 'arn:aws:events:us-east-1:111111111111:default',
41+
}],
42+
Version: '2012-10-17',
43+
},
44+
Roles: [{
45+
Ref: 'RuleEventsRoleC51A4248',
46+
}],
47+
});
48+
});
49+
50+
test('with supplied role', () => {
51+
const stack = new Stack();
52+
const rule = new events.Rule(stack, 'Rule', {
53+
schedule: events.Schedule.expression('rate(1 min)'),
54+
});
55+
const role = new iam.Role(stack, 'Role', {
56+
assumedBy: new iam.ServicePrincipal('events.amazonaws.com'),
57+
roleName: 'GivenRole',
58+
});
59+
60+
rule.addTarget(new targets.EventBus({
61+
eventBus: events.EventBus.fromEventBusArn(
62+
stack,
63+
'External',
64+
'arn:aws:events:us-east-1:123456789012:default'
65+
),
66+
role,
67+
}));
68+
69+
expect(stack).toHaveResource('AWS::Events::Rule', {
70+
Targets: [{
71+
Arn: 'arn:aws:events:us-east-1:123456789012:default',
72+
Id: 'Target0',
73+
RoleArn: {
74+
'Fn::GetAtt': [
75+
'Role1ABCC5F0',
76+
'Arn',
77+
],
78+
},
79+
}],
80+
});
81+
expect(stack).toHaveResource('AWS::IAM::Policy', {
82+
PolicyDocument: {
83+
Statement: [{
84+
Effect: 'Allow',
85+
Action: 'events:PutEvents',
86+
Resource: 'arn:aws:events:us-east-1:123456789012:default',
87+
}],
88+
Version: '2012-10-17',
89+
},
90+
Roles: [{
91+
Ref: 'Role1ABCC5F0',
92+
}],
93+
});
94+
});
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
"Resources": {
3+
"Rule4C995B7F": {
4+
"Type": "AWS::Events::Rule",
5+
"Properties": {
6+
"ScheduleExpression": "rate(1 minute)",
7+
"State": "ENABLED",
8+
"Targets": [
9+
{
10+
"Arn": {
11+
"Fn::Join": [
12+
"",
13+
[
14+
"arn:aws:events:",
15+
{
16+
"Ref": "AWS::Region"
17+
},
18+
":999999999999:event-bus/test-bus"
19+
]
20+
]
21+
},
22+
"Id": "Target0",
23+
"RoleArn": {
24+
"Fn::GetAtt": [
25+
"RuleEventsRoleC51A4248",
26+
"Arn"
27+
]
28+
}
29+
}
30+
]
31+
}
32+
},
33+
"RuleEventsRoleC51A4248": {
34+
"Type": "AWS::IAM::Role",
35+
"Properties": {
36+
"AssumeRolePolicyDocument": {
37+
"Statement": [
38+
{
39+
"Action": "sts:AssumeRole",
40+
"Effect": "Allow",
41+
"Principal": {
42+
"Service": "events.amazonaws.com"
43+
}
44+
}
45+
],
46+
"Version": "2012-10-17"
47+
}
48+
}
49+
},
50+
"RuleEventsRoleDefaultPolicy0510525D": {
51+
"Type": "AWS::IAM::Policy",
52+
"Properties": {
53+
"PolicyDocument": {
54+
"Statement": [
55+
{
56+
"Action": "events:PutEvents",
57+
"Effect": "Allow",
58+
"Resource": {
59+
"Fn::Join": [
60+
"",
61+
[
62+
"arn:aws:events:",
63+
{
64+
"Ref": "AWS::Region"
65+
},
66+
":999999999999:event-bus/test-bus"
67+
]
68+
]
69+
}
70+
}
71+
],
72+
"Version": "2012-10-17"
73+
},
74+
"PolicyName": "RuleEventsRoleDefaultPolicy0510525D",
75+
"Roles": [
76+
{
77+
"Ref": "RuleEventsRoleC51A4248"
78+
}
79+
]
80+
}
81+
}
82+
}
83+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// !cdk-integ pragma:ignore-assets
2+
import * as events from '@aws-cdk/aws-events';
3+
import * as cdk from '@aws-cdk/core';
4+
import * as targets from '../../lib';
5+
6+
const app = new cdk.App();
7+
8+
class EventSourceStack extends cdk.Stack {
9+
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
10+
super(scope, id, props);
11+
12+
const rule = new events.Rule(this, 'Rule', {
13+
schedule: events.Schedule.expression('rate(1 minute)'),
14+
});
15+
rule.addTarget(new targets.EventBus({
16+
eventBus: events.EventBus.fromEventBusArn(
17+
this,
18+
'External',
19+
`arn:aws:events:${this.region}:999999999999:event-bus/test-bus`
20+
),
21+
}));
22+
}
23+
}
24+
25+
new EventSourceStack(app, 'event-source-stack');
26+
app.synth();

0 commit comments

Comments
 (0)