forked from serverless-operations/serverless-step-functions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompileScheduledEvents.js
284 lines (271 loc) · 11.1 KB
/
compileScheduledEvents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
'use strict';
const _ = require('lodash');
const BbPromise = require('bluebird');
const METHOD_SCHEDULER = 'scheduler';
const METHOD_EVENT_BUS = 'eventBus';
module.exports = {
compileScheduledEvents() {
const service = this.serverless.service;
const permissionsBoundary = service.provider.rolePermissionsBoundary;
_.forEach(this.getAllStateMachines(), (stateMachineName) => {
const stateMachineObj = this.getStateMachine(stateMachineName);
let scheduleNumberInFunction = 0;
if (stateMachineObj.events) {
_.forEach(stateMachineObj.events, (event) => {
if (event.schedule) {
scheduleNumberInFunction++;
let ScheduleExpression;
let State;
let Input;
let InputPath;
let InputTransformer;
let InputTemplate;
let InputPathsMap;
let Name;
let Description;
let method;
let timezone;
// TODO validate rate syntax
if (typeof event.schedule === 'object') {
if (!event.schedule.rate) {
const errorMessage = [
`Missing "rate" property for schedule event in stateMachine ${stateMachineName}`,
' The correct syntax is: schedule: rate(10 minutes)',
' OR an object with "rate" property.',
' Please check the README for more info.',
].join('');
throw new this.serverless.classes
.Error(errorMessage);
}
ScheduleExpression = event.schedule.rate;
State = 'ENABLED';
if (event.schedule.enabled === false) {
State = 'DISABLED';
}
Input = event.schedule.input;
InputPath = event.schedule.inputPath;
InputTransformer = event.schedule.inputTransformer;
InputPathsMap = InputTransformer && event.schedule.inputTransformer.inputPathsMap;
InputTemplate = InputTransformer && event.schedule.inputTransformer.inputTemplate;
Name = event.schedule.name;
Description = event.schedule.description;
method = event.schedule.method || METHOD_EVENT_BUS;
timezone = event.schedule.timezone;
if ([Input, InputPath, InputTransformer].filter(Boolean).length > 1) {
const errorMessage = [
'You can\'t set both input & inputPath properties at the',
'same time for schedule events.',
'Please check the AWS docs for more info',
].join('');
throw new this.serverless.classes
.Error(errorMessage);
}
if (Input && typeof Input === 'object') {
Input = JSON.stringify(Input);
}
if (Input && typeof Input === 'string') {
// escape quotes to favor JSON.parse
Input = Input.replace(/\"/g, '\\"'); // eslint-disable-line
}
// no need to escape quotes in inputPathsMap
// because we add it as an object to the template
if (InputPathsMap && typeof InputPathsMap === 'object') {
InputPathsMap = JSON.stringify(InputPathsMap);
}
if (InputTemplate && typeof InputTemplate === 'string') {
// escape quotes to favor JSON.parse
InputTemplate = InputTemplate.replace(/\"/g, '\\"'); // eslint-disable-line
}
if (InputTransformer) {
if (method === METHOD_SCHEDULER) {
const errorMessage = [
'Cannot setup "schedule" event: "inputTransformer" is not supported with "scheduler" mode',
].join('');
throw new this.serverless.classes
.Error(errorMessage);
}
}
if (InputPath && method === METHOD_SCHEDULER) {
const errorMessage = [
'Cannot setup "schedule" event: "inputPath" is not supported with "scheduler" mode',
].join('');
throw new this.serverless.classes
.Error(errorMessage);
}
if (timezone && method !== METHOD_SCHEDULER) {
const errorMessage = [
'Cannot setup "schedule" event: "timezone" is only supported with "scheduler" mode',
].join('');
throw new this.serverless.classes
.Error(errorMessage);
}
} else if (typeof event.schedule === 'string') {
ScheduleExpression = event.schedule;
State = 'ENABLED';
} else {
const errorMessage = [
`Schedule event of stateMachine ${stateMachineName} is not an object nor a string`,
' The correct syntax is: schedule: rate(10 minutes)',
' OR an object with "rate" property.',
' Please check the README for more info.',
].join('');
throw new this.serverless.classes
.Error(errorMessage);
}
const stateMachineLogicalId = this
.getStateMachineLogicalId(stateMachineName, stateMachineObj);
const scheduleLogicalId = method !== METHOD_SCHEDULER ? this
.getScheduleLogicalId(stateMachineName, scheduleNumberInFunction) : this
.getSchedulerScheduleLogicalId(stateMachineName, scheduleNumberInFunction);
const scheduleIamRoleLogicalId = this
.getScheduleToStepFunctionsIamRoleLogicalId(stateMachineName);
const scheduleId = this.getScheduleId(stateMachineName);
const policyName = this.getSchedulePolicyName(stateMachineName);
const roleArn = event.schedule.role
? JSON.stringify(event.schedule.role)
: `
{
"Fn::GetAtt": [
"${scheduleIamRoleLogicalId}",
"Arn"
]
}
`;
let scheduleTemplate;
let iamRoleTemplate;
// If condition for the event bridge schedular and it define
// resource template and iamrole for the same
if (method === METHOD_SCHEDULER) {
scheduleTemplate = `{
"Type": "AWS::Scheduler::Schedule",
"Properties": {
"ScheduleExpression": "${ScheduleExpression}",
"State": "${State}",
${timezone ? `"ScheduleExpressionTimezone": "${timezone}",` : ''}
${Name ? `"Name": "${Name}",` : ''}
${Description ? `"Description": "${Description}",` : ''}
"Target": {
"Arn": { "Ref": "${stateMachineLogicalId}" },
"RoleArn": ${roleArn}
},
"FlexibleTimeWindow": {
"Mode": "OFF"
}
}
}`;
iamRoleTemplate = `{
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "scheduler.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
},
"Policies": [
{
"PolicyName": "${policyName}",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"states:StartExecution"
],
"Resource": {
"Ref": "${stateMachineLogicalId}"
}
}
]
}
}
]
}
}`;
} else {
// else condition for the event rule and
// it define resource template and iamrole for the same
scheduleTemplate = `{
"Type": "AWS::Events::Rule",
"Properties": {
"ScheduleExpression": "${ScheduleExpression}",
"State": "${State}",
${Name ? `"Name": "${Name}",` : ''}
${Description ? `"Description": "${Description}",` : ''}
"Targets": [{
${Input ? `"Input": "${Input}",` : ''}
${InputPath ? `"InputPath": "${InputPath}",` : ''}
${InputTransformer ? `"InputTransformer": {
"InputPathsMap": ${InputPathsMap},
"InputTemplate": "${InputTemplate}"
},` : ''}
"Arn": { "Ref": "${stateMachineLogicalId}" },
"Id": "${scheduleId}",
"RoleArn": ${roleArn}
}]
}
}`;
iamRoleTemplate = `{
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "events.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
},
"Policies": [
{
"PolicyName": "${policyName}",
"PolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"states:StartExecution"
],
"Resource": {
"Ref": "${stateMachineLogicalId}"
}
}
]
}
}
]
}
}`;
}
if (permissionsBoundary) {
const jsonIamRole = JSON.parse(iamRoleTemplate);
jsonIamRole.Properties.PermissionsBoundary = permissionsBoundary;
iamRoleTemplate = JSON.stringify(jsonIamRole);
}
const newScheduleObject = {
[scheduleLogicalId]: JSON.parse(scheduleTemplate),
};
const newPermissionObject = event.schedule.role ? {} : {
[scheduleIamRoleLogicalId]: JSON.parse(iamRoleTemplate),
};
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
newScheduleObject, newPermissionObject);
}
});
}
});
return BbPromise.resolve();
},
};