Skip to content

Commit 7a8009f

Browse files
authored
Merge pull request #290 from horike37/feature/eventBridge_support
Feature/event bridge support
2 parents 3347b13 + 044539a commit 7a8009f

File tree

5 files changed

+75
-43
lines changed

5 files changed

+75
-43
lines changed

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ events:
820820
role: arn:aws:iam::xxxxxxxx:role/yourRole
821821
```
822822

823-
### CloudWatch Event
823+
### CloudWatch Event / EventBridge
824824

825825
#### Simple event definition
826826

@@ -845,9 +845,31 @@ stepFunctions:
845845
...
846846
```
847847

848+
You can alternatively use EventBridge:
849+
850+
```yml
851+
stepFunctions:
852+
stateMachines:
853+
first:
854+
events:
855+
- eventBridge:
856+
event:
857+
source:
858+
- "aws.ec2"
859+
detail-type:
860+
- "EC2 Instance State-change Notification"
861+
detail:
862+
state:
863+
- pending
864+
definition:
865+
...
866+
```
867+
868+
All the configurations in this section applies to both `cloudwatchEvent` and `eventBridge`.
869+
848870
#### Enabling / Disabling
849871

850-
**Note:** `cloudwatchEvent` events are enabled by default.
872+
**Note:** `cloudwatchEvent` and `eventBridge` events are enabled by default.
851873

852874
This will create and attach a disabled `cloudwatchEvent` event for the `myCloudWatch` statemachine.
853875

lib/deploy/events/cloudWatchEvent/compileCloudWatchEventEvents.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ module.exports = {
77
compileCloudWatchEventEvents() {
88
_.forEach(this.getAllStateMachines(), (stateMachineName) => {
99
const stateMachineObj = this.getStateMachine(stateMachineName);
10-
let cloudWatchEventNumberInFunction = 0;
10+
let eventRuleNumberInFunction = 0;
1111

1212
if (stateMachineObj.events) {
1313
_.forEach(stateMachineObj.events, (event) => {
14-
if (event.cloudwatchEvent) {
15-
cloudWatchEventNumberInFunction++;
14+
const eventRule = event.cloudwatchEvent || event.eventBridge;
15+
16+
if (eventRule) {
17+
eventRuleNumberInFunction++;
1618
let EventPattern;
1719
let State;
1820
let Input;
@@ -21,8 +23,8 @@ module.exports = {
2123
let Name;
2224
let EventBusName;
2325

24-
if (typeof event.cloudwatchEvent === 'object') {
25-
if (!event.cloudwatchEvent.event) {
26+
if (typeof eventRule === 'object') {
27+
if (!eventRule.event) {
2628
const errorMessage = [
2729
`Missing "event" property for cloudwatch event in stateMachine ${stateMachineName}`, // eslint-disable-line max-len
2830
' Please check the docs for more info.',
@@ -31,16 +33,16 @@ module.exports = {
3133
.Error(errorMessage);
3234
}
3335

34-
EventPattern = JSON.stringify(event.cloudwatchEvent.event);
36+
EventPattern = JSON.stringify(eventRule.event);
3537
State = 'ENABLED';
36-
if (event.cloudwatchEvent.enabled === false) {
38+
if (eventRule.enabled === false) {
3739
State = 'DISABLED';
3840
}
39-
Input = event.cloudwatchEvent.input;
40-
InputPath = event.cloudwatchEvent.inputPath;
41-
Description = event.cloudwatchEvent.description;
42-
Name = event.cloudwatchEvent.name;
43-
EventBusName = event.cloudwatchEvent.eventBusName;
41+
Input = eventRule.input;
42+
InputPath = eventRule.inputPath;
43+
Description = eventRule.description;
44+
Name = eventRule.name;
45+
EventBusName = eventRule.eventBusName;
4446

4547
if (Input && InputPath) {
4648
const errorMessage = [
@@ -70,7 +72,7 @@ module.exports = {
7072
const stateMachineLogicalId = this
7173
.getStateMachineLogicalId(stateMachineName, stateMachineObj);
7274
const cloudWatchLogicalId = this
73-
.getCloudWatchEventLogicalId(stateMachineName, cloudWatchEventNumberInFunction);
75+
.getCloudWatchEventLogicalId(stateMachineName, eventRuleNumberInFunction);
7476
const cloudWatchIamRoleLogicalId = this
7577
.getCloudWatchEventToStepFunctionsIamRoleLogicalId(stateMachineName);
7678
const cloudWatchId = this.getCloudWatchEventId(stateMachineName);

lib/deploy/events/cloudWatchEvent/compileCloudWatchEventEvents.test.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const itParam = require('mocha-param');
34
const expect = require('chai').expect;
45
const Serverless = require('serverless/lib/Serverless');
56
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider');
@@ -21,13 +22,13 @@ describe('awsCompileCloudWatchEventEvents', () => {
2122
});
2223

2324
describe('#compileCloudWatchEventEvents()', () => {
24-
it('should throw an error if cloudwatch event type is not an object', () => {
25+
itParam('should throw an error if event type is not an object', ['cloudwatchEvent', 'eventBridge'], (source) => {
2526
serverlessStepFunctions.serverless.service.stepFunctions = {
2627
stateMachines: {
2728
first: {
2829
events: [
2930
{
30-
cloudwatchEvent: 42,
31+
[source]: 42,
3132
},
3233
],
3334
},
@@ -37,13 +38,13 @@ describe('awsCompileCloudWatchEventEvents', () => {
3738
expect(() => serverlessStepFunctions.compileCloudWatchEventEvents()).to.throw(Error);
3839
});
3940

40-
it('should throw an error if the "event" property is not given', () => {
41+
itParam('should throw an error if the "event" property is not given', ['cloudwatchEvent', 'eventBridge'], (source) => {
4142
serverlessStepFunctions.serverless.service.stepFunctions = {
4243
stateMachines: {
4344
first: {
4445
events: [
4546
{
46-
cloudwatchEvent: {
47+
[source]: {
4748
event: null,
4849
},
4950
},
@@ -55,13 +56,13 @@ describe('awsCompileCloudWatchEventEvents', () => {
5556
expect(() => serverlessStepFunctions.compileCloudWatchEventEvents()).to.throw(Error);
5657
});
5758

58-
it('should create corresponding resources when cloudwatch events are given', () => {
59+
itParam('should create corresponding resources when events are given', ['cloudwatchEvent', 'eventBridge'], (source) => {
5960
serverlessStepFunctions.serverless.service.stepFunctions = {
6061
stateMachines: {
6162
first: {
6263
events: [
6364
{
64-
cloudwatchEvent: {
65+
[source]: {
6566
event: {
6667
source: ['aws.ec2'],
6768
'detail-type': ['EC2 Instance State-change Notification'],
@@ -71,7 +72,7 @@ describe('awsCompileCloudWatchEventEvents', () => {
7172
},
7273
},
7374
{
74-
cloudwatchEvent: {
75+
[source]: {
7576
event: {
7677
source: ['aws.ec2'],
7778
'detail-type': ['EC2 Instance State-change Notification'],
@@ -96,13 +97,13 @@ describe('awsCompileCloudWatchEventEvents', () => {
9697
.FirstEventToStepFunctionsRole.Type).to.equal('AWS::IAM::Role');
9798
});
9899

99-
it('should respect enabled variable, defaulting to true', () => {
100+
itParam('should respect enabled variable, defaulting to true', ['cloudwatchEvent', 'eventBridge'], (source) => {
100101
serverlessStepFunctions.serverless.service.stepFunctions = {
101102
stateMachines: {
102103
first: {
103104
events: [
104105
{
105-
cloudwatchEvent: {
106+
[source]: {
106107
event: {
107108
source: ['aws.ec2'],
108109
'detail-type': ['EC2 Instance State-change Notification'],
@@ -112,7 +113,7 @@ describe('awsCompileCloudWatchEventEvents', () => {
112113
},
113114
},
114115
{
115-
cloudwatchEvent: {
116+
[source]: {
116117
event: {
117118
source: ['aws.ec2'],
118119
'detail-type': ['EC2 Instance State-change Notification'],
@@ -122,7 +123,7 @@ describe('awsCompileCloudWatchEventEvents', () => {
122123
},
123124
},
124125
{
125-
cloudwatchEvent: {
126+
[source]: {
126127
event: {
127128
source: ['aws.ec2'],
128129
'detail-type': ['EC2 Instance State-change Notification'],
@@ -148,13 +149,13 @@ describe('awsCompileCloudWatchEventEvents', () => {
148149
.Properties.State).to.equal('ENABLED');
149150
});
150151

151-
it('should respect inputPath variable', () => {
152+
itParam('should respect inputPath variable', ['cloudwatchEvent', 'eventBridge'], (source) => {
152153
serverlessStepFunctions.serverless.service.stepFunctions = {
153154
stateMachines: {
154155
first: {
155156
events: [
156157
{
157-
cloudwatchEvent: {
158+
[source]: {
158159
event: {
159160
source: ['aws.ec2'],
160161
'detail-type': ['EC2 Instance State-change Notification'],
@@ -176,13 +177,13 @@ describe('awsCompileCloudWatchEventEvents', () => {
176177
.Properties.Targets[0].InputPath).to.equal('$.stageVariables');
177178
});
178179

179-
it('should respect input variable', () => {
180+
itParam('should respect input variable', ['cloudwatchEvent', 'eventBridge'], (source) => {
180181
serverlessStepFunctions.serverless.service.stepFunctions = {
181182
stateMachines: {
182183
first: {
183184
events: [
184185
{
185-
cloudwatchEvent: {
186+
[source]: {
186187
event: {
187188
source: ['aws.ec2'],
188189
'detail-type': ['EC2 Instance State-change Notification'],
@@ -204,13 +205,13 @@ describe('awsCompileCloudWatchEventEvents', () => {
204205
.Properties.Targets[0].Input).to.equal('{"key":"value"}');
205206
});
206207

207-
it('should respect description variable', () => {
208+
itParam('should respect description variable', ['cloudwatchEvent', 'eventBridge'], (source) => {
208209
serverlessStepFunctions.serverless.service.stepFunctions = {
209210
stateMachines: {
210211
first: {
211212
events: [
212213
{
213-
cloudwatchEvent: {
214+
[source]: {
214215
event: {
215216
source: ['aws.ec2'],
216217
'detail-type': ['EC2 Instance State-change Notification'],
@@ -233,13 +234,13 @@ describe('awsCompileCloudWatchEventEvents', () => {
233234
.Properties.Description).to.equal('test description');
234235
});
235236

236-
it('should respect name variable', () => {
237+
itParam('should respect name variable', ['cloudwatchEvent', 'eventBridge'], (source) => {
237238
serverlessStepFunctions.serverless.service.stepFunctions = {
238239
stateMachines: {
239240
first: {
240241
events: [
241242
{
242-
cloudwatchEvent: {
243+
[source]: {
243244
event: {
244245
source: ['aws.ec2'],
245246
'detail-type': ['EC2 Instance State-change Notification'],
@@ -262,13 +263,13 @@ describe('awsCompileCloudWatchEventEvents', () => {
262263
.Properties.Name).to.equal('test-event-name');
263264
});
264265

265-
it('should respect eventBusName variable', () => {
266+
itParam('should respect eventBusName variable', ['cloudwatchEvent', 'eventBridge'], (source) => {
266267
serverlessStepFunctions.serverless.service.stepFunctions = {
267268
stateMachines: {
268269
first: {
269270
events: [
270271
{
271-
cloudwatchEvent: {
272+
[source]: {
272273
event: {
273274
source: ['aws.ec2'],
274275
'detail-type': ['EC2 Instance State-change Notification'],
@@ -292,13 +293,13 @@ describe('awsCompileCloudWatchEventEvents', () => {
292293
.Properties.EventBusName).to.equal('custom-event-bus');
293294
});
294295

295-
it('should respect input variable as an object', () => {
296+
itParam('should respect input variable as an object', ['cloudwatchEvent', 'eventBridge'], (source) => {
296297
serverlessStepFunctions.serverless.service.stepFunctions = {
297298
stateMachines: {
298299
first: {
299300
events: [
300301
{
301-
cloudwatchEvent: {
302+
[source]: {
302303
event: {
303304
source: ['aws.ec2'],
304305
'detail-type': ['EC2 Instance State-change Notification'],
@@ -322,13 +323,13 @@ describe('awsCompileCloudWatchEventEvents', () => {
322323
.Properties.Targets[0].Input).to.equal('{"key":"value"}');
323324
});
324325

325-
it('should throw an error when both Input and InputPath are set', () => {
326+
itParam('should throw an error when both Input and InputPath are set', ['cloudwatchEvent', 'eventBridge'], (source) => {
326327
serverlessStepFunctions.serverless.service.stepFunctions = {
327328
stateMachines: {
328329
first: {
329330
events: [
330331
{
331-
cloudwatchEvent: {
332+
[source]: {
332333
event: {
333334
source: ['aws.ec2'],
334335
'detail-type': ['EC2 Instance State-change Notification'],
@@ -349,13 +350,13 @@ describe('awsCompileCloudWatchEventEvents', () => {
349350
expect(() => serverlessStepFunctions.compileCloudWatchEventEvents()).to.throw(Error);
350351
});
351352

352-
it('should respect variables if multi-line variables is given', () => {
353+
itParam('should respect variables if multi-line variables is given', ['cloudwatchEvent', 'eventBridge'], (source) => {
353354
serverlessStepFunctions.serverless.service.stepFunctions = {
354355
stateMachines: {
355356
first: {
356357
events: [
357358
{
358-
cloudwatchEvent: {
359+
[source]: {
359360
event: {
360361
source: ['aws.ec2'],
361362
'detail-type': ['EC2 Instance State-change Notification \n with newline'],
@@ -381,7 +382,7 @@ describe('awsCompileCloudWatchEventEvents', () => {
381382
.FirstEventsRuleCloudWatchEvent1.Properties.Targets[0].Input).to.equal('{"key":"value"}');
382383
});
383384

384-
it('should not create corresponding resources when cloudwatch events are not given', () => {
385+
it('should not create corresponding resources when events are not given', () => {
385386
serverlessStepFunctions.serverless.service.stepFunctions = {
386387
stateMachines: {
387388
first: {

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"lint-staged": "^8.1.4",
3838
"mocha": "^5.2.0",
3939
"mocha-lcov-reporter": "^1.2.0",
40+
"mocha-param": "^2.0.0",
4041
"semantic-release": "^15.13.19",
4142
"sinon": "^1.17.5"
4243
},

0 commit comments

Comments
 (0)