Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions docs/events/schedule.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ However, `AWS::Scheduler::Schedule` has much higher limits (1,000,000 events), a
`method` can be set in order to migrate to this trigger type seamlessly. It also allows you to specify a timezone to run your event based on local time.
The default method is `eventBus`, which configures an `AWS::Event::Rule`.

By default, `scheduler` uses the function execution role as target role.
You can provide `roleArn` to use a dedicated role for EventBridge Scheduler.
By default, `scheduler` uses the function execution role as the target role and the `default` schedule group.

You can provide:

- roleArn to use a dedicated role for EventBridge Scheduler.
- groupName to use another EventBridge Scheduler schedule group.

```yaml
functions:
Expand All @@ -99,6 +103,7 @@ functions:
- schedule:
method: scheduler
roleArn: arn:aws:iam::123456789012:role/scheduler-execution-role
groupName: custom-scheduler-group
rate:
- cron(0 0/4 ? * MON-FRI *)
timezone: America/New_York
Expand Down
20 changes: 20 additions & 0 deletions lib/plugins/aws/package/compile/events/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ class AwsCompileScheduledEvents {
roleArn: {
anyOf: [{ type: 'string' }, { $ref: '#/definitions/awsCfFunction' }],
},
groupName: {
anyOf: [
{
type: 'string',
minLength: 1,
maxLength: 64,
pattern: '^[0-9a-zA-Z-_.]+$',
},
{ $ref: '#/definitions/awsCfFunction' },
],
},
timezone: {
type: 'string',
pattern: '[\\w\\-\\/]+',
Expand Down Expand Up @@ -124,6 +135,7 @@ class AwsCompileScheduledEvents {
let Description;
let method;
let roleArn;
let groupName;
let timezone;

if (typeof event.schedule === 'object') {
Expand All @@ -137,6 +149,7 @@ class AwsCompileScheduledEvents {
InputPath = event.schedule.inputPath;
InputTransformer = event.schedule.inputTransformer;
Name = event.schedule.name;
groupName = event.schedule.groupName;
timezone = event.schedule.timezone;
Description = event.schedule.description;

Expand Down Expand Up @@ -186,6 +199,12 @@ class AwsCompileScheduledEvents {
'SCHEDULE_PARAMETER_NOT_SUPPORTED'
);
}
if (groupName && method !== METHOD_SCHEDULER) {
throw new ServerlessError(
'Cannot setup "schedule" event: "groupName" is only supported with "scheduler" mode',
'SCHEDULE_PARAMETER_NOT_SUPPORTED'
);
}
} else {
ScheduleExpressions = [event.schedule];
State = 'ENABLED';
Expand Down Expand Up @@ -227,6 +246,7 @@ class AwsCompileScheduledEvents {
Mode: 'OFF',
},
Name,
GroupName: groupName,
Description,
ScheduleExpressionTimezone: timezone,
},
Expand Down
47 changes: 47 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 @@ -380,6 +380,37 @@ describe('test/unit/lib/plugins/aws/package/compile/events/schedule.test.js', ()
.and.have.property('code', 'SCHEDULE_PARAMETER_NOT_SUPPORTED');
});

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

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

it('should throw when passing "groupName" without method:scheduler specified', async () => {
const events = [
{
schedule: {
rate: 'rate(15 minutes)',
groupName: 'custom-scheduler-group',
},
},
];

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

it('should have not scheduler policies when there are no scheduler schedules', async () => {
const events = [
{
Expand Down Expand Up @@ -453,4 +484,20 @@ describe('test/unit/lib/plugins/aws/package/compile/events/schedule.test.js', ()
'arn:aws:iam::123456789012:role/scheduler-execution-role'
);
});

it('should pass explicit schedule groupName to method:scheduler resources', async () => {
const events = [
{
schedule: {
rate: 'rate(15 minutes)',
method: METHOD_SCHEDULER,
groupName: 'custom-scheduler-group',
},
},
];

const { scheduleCfResources } = await run(events);

expect(scheduleCfResources[0].Properties.GroupName).to.equal('custom-scheduler-group');
});
});
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export interface AWS {
};
method?: 'eventBus' | 'scheduler';
roleArn?: AwsCfFunction | string;
groupName?: AwsCfFunction | string;
timezone?: string;
};
}
Expand Down
Loading