@@ -10,6 +10,8 @@ module.exports = {
10
10
_ . forEach ( this . getAllStateMachines ( ) , ( stateMachineName ) => {
11
11
const stateMachineObj = this . getStateMachine ( stateMachineName ) ;
12
12
let scheduleNumberInFunction = 0 ;
13
+ const METHOD_SCHEDULER = 'scheduler' ;
14
+ const METHOD_EVENT_BUS = 'eventBus' ;
13
15
14
16
if ( stateMachineObj . events ) {
15
17
_ . forEach ( stateMachineObj . events , ( event ) => {
@@ -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,34 @@ 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
+ 'SCHEDULE_PARAMETER_NOT_SUPPORTED' ,
90
+ ] . join ( '' ) ;
91
+ throw new this . serverless . classes
92
+ . Error ( errorMessage ) ;
93
+ } else {
94
+ InputTransformer = this . formatInputTransformer ( InputTransformer ) ;
95
+ }
96
+ }
97
+ if ( InputPath && method === METHOD_SCHEDULER ) {
98
+ const errorMessage = [
99
+ 'Cannot setup "schedule" event: "inputPath" is not supported with "scheduler" mode' ,
100
+ 'SCHEDULE_PARAMETER_NOT_SUPPORTED' ,
101
+ ] . join ( '' ) ;
102
+ throw new this . serverless . classes
103
+ . Error ( errorMessage ) ;
104
+ }
105
+ if ( timezone && method !== METHOD_SCHEDULER ) {
106
+ const errorMessage = [
107
+ 'Cannot setup "schedule" event: "timezone" is only supported with "scheduler" mode' ,
108
+ 'SCHEDULE_PARAMETER_NOT_SUPPORTED' ,
109
+ ] . join ( '' ) ;
110
+ throw new this . serverless . classes
111
+ . Error ( errorMessage ) ;
112
+ }
79
113
} else if ( typeof event . schedule === 'string' ) {
80
114
ScheduleExpression = event . schedule ;
81
115
State = 'ENABLED' ;
@@ -110,8 +144,66 @@ module.exports = {
110
144
}
111
145
` ;
112
146
113
- const scheduleTemplate = `
114
- {
147
+ let scheduleTemplate ;
148
+ let iamRoleTemplate ;
149
+
150
+ if ( method === METHOD_SCHEDULER ) {
151
+ scheduleTemplate = `{
152
+ "Type": "AWS::Scheduler::Schedule",
153
+ "Properties": {
154
+ "ScheduleExpression": "${ ScheduleExpression } ",
155
+ "State": "${ State } ",
156
+ ${ timezone ? `"ScheduleExpressionTimezone": "${ timezone } ",` : '' }
157
+ ${ Name ? `"Name": "${ Name } ",` : '' }
158
+ ${ Description ? `"Description": "${ Description } ",` : '' }
159
+ "Target": {
160
+ "Arn": { "Ref": "${ stateMachineLogicalId } " },
161
+ "RoleArn": ${ roleArn }
162
+ },
163
+ "FlexibleTimeWindow": {
164
+ "Mode": "OFF"
165
+ }
166
+ }
167
+ }` ;
168
+
169
+ iamRoleTemplate = `{
170
+ "Type": "AWS::IAM::Role",
171
+ "Properties": {
172
+ "AssumeRolePolicyDocument": {
173
+ "Version": "2012-10-17",
174
+ "Statement": [
175
+ {
176
+ "Effect": "Allow",
177
+ "Principal": {
178
+ "Service": "scheduler.amazonaws.com"
179
+ },
180
+ "Action": "sts:AssumeRole"
181
+ }
182
+ ]
183
+ },
184
+ "Policies": [
185
+ {
186
+ "PolicyName": "${ policyName } ",
187
+ "PolicyDocument": {
188
+ "Version": "2012-10-17",
189
+ "Statement": [
190
+ {
191
+ "Effect": "Allow",
192
+ "Action": [
193
+ "states:StartExecution"
194
+ ],
195
+ "Resource": {
196
+ "Ref": "${ stateMachineLogicalId } "
197
+ }
198
+ }
199
+ ]
200
+ }
201
+ }
202
+ ]
203
+ }
204
+ }` ;
205
+ } else {
206
+ scheduleTemplate = `{
115
207
"Type": "AWS::Events::Rule",
116
208
"Properties": {
117
209
"ScheduleExpression": "${ ScheduleExpression } ",
@@ -130,11 +222,8 @@ module.exports = {
130
222
"RoleArn": ${ roleArn }
131
223
}]
132
224
}
133
- }
134
- ` ;
135
-
136
- let iamRoleTemplate = `
137
- {
225
+ }` ;
226
+ iamRoleTemplate = `{
138
227
"Type": "AWS::IAM::Role",
139
228
"Properties": {
140
229
"AssumeRolePolicyDocument": {
@@ -169,8 +258,8 @@ module.exports = {
169
258
}
170
259
]
171
260
}
261
+ }` ;
172
262
}
173
- ` ;
174
263
if ( permissionsBoundary ) {
175
264
const jsonIamRole = JSON . parse ( iamRoleTemplate ) ;
176
265
jsonIamRole . Properties . PermissionsBoundary = permissionsBoundary ;
0 commit comments