Skip to content

Commit 346a7b7

Browse files
Merge pull request #186 from campus-explorer/scheduled-events-custom-iam-role
feat: allow the specification of a custom IAM role for scheduled events
2 parents 1a7da96 + a72f6f3 commit 346a7b7

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
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 than 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

+13-7
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ module.exports = {
8282
const scheduleId = this.getScheduleId(stateMachineName);
8383
const policyName = this.getSchedulePolicyName(stateMachineName);
8484

85+
const roleArn = event.schedule.role ?
86+
JSON.stringify(event.schedule.role) :
87+
`
88+
{
89+
"Fn::GetAtt": [
90+
"${scheduleIamRoleLogicalId}",
91+
"Arn"
92+
]
93+
}
94+
`;
95+
8596
const scheduleTemplate = `
8697
{
8798
"Type": "AWS::Events::Rule",
@@ -95,12 +106,7 @@ module.exports = {
95106
${InputPath ? `"InputPath": "${InputPath}",` : ''}
96107
"Arn": { "Ref": "${stateMachineLogicalId}" },
97108
"Id": "${scheduleId}",
98-
"RoleArn": {
99-
"Fn::GetAtt": [
100-
"${scheduleIamRoleLogicalId}",
101-
"Arn"
102-
]
103-
}
109+
"RoleArn": ${roleArn}
104110
}]
105111
}
106112
}
@@ -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)