-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aws-events-targets) external event bus event target
- Loading branch information
1 parent
6de792c
commit 0b22111
Showing
5 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as events from '@aws-cdk/aws-events'; | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import { singletonEventRole } from './util'; | ||
|
||
/** | ||
* Configuration properties of an Event Bus event | ||
*/ | ||
export interface EventBusProps { | ||
/** | ||
* The target event bus | ||
*/ | ||
readonly eventBus: events.IEventBus; | ||
|
||
/** | ||
* Role to be used to publish the event | ||
* | ||
* @default a new role is created. | ||
*/ | ||
readonly role?: iam.IRole; | ||
} | ||
|
||
/** | ||
* Notify an existing Event Bus of an event | ||
*/ | ||
export class EventBus implements events.IRuleTarget { | ||
private readonly eventBus: events.IEventBus; | ||
private readonly role?: iam.IRole; | ||
|
||
constructor(props: EventBusProps) { | ||
this.eventBus = props.eventBus; | ||
this.role = props.role; | ||
} | ||
|
||
bind(rule: events.IRule, id?: string): events.RuleTargetConfig { | ||
if (this.role) { | ||
this.role.addToPrincipalPolicy(this.putEventStatement()); | ||
} | ||
const role = this.role ?? singletonEventRole(rule, [this.putEventStatement()]); | ||
return { | ||
id: id ? id : '', | ||
arn: this.eventBus.eventBusArn, | ||
role, | ||
}; | ||
} | ||
|
||
private putEventStatement() { | ||
return new iam.PolicyStatement({ | ||
actions: ['events:PutEvents'], | ||
resources: [this.eventBus.eventBusArn], | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
packages/@aws-cdk/aws-events-targets/test/event-bus/event-rule-target.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import '@aws-cdk/assert/jest'; | ||
import * as events from '@aws-cdk/aws-events'; | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import { Stack } from '@aws-cdk/core'; | ||
import * as targets from '../../lib'; | ||
|
||
test('Use EventBus as an event rule target', () => { | ||
const stack = new Stack(); | ||
const rule = new events.Rule(stack, 'Rule', { | ||
schedule: events.Schedule.expression('rate(1 min)'), | ||
}); | ||
|
||
rule.addTarget(new targets.EventBus({ | ||
eventBus: events.EventBus.fromEventBusArn( | ||
stack, | ||
'External', | ||
'arn:aws:events:us-east-1:111111111111:default' | ||
), | ||
})); | ||
|
||
expect(stack).toHaveResource('AWS::Events::Rule', { | ||
Targets: [ | ||
{ | ||
Arn: 'arn:aws:events:us-east-1:111111111111:default', | ||
Id: 'Target0', | ||
RoleArn: { | ||
'Fn::GetAtt': [ | ||
'RuleEventsRoleC51A4248', | ||
'Arn', | ||
], | ||
}, | ||
}, | ||
], | ||
}); | ||
expect(stack).toHaveResource('AWS::IAM::Policy', { | ||
PolicyDocument: { | ||
Statement: [{ | ||
Effect: 'Allow', | ||
Action: 'events:PutEvents', | ||
Resource: 'arn:aws:events:us-east-1:111111111111:default', | ||
}], | ||
Version: '2012-10-17', | ||
}, | ||
Roles: [{ | ||
Ref: 'RuleEventsRoleC51A4248', | ||
}], | ||
}); | ||
}); | ||
|
||
test('with supplied role', () => { | ||
const stack = new Stack(); | ||
const rule = new events.Rule(stack, 'Rule', { | ||
schedule: events.Schedule.expression('rate(1 min)'), | ||
}); | ||
const role = new iam.Role(stack, 'Role', { | ||
assumedBy: new iam.ServicePrincipal('events.amazonaws.com'), | ||
roleName: 'GivenRole', | ||
}); | ||
|
||
rule.addTarget(new targets.EventBus({ | ||
eventBus: events.EventBus.fromEventBusArn( | ||
stack, | ||
'External', | ||
'arn:aws:events:us-east-1:123456789012:default' | ||
), | ||
role, | ||
})); | ||
|
||
expect(stack).toHaveResource('AWS::Events::Rule', { | ||
Targets: [{ | ||
Arn: 'arn:aws:events:us-east-1:123456789012:default', | ||
Id: 'Target0', | ||
RoleArn: { | ||
'Fn::GetAtt': [ | ||
'Role1ABCC5F0', | ||
'Arn', | ||
], | ||
}, | ||
}], | ||
}); | ||
expect(stack).toHaveResource('AWS::IAM::Policy', { | ||
PolicyDocument: { | ||
Statement: [{ | ||
Effect: 'Allow', | ||
Action: 'events:PutEvents', | ||
Resource: 'arn:aws:events:us-east-1:123456789012:default', | ||
}], | ||
Version: '2012-10-17', | ||
}, | ||
Roles: [{ | ||
Ref: 'Role1ABCC5F0', | ||
}], | ||
}); | ||
}); |
83 changes: 83 additions & 0 deletions
83
packages/@aws-cdk/aws-events-targets/test/event-bus/integ.event-bus.expected.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
{ | ||
"Resources": { | ||
"Rule4C995B7F": { | ||
"Type": "AWS::Events::Rule", | ||
"Properties": { | ||
"ScheduleExpression": "rate(1 minute)", | ||
"State": "ENABLED", | ||
"Targets": [ | ||
{ | ||
"Arn": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"arn:aws:events:", | ||
{ | ||
"Ref": "AWS::Region" | ||
}, | ||
":999999999999:event-bus/test-bus" | ||
] | ||
] | ||
}, | ||
"Id": "Target0", | ||
"RoleArn": { | ||
"Fn::GetAtt": [ | ||
"RuleEventsRoleC51A4248", | ||
"Arn" | ||
] | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"RuleEventsRoleC51A4248": { | ||
"Type": "AWS::IAM::Role", | ||
"Properties": { | ||
"AssumeRolePolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "events.amazonaws.com" | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
} | ||
} | ||
}, | ||
"RuleEventsRoleDefaultPolicy0510525D": { | ||
"Type": "AWS::IAM::Policy", | ||
"Properties": { | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "events:PutEvents", | ||
"Effect": "Allow", | ||
"Resource": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
"arn:aws:events:", | ||
{ | ||
"Ref": "AWS::Region" | ||
}, | ||
":999999999999:event-bus/test-bus" | ||
] | ||
] | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
}, | ||
"PolicyName": "RuleEventsRoleDefaultPolicy0510525D", | ||
"Roles": [ | ||
{ | ||
"Ref": "RuleEventsRoleC51A4248" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/@aws-cdk/aws-events-targets/test/event-bus/integ.event-bus.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/// !cdk-integ pragma:ignore-assets | ||
import * as events from '@aws-cdk/aws-events'; | ||
import * as cdk from '@aws-cdk/core'; | ||
import * as targets from '../../lib'; | ||
|
||
const app = new cdk.App(); | ||
|
||
class EventSourceStack extends cdk.Stack { | ||
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) { | ||
super(scope, id, props); | ||
|
||
const rule = new events.Rule(this, 'Rule', { | ||
schedule: events.Schedule.expression('rate(1 minute)'), | ||
}); | ||
rule.addTarget(new targets.EventBus({ | ||
eventBus: events.EventBus.fromEventBusArn( | ||
this, | ||
'External', | ||
`arn:aws:events:${this.region}:999999999999:event-bus/test-bus` | ||
), | ||
})); | ||
} | ||
} | ||
|
||
new EventSourceStack(app, 'event-source-stack'); | ||
app.synth(); |