Skip to content

Commit 3a57548

Browse files
Merge pull request #317 from bucha09/patch-1
Add custom role to cloudwatch event which trigger step functions
2 parents 3172e2b + bab4302 commit 3a57548

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ This is the Serverless Framework plugin for AWS Step Functions.
4242
- [Specify Input or Inputpath](#specify-input-or-inputpath)
4343
- [Specifying a Description](#specifying-a-description)
4444
- [Specifying a Name](#specifying-a-name)
45+
- [Specifying a RoleArn](#specifying-a-rolearn)
4546
- [Specifying a custom CloudWatch EventBus](#specifying-a-custom-cloudwatch-eventbus)
4647
- [Tags](#tags)
4748
- [Commands](#commands)
@@ -1001,6 +1002,33 @@ stepFunctions:
10011002
...
10021003
```
10031004

1005+
#### Specifying a RoleArn
1006+
1007+
You can also specify a CloudWatch Event RoleArn.
1008+
The Amazon Resource Name (ARN) of the role that is used for target invocation.
1009+
1010+
Required: No
1011+
1012+
```yml
1013+
stepFunctions:
1014+
stateMachines:
1015+
cloudwatchEvent:
1016+
events:
1017+
- cloudwatchEvent:
1018+
name: 'my-cloudwatch-event-name'
1019+
iamRole: 'arn:aws:iam::012345678910:role/Events-InvokeStepFunctions-Role'
1020+
event:
1021+
source:
1022+
- "aws.ec2"
1023+
detail-type:
1024+
- "EC2 Instance State-change Notification"
1025+
detail:
1026+
state:
1027+
- pending
1028+
definition:
1029+
...
1030+
```
1031+
10041032
#### Specifying a custom CloudWatch EventBus
10051033

10061034
You can choose which CloudWatch Event bus to listen to:

lib/deploy/events/cloudWatchEvent/compileCloudWatchEventEvents.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = {
2222
let Description;
2323
let Name;
2424
let EventBusName;
25+
let IamRole;
2526

2627
if (typeof eventRule === 'object') {
2728
if (!eventRule.event) {
@@ -43,6 +44,7 @@ module.exports = {
4344
Description = eventRule.description;
4445
Name = eventRule.name;
4546
EventBusName = eventRule.eventBusName;
47+
IamRole = eventRule.iamRole;
4648

4749
if (Input && InputPath) {
4850
const errorMessage = [
@@ -92,12 +94,12 @@ module.exports = {
9294
${InputPath ? `"InputPath": "${InputPath.replace(/\r?\n/g, '')}",` : ''}
9395
"Arn": { "Ref": "${stateMachineLogicalId}" },
9496
"Id": "${cloudWatchId}",
95-
"RoleArn": {
97+
${IamRole ? `"RoleArn":"${IamRole}"` : `"RoleArn": {
9698
"Fn::GetAtt": [
9799
"${cloudWatchIamRoleLogicalId}",
98100
"Arn"
99101
]
100-
}
102+
}`}
101103
}]
102104
}
103105
}
@@ -146,12 +148,18 @@ module.exports = {
146148
[cloudWatchLogicalId]: JSON.parse(cloudWatchEventRuleTemplate),
147149
};
148150

149-
const newPermissionObject = {
150-
[cloudWatchIamRoleLogicalId]: JSON.parse(iamRoleTemplate),
151-
};
151+
const objectsToMerge = [newCloudWatchEventRuleObject];
152+
153+
if (!IamRole) {
154+
const newPermissionObject = {
155+
[cloudWatchIamRoleLogicalId]: JSON.parse(iamRoleTemplate),
156+
};
157+
158+
objectsToMerge.push(newPermissionObject);
159+
}
152160

153161
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
154-
newCloudWatchEventRuleObject, newPermissionObject);
162+
...objectsToMerge);
155163
}
156164
});
157165
}

0 commit comments

Comments
 (0)