Skip to content

Commit ca4a4f7

Browse files
notanmayKillDozerX2
authored andcommitted
feat(EventBridge Scheduler): ✨ add logic for eventbridge scheduler
1 parent 28fa07b commit ca4a4f7

File tree

1 file changed

+97
-8
lines changed

1 file changed

+97
-8
lines changed

lib/deploy/events/schedule/compileScheduledEvents.js

+97-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ module.exports = {
1010
_.forEach(this.getAllStateMachines(), (stateMachineName) => {
1111
const stateMachineObj = this.getStateMachine(stateMachineName);
1212
let scheduleNumberInFunction = 0;
13+
const METHOD_SCHEDULER = 'scheduler';
14+
const METHOD_EVENT_BUS = 'eventBus';
1315

1416
if (stateMachineObj.events) {
1517
_.forEach(stateMachineObj.events, (event) => {
@@ -24,6 +26,8 @@ module.exports = {
2426
let InputPathsMap;
2527
let Name;
2628
let Description;
29+
let method;
30+
let timezone;
2731

2832
// TODO validate rate syntax
2933
if (typeof event.schedule === 'object') {
@@ -49,6 +53,8 @@ module.exports = {
4953
InputTemplate = InputTransformer && event.schedule.inputTransformer.inputTemplate;
5054
Name = event.schedule.name;
5155
Description = event.schedule.description;
56+
method = event.schedule.method || METHOD_EVENT_BUS;
57+
timezone = event.schedule.timezone;
5258

5359
if ([Input, InputPath, InputTransformer].filter(Boolean).length > 1) {
5460
const errorMessage = [
@@ -76,6 +82,34 @@ module.exports = {
7682
// escape quotes to favor JSON.parse
7783
InputTemplate = InputTemplate.replace(/\"/g, '\\"'); // eslint-disable-line
7884
}
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+
}
79113
} else if (typeof event.schedule === 'string') {
80114
ScheduleExpression = event.schedule;
81115
State = 'ENABLED';
@@ -110,8 +144,66 @@ module.exports = {
110144
}
111145
`;
112146

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 = `{
115207
"Type": "AWS::Events::Rule",
116208
"Properties": {
117209
"ScheduleExpression": "${ScheduleExpression}",
@@ -130,11 +222,8 @@ module.exports = {
130222
"RoleArn": ${roleArn}
131223
}]
132224
}
133-
}
134-
`;
135-
136-
let iamRoleTemplate = `
137-
{
225+
}`;
226+
iamRoleTemplate = `{
138227
"Type": "AWS::IAM::Role",
139228
"Properties": {
140229
"AssumeRolePolicyDocument": {
@@ -169,8 +258,8 @@ module.exports = {
169258
}
170259
]
171260
}
261+
}`;
172262
}
173-
`;
174263
if (permissionsBoundary) {
175264
const jsonIamRole = JSON.parse(iamRoleTemplate);
176265
jsonIamRole.Properties.PermissionsBoundary = permissionsBoundary;

0 commit comments

Comments
 (0)