Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(AWS Schedule): Support groupName for scheduler method #12357

Open
wants to merge 2 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/providers/aws/events/schedule.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,14 @@ functions:
key1: value1
key2: value2
```

You can configure a scheduler group for the schedules with the property `groupName` which can be either the name of or a ref to an existing group. You can create the schedule group as a resource.

```yaml
resources:
Resources:
- SchedulerGroup:
Type: AWS::Scheduler::ScheduleGroup
Properties:
Name: ${self:service}-${sls:stage}
```
11 changes: 11 additions & 0 deletions lib/plugins/aws/package/compile/events/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class AwsCompileScheduledEvents {
enabled: { type: 'boolean' },
name: { type: 'string', minLength: 1, maxLength: 64, pattern: '[\\.\\-_A-Za-z0-9]+' },
description: { type: 'string', maxLength: 512 },
groupName: { type: 'string', maxLength: 64, pattern: '[\\.\\-_A-Za-z0-9]+' },
input: {
anyOf: [
{ type: 'string', maxLength: 8192 },
Expand Down Expand Up @@ -114,6 +115,7 @@ class AwsCompileScheduledEvents {
if (event.schedule) {
let ScheduleExpressions;
let State;
let GroupName;
let Input;
let InputPath;
let InputTransformer;
Expand All @@ -130,6 +132,7 @@ class AwsCompileScheduledEvents {
if (event.schedule.enabled === false) {
State = 'DISABLED';
}
GroupName = event.schedule.groupName;
Input = event.schedule.input;
InputPath = event.schedule.inputPath;
InputTransformer = event.schedule.inputTransformer;
Expand All @@ -151,6 +154,13 @@ class AwsCompileScheduledEvents {
);
}

if (GroupName && method !== METHOD_SCHEDULER) {
throw new ServerlessError(
'Cannot setup "schedule" event: "groupName" is only supported with "scheduler" mode',
'SCHEDULE_PARAMETER_NOT_SUPPORTED'
);
}

if (Input && typeof Input === 'object') {
if (typeof Input.body === 'string') {
Input.body = JSON.parse(Input.body);
Expand Down Expand Up @@ -226,6 +236,7 @@ class AwsCompileScheduledEvents {
Name,
Description,
ScheduleExpressionTimezone: timezone,
GroupName,
},
};
} else {
Expand Down
30 changes: 30 additions & 0 deletions test/unit/lib/plugins/aws/package/compile/events/schedule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ describe('test/unit/lib/plugins/aws/package/compile/events/schedule.test.js', ()
method: METHOD_SCHEDULER,
name: 'scheduler-scheduled-event',
description: 'Scheduler Scheduled Event',
groupName: 'Scheduler Group Name',
input: '{"key":"array"}',
},
},
Expand Down Expand Up @@ -219,6 +220,19 @@ describe('test/unit/lib/plugins/aws/package/compile/events/schedule.test.js', ()
expect(scheduleCfResources[9].Properties.Description).to.be.undefined;
});

it('should respect the "groupName" variable', () => {
expect(scheduleCfResources[0].Properties.GroupName).to.be.undefined;
expect(scheduleCfResources[1].Properties.GroupName).to.be.undefined;
expect(scheduleCfResources[2].Properties.GroupName).to.be.undefined;
expect(scheduleCfResources[3].Properties.GroupName).to.be.undefined;
expect(scheduleCfResources[4].Properties.GroupName).to.be.undefined;
expect(scheduleCfResources[5].Properties.GroupName).to.be.undefined;
expect(scheduleCfResources[6].Properties.GroupName).to.be.undefined;
expect(scheduleCfResources[7].Properties.GroupName).to.be.undefined;
expect(scheduleCfResources[8].Properties.GroupName).to.be.equal('Scheduler Group Name');
expect(scheduleCfResources[9].Properties.GroupName).to.be.undefined;
});

it('should respect the "inputPath" variable', () => {
expect(scheduleCfResources[0].Properties.Targets[0].InputPath).to.be.undefined;
expect(scheduleCfResources[1].Properties.Targets[0].InputPath).to.equal('$.stageVariables');
Expand Down Expand Up @@ -317,6 +331,22 @@ describe('test/unit/lib/plugins/aws/package/compile/events/schedule.test.js', ()
);
});

it('should throw when passing "groupName" to method:eventBus resources', async () => {
const events = [
{
schedule: {
rate: 'rate(15 minutes)',
method: METHOD_EVENT_BUS,
groupName: 'Group Name',
},
},
];

await expect(run(events))
.to.be.eventually.rejectedWith(ServerlessError)
.and.have.property('code', 'SCHEDULE_PARAMETER_NOT_SUPPORTED');
});

it('should throw when passing "inputPath" to method:schedule resources', async () => {
const events = [
{
Expand Down