Skip to content

Commit f5362f4

Browse files
feat: allow the specification of a custom IAM role for scheduled events
1 parent 1a7da96 commit f5362f4

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,17 @@ events:
579579
rate: rate(2 hours)
580580
```
581581

582+
## Scheduled Events IAM Role
583+
584+
By default, the plugin will create a new IAM role that allows AWS Events to start your state machine. Note that this role is different then the role assumed by the state machine. You can specify your own role instead (it must allow `events.amazonaws.com` to assume it, and it must be able to run `states:StartExecution` on your state machine):
585+
586+
```yaml
587+
events:
588+
- schedule:
589+
rate: rate(2 hours)
590+
role: arn:aws:iam::xxxxxxxx:role/yourRole
591+
592+
582593
### CloudWatch Event
583594
## Simple event definition
584595

lib/deploy/events/schedule/compileScheduledEvents.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,17 @@ module.exports = {
9595
${InputPath ? `"InputPath": "${InputPath}",` : ''}
9696
"Arn": { "Ref": "${stateMachineLogicalId}" },
9797
"Id": "${scheduleId}",
98-
"RoleArn": {
99-
"Fn::GetAtt": [
100-
"${scheduleIamRoleLogicalId}",
101-
"Arn"
102-
]
98+
"RoleArn": ${
99+
event.schedule.role ?
100+
JSON.stringify(event.schedule.role) :
101+
`
102+
{
103+
"Fn::GetAtt": [
104+
"${scheduleIamRoleLogicalId}",
105+
"Arn"
106+
]
107+
}
108+
`
103109
}
104110
}]
105111
}
@@ -149,7 +155,7 @@ module.exports = {
149155
[scheduleLogicalId]: JSON.parse(scheduleTemplate),
150156
};
151157

152-
const newPermissionObject = {
158+
const newPermissionObject = event.schedule.role ? {} : {
153159
[scheduleIamRoleLogicalId]: JSON.parse(iamRoleTemplate),
154160
};
155161

lib/deploy/events/schedule/compileScheduledEvents.test.js

+30
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,36 @@ describe('#httpValidate()', () => {
309309
expect(() => serverlessStepFunctions.compileScheduledEvents()).to.throw(Error);
310310
});
311311

312+
it('should respect role variable', () => {
313+
serverlessStepFunctions.serverless.service.stepFunctions = {
314+
stateMachines: {
315+
first: {
316+
events: [
317+
{
318+
schedule: {
319+
rate: 'rate(10 minutes)',
320+
enabled: false,
321+
role: 'arn:aws:iam::000000000000:role/test-role',
322+
},
323+
},
324+
],
325+
},
326+
},
327+
};
328+
329+
serverlessStepFunctions.compileScheduledEvents();
330+
331+
expect(serverlessStepFunctions.serverless.service
332+
.provider.compiledCloudFormationTemplate.Resources
333+
.FirstScheduleToStepFunctionsRole
334+
).to.equal(undefined);
335+
336+
expect(serverlessStepFunctions.serverless.service
337+
.provider.compiledCloudFormationTemplate.Resources.FirstStepFunctionsEventsRuleSchedule1
338+
.Properties.Targets[0].RoleArn
339+
).to.equal('arn:aws:iam::000000000000:role/test-role');
340+
});
341+
312342
it('should not create corresponding resources when scheduled events are not given', () => {
313343
serverlessStepFunctions.serverless.service.stepFunctions = {
314344
stateMachines: {

0 commit comments

Comments
 (0)