Skip to content

Commit ea91aa3

Browse files
authored
feat(aws-events): Event Bus target (#12926)
Closes #9473 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent fc87574 commit ea91aa3

File tree

6 files changed

+249
-0
lines changed

6 files changed

+249
-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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
* Role to be used to publish the event
11+
*
12+
* @default a new role is created.
13+
*/
14+
readonly role?: iam.IRole;
15+
}
16+
17+
/**
18+
* Notify an existing Event Bus of an event
19+
*/
20+
export class EventBus implements events.IRuleTarget {
21+
private readonly role?: iam.IRole;
22+
23+
constructor(private readonly eventBus: events.IEventBus, props: EventBusProps = {}) {
24+
this.role = props.role;
25+
}
26+
27+
bind(rule: events.IRule, id?: string): events.RuleTargetConfig {
28+
if (this.role) {
29+
this.role.addToPrincipalPolicy(this.putEventStatement());
30+
}
31+
const role = this.role ?? singletonEventRole(rule, [this.putEventStatement()]);
32+
return {
33+
id: id ?? '',
34+
arn: this.eventBus.eventBusArn,
35+
role,
36+
};
37+
}
38+
39+
private putEventStatement() {
40+
return new iam.PolicyStatement({
41+
actions: ['events:PutEvents'],
42+
resources: [this.eventBus.eventBusArn],
43+
});
44+
}
45+
}

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: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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(events.EventBus.fromEventBusArn(
14+
stack,
15+
'External',
16+
'arn:aws:events:us-east-1:111111111111:default',
17+
),
18+
));
19+
20+
expect(stack).toHaveResource('AWS::Events::Rule', {
21+
Targets: [
22+
{
23+
Arn: 'arn:aws:events:us-east-1:111111111111:default',
24+
Id: 'Target0',
25+
RoleArn: {
26+
'Fn::GetAtt': [
27+
'RuleEventsRoleC51A4248',
28+
'Arn',
29+
],
30+
},
31+
},
32+
],
33+
});
34+
expect(stack).toHaveResource('AWS::IAM::Policy', {
35+
PolicyDocument: {
36+
Statement: [{
37+
Effect: 'Allow',
38+
Action: 'events:PutEvents',
39+
Resource: 'arn:aws:events:us-east-1:111111111111:default',
40+
}],
41+
Version: '2012-10-17',
42+
},
43+
Roles: [{
44+
Ref: 'RuleEventsRoleC51A4248',
45+
}],
46+
});
47+
});
48+
49+
test('with supplied role', () => {
50+
const stack = new Stack();
51+
const rule = new events.Rule(stack, 'Rule', {
52+
schedule: events.Schedule.expression('rate(1 min)'),
53+
});
54+
const role = new iam.Role(stack, 'Role', {
55+
assumedBy: new iam.ServicePrincipal('events.amazonaws.com'),
56+
roleName: 'GivenRole',
57+
});
58+
59+
rule.addTarget(new targets.EventBus(
60+
events.EventBus.fromEventBusArn(
61+
stack,
62+
'External',
63+
'arn:aws:events:us-east-1:123456789012:default',
64+
),
65+
{ role },
66+
));
67+
68+
expect(stack).toHaveResource('AWS::Events::Rule', {
69+
Targets: [{
70+
Arn: 'arn:aws:events:us-east-1:123456789012:default',
71+
Id: 'Target0',
72+
RoleArn: {
73+
'Fn::GetAtt': [
74+
'Role1ABCC5F0',
75+
'Arn',
76+
],
77+
},
78+
}],
79+
});
80+
expect(stack).toHaveResource('AWS::IAM::Policy', {
81+
PolicyDocument: {
82+
Statement: [{
83+
Effect: 'Allow',
84+
Action: 'events:PutEvents',
85+
Resource: 'arn:aws:events:us-east-1:123456789012:default',
86+
}],
87+
Version: '2012-10-17',
88+
},
89+
Roles: [{
90+
Ref: 'Role1ABCC5F0',
91+
}],
92+
});
93+
});
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+
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)