3
3
const _ = require ( 'lodash' ) ;
4
4
const BbPromise = require ( 'bluebird' ) ;
5
5
6
+ const METHOD_SCHEDULER = 'scheduler' ;
7
+ const METHOD_EVENT_BUS = 'eventBus' ;
6
8
module . exports = {
7
9
compileScheduledEvents ( ) {
8
10
const service = this . serverless . service ;
@@ -24,6 +26,8 @@ module.exports = {
24
26
let InputPathsMap ;
25
27
let Name ;
26
28
let Description ;
29
+ let method ;
30
+ let timezone ;
27
31
28
32
// TODO validate rate syntax
29
33
if ( typeof event . schedule === 'object' ) {
@@ -49,6 +53,8 @@ module.exports = {
49
53
InputTemplate = InputTransformer && event . schedule . inputTransformer . inputTemplate ;
50
54
Name = event . schedule . name ;
51
55
Description = event . schedule . description ;
56
+ method = event . schedule . method || METHOD_EVENT_BUS ;
57
+ timezone = event . schedule . timezone ;
52
58
53
59
if ( [ Input , InputPath , InputTransformer ] . filter ( Boolean ) . length > 1 ) {
54
60
const errorMessage = [
@@ -76,6 +82,29 @@ module.exports = {
76
82
// escape quotes to favor JSON.parse
77
83
InputTemplate = InputTemplate . replace ( / \" / g, '\\"' ) ; // eslint-disable-line
78
84
}
85
+ if ( InputTransformer ) {
86
+ if ( method === METHOD_SCHEDULER ) {
87
+ const errorMessage = [
88
+ 'Cannot setup "schedule" event: "inputTransformer" is not supported with "scheduler" mode' ,
89
+ ] . join ( '' ) ;
90
+ throw new this . serverless . classes
91
+ . Error ( errorMessage ) ;
92
+ }
93
+ }
94
+ if ( InputPath && method === METHOD_SCHEDULER ) {
95
+ const errorMessage = [
96
+ 'Cannot setup "schedule" event: "inputPath" is not supported with "scheduler" mode' ,
97
+ ] . join ( '' ) ;
98
+ throw new this . serverless . classes
99
+ . Error ( errorMessage ) ;
100
+ }
101
+ if ( timezone && method !== METHOD_SCHEDULER ) {
102
+ const errorMessage = [
103
+ 'Cannot setup "schedule" event: "timezone" is only supported with "scheduler" mode' ,
104
+ ] . join ( '' ) ;
105
+ throw new this . serverless . classes
106
+ . Error ( errorMessage ) ;
107
+ }
79
108
} else if ( typeof event . schedule === 'string' ) {
80
109
ScheduleExpression = event . schedule ;
81
110
State = 'ENABLED' ;
@@ -92,8 +121,9 @@ module.exports = {
92
121
93
122
const stateMachineLogicalId = this
94
123
. getStateMachineLogicalId ( stateMachineName , stateMachineObj ) ;
95
- const scheduleLogicalId = this
96
- . getScheduleLogicalId ( stateMachineName , scheduleNumberInFunction ) ;
124
+ const scheduleLogicalId = method !== METHOD_SCHEDULER ? this
125
+ . getScheduleLogicalId ( stateMachineName , scheduleNumberInFunction ) : this
126
+ . getSchedulerScheduleLogicalId ( stateMachineName , scheduleNumberInFunction ) ;
97
127
const scheduleIamRoleLogicalId = this
98
128
. getScheduleToStepFunctionsIamRoleLogicalId ( stateMachineName ) ;
99
129
const scheduleId = this . getScheduleId ( stateMachineName ) ;
@@ -110,8 +140,69 @@ module.exports = {
110
140
}
111
141
` ;
112
142
113
- const scheduleTemplate = `
114
- {
143
+ let scheduleTemplate ;
144
+ let iamRoleTemplate ;
145
+ // If condition for the event bridge schedular and it define
146
+ // resource template and iamrole for the same
147
+ if ( method === METHOD_SCHEDULER ) {
148
+ scheduleTemplate = `{
149
+ "Type": "AWS::Scheduler::Schedule",
150
+ "Properties": {
151
+ "ScheduleExpression": "${ ScheduleExpression } ",
152
+ "State": "${ State } ",
153
+ ${ timezone ? `"ScheduleExpressionTimezone": "${ timezone } ",` : '' }
154
+ ${ Name ? `"Name": "${ Name } ",` : '' }
155
+ ${ Description ? `"Description": "${ Description } ",` : '' }
156
+ "Target": {
157
+ "Arn": { "Ref": "${ stateMachineLogicalId } " },
158
+ "RoleArn": ${ roleArn }
159
+ },
160
+ "FlexibleTimeWindow": {
161
+ "Mode": "OFF"
162
+ }
163
+ }
164
+ }` ;
165
+
166
+ iamRoleTemplate = `{
167
+ "Type": "AWS::IAM::Role",
168
+ "Properties": {
169
+ "AssumeRolePolicyDocument": {
170
+ "Version": "2012-10-17",
171
+ "Statement": [
172
+ {
173
+ "Effect": "Allow",
174
+ "Principal": {
175
+ "Service": "scheduler.amazonaws.com"
176
+ },
177
+ "Action": "sts:AssumeRole"
178
+ }
179
+ ]
180
+ },
181
+ "Policies": [
182
+ {
183
+ "PolicyName": "${ policyName } ",
184
+ "PolicyDocument": {
185
+ "Version": "2012-10-17",
186
+ "Statement": [
187
+ {
188
+ "Effect": "Allow",
189
+ "Action": [
190
+ "states:StartExecution"
191
+ ],
192
+ "Resource": {
193
+ "Ref": "${ stateMachineLogicalId } "
194
+ }
195
+ }
196
+ ]
197
+ }
198
+ }
199
+ ]
200
+ }
201
+ }` ;
202
+ } else {
203
+ // else condition for the event rule and
204
+ // it define resource template and iamrole for the same
205
+ scheduleTemplate = `{
115
206
"Type": "AWS::Events::Rule",
116
207
"Properties": {
117
208
"ScheduleExpression": "${ ScheduleExpression } ",
@@ -130,11 +221,8 @@ module.exports = {
130
221
"RoleArn": ${ roleArn }
131
222
}]
132
223
}
133
- }
134
- ` ;
135
-
136
- let iamRoleTemplate = `
137
- {
224
+ }` ;
225
+ iamRoleTemplate = `{
138
226
"Type": "AWS::IAM::Role",
139
227
"Properties": {
140
228
"AssumeRolePolicyDocument": {
@@ -169,8 +257,8 @@ module.exports = {
169
257
}
170
258
]
171
259
}
260
+ }` ;
172
261
}
173
- ` ;
174
262
if ( permissionsBoundary ) {
175
263
const jsonIamRole = JSON . parse ( iamRoleTemplate ) ;
176
264
jsonIamRole . Properties . PermissionsBoundary = permissionsBoundary ;
0 commit comments