Skip to content

Commit 14b54e2

Browse files
committed
feat: action parameter support for event.http
BREAKING CHANGE: action key in config event.http is now used for creating dynamic IAM Role and API Gateway Request Template to passthru request
1 parent 9b56ca1 commit 14b54e2

File tree

3 files changed

+66
-45
lines changed

3 files changed

+66
-45
lines changed

lib/deploy/events/apiGateway/apigateway-to-stepfunctions-assume-role.json

Lines changed: 0 additions & 35 deletions
This file was deleted.

lib/deploy/events/apiGateway/iamRole.js

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22
const _ = require('lodash');
33
const BbPromise = require('bluebird');
4-
const path = require('path');
54

65
module.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,

lib/deploy/events/apiGateway/methods.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ module.exports = {
150150
],
151151
};
152152
const iamRole = _.get(http, 'iamRole', defaultIamRole);
153+
let integrationAction = ':states:action/StartExecution';
154+
let passthroughBehavior = 'NEVER';
155+
if (http && http.action) {
156+
integrationAction = `:states:action/${http.action}`;
157+
passthroughBehavior = 'WHEN_NO_TEMPLATES';
158+
}
159+
153160
const integration = {
154161
IntegrationHttpMethod: 'POST',
155162
Type: 'AWS',
@@ -162,12 +169,11 @@ module.exports = {
162169
{
163170
Ref: 'AWS::Region',
164171
},
165-
(!http || !http.is_describe) ?
166-
':states:action/StartExecution' : ':states:action/DescribeExecution',
172+
integrationAction,
167173
],
168174
],
169175
},
170-
PassthroughBehavior: 'NEVER',
176+
PassthroughBehavior: passthroughBehavior,
171177
RequestTemplates: this.getIntegrationRequestTemplates(
172178
stateMachineName,
173179
stateMachineObj,
@@ -235,6 +241,12 @@ module.exports = {
235241
http.request.template
236242
);
237243
}
244+
245+
if (_.has(http, 'action')) {
246+
// no template if some action was defined.
247+
return {};
248+
}
249+
238250
// default template
239251
return defaultTemplate;
240252
},

0 commit comments

Comments
 (0)