Skip to content

Commit efe9f31

Browse files
Merge pull request #5 from horike37/master
sync
2 parents 110c938 + f9e913f commit efe9f31

File tree

7 files changed

+59
-12
lines changed

7 files changed

+59
-12
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ stepFunctions:
444444

445445
#### Customizing request body mapping templates
446446

447-
The plugin generates default body mapping templates for `application/json` and `application/x-www-form-urlencoded` content types. If you'd like to add more content types or customize the default ones, you can do so by including them in `serverless.yml`:
447+
The plugin generates default body mapping templates for `application/json` and `application/x-www-form-urlencoded` content types. If you'd like to add more content types or customize the default ones, you can do so by including your custom [API Gateway request mapping template](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html) in `serverless.yml` like so:
448448

449449
```yml
450450
stepFunctions:
@@ -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

Lines changed: 13 additions & 7 deletions
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

Lines changed: 30 additions & 0 deletions
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: {

lib/deploy/stepFunctions/compileIamRole.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function getGluePermissions() {
100100

101101
function getEcsPermissions() {
102102
return [{
103-
action: 'ecs:RunTask,ecs:StopTask,ecs:DescribeTasks',
103+
action: 'ecs:RunTask,ecs:StopTask,ecs:DescribeTasks,iam:PassRole',
104104
resource: '*',
105105
}, {
106106
action: 'events:PutTargets,events:PutRule,events:DescribeRule',

lib/deploy/stepFunctions/compileIamRole.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ describe('#compileIamRole', () => {
663663
.Properties.Policies[0].PolicyDocument.Statement;
664664

665665
const ecsPermissions = statements.filter(s =>
666-
_.isEqual(s.Action, ['ecs:RunTask', 'ecs:StopTask', 'ecs:DescribeTasks'])
666+
_.isEqual(s.Action, ['ecs:RunTask', 'ecs:StopTask', 'ecs:DescribeTasks', 'iam:PassRole'])
667667
);
668668
expect(ecsPermissions).to.have.lengthOf(1);
669669
expect(ecsPermissions[0].Resource).to.equal('*');

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-step-functions",
3-
"version": "1.16.0",
3+
"version": "1.17.1",
44
"description": "The module is AWS Step Functions plugin for Serverless Framework",
55
"main": "lib/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)