11'use strict' ;
22const _ = require ( 'lodash' ) ;
33const BbPromise = require ( 'bluebird' ) ;
4- const path = require ( 'path' ) ;
54
65module . exports = {
76 compileHttpIamRole ( ) {
@@ -14,15 +13,60 @@ module.exports = {
1413 return BbPromise . resolve ( ) ;
1514 }
1615
17- const iamRoleApiGatewayToStepFunctionsTemplate =
18- JSON . stringify ( this . serverless . utils . readFileSync (
19- path . join ( __dirname ,
20- 'apigateway-to-stepfunctions-assume-role.json' ) )
21- ) ;
16+ const iamRoleApiGatewayToStepFunctionsAction = [
17+ 'states:StartExecution' ,
18+ ] ;
19+
20+ // generate IAM Role action by http.action parameter.
21+ this . pluginhttpValidated . events . forEach ( ( event ) => {
22+ if ( ! _ . has ( event , 'http' ) ) return ;
23+
24+ if ( _ . has ( event . http , 'action' ) ) {
25+ const actionName = `states:${ event . http . action } ` ;
26+
27+ if ( iamRoleApiGatewayToStepFunctionsAction . indexOf ( actionName ) === - 1 ) {
28+ iamRoleApiGatewayToStepFunctionsAction . push ( actionName ) ;
29+ }
30+ }
31+ } ) ;
32+
33+ const iamRoleApiGatewayToStepFunctions = {
34+ Type : 'AWS::IAM::Role' ,
35+ Properties : {
36+ AssumeRolePolicyDocument : {
37+ Version : '2012-10-17' ,
38+ Statement : [
39+ {
40+ Effect : 'Allow' ,
41+ Principal : {
42+ Service : 'apigateway.amazonaws.com' ,
43+ } ,
44+ Action : 'sts:AssumeRole' ,
45+ } ,
46+ ] ,
47+ } ,
48+ Policies : [
49+ {
50+ PolicyName : 'apigatewaytostepfunctions' ,
51+ PolicyDocument : {
52+ Version : '2012-10-17' ,
53+ Statement : [
54+ {
55+ Effect : 'Allow' ,
56+ Action : iamRoleApiGatewayToStepFunctionsAction ,
57+ Resource : '*' ,
58+ } ,
59+ ] ,
60+ } ,
61+ } ,
62+ ] ,
63+ } ,
64+ } ;
65+
2266
2367 const getApiToStepFunctionsIamRoleLogicalId = this . getApiToStepFunctionsIamRoleLogicalId ( ) ;
2468 const newIamRoleStateMachineExecutionObject = {
25- [ getApiToStepFunctionsIamRoleLogicalId ] : JSON . parse ( iamRoleApiGatewayToStepFunctionsTemplate ) ,
69+ [ getApiToStepFunctionsIamRoleLogicalId ] : iamRoleApiGatewayToStepFunctions ,
2670 } ;
2771
2872 _ . merge ( this . serverless . service . provider . compiledCloudFormationTemplate . Resources ,
0 commit comments