/
stepfunctions-api.ts
135 lines (122 loc) · 3.77 KB
/
stepfunctions-api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { Construct } from 'constructs';
import { RestApi, RestApiProps } from '.';
import { RequestContext } from './integrations';
import { StepFunctionsIntegration } from './integrations/stepfunctions';
import * as iam from '../../aws-iam';
import * as sfn from '../../aws-stepfunctions';
/**
* Properties for StepFunctionsRestApi
*
*/
export interface StepFunctionsRestApiProps extends RestApiProps {
/**
* The default State Machine that handles all requests from this API.
*
* This stateMachine will be used as a the default integration for all methods in
* this API, unless specified otherwise in `addMethod`.
*/
readonly stateMachine: sfn.IStateMachine;
/**
* Which details of the incoming request must be passed onto the underlying state machine,
* such as, account id, user identity, request id, etc. The execution input will include a new key `requestContext`:
*
* {
* "body": {},
* "requestContext": {
* "key": "value"
* }
* }
*
* @default - all parameters within request context will be set as false
*/
readonly requestContext?: RequestContext;
/**
* Check if querystring is to be included inside the execution input. The execution input will include a new key `queryString`:
*
* {
* "body": {},
* "querystring": {
* "key": "value"
* }
* }
*
* @default true
*/
readonly querystring?: boolean;
/**
* Check if path is to be included inside the execution input. The execution input will include a new key `path`:
*
* {
* "body": {},
* "path": {
* "resourceName": "resourceValue"
* }
* }
*
* @default true
*/
readonly path?: boolean;
/**
* Check if header is to be included inside the execution input. The execution input will include a new key `headers`:
*
* {
* "body": {},
* "headers": {
* "header1": "value",
* "header2": "value"
* }
* }
* @default false
*/
readonly headers?: boolean;
/**
* If the whole authorizer object, including custom context values should be in the execution input. The execution input will include a new key `authorizer`:
*
* {
* "body": {},
* "authorizer": {
* "key": "value"
* }
* }
*
* @default false
*/
readonly authorizer?: boolean;
/**
* An IAM role that API Gateway will assume to start the execution of the
* state machine.
*
* @default - a new role is created
*/
readonly role?: iam.IRole;
/**
* Whether to add default response models with 200, 400, and 500 status codes to the method.
*
* @default true
*/
readonly useDefaultMethodResponses?: boolean;
}
/**
* Defines an API Gateway REST API with a Synchrounous Express State Machine as a proxy integration.
*/
export class StepFunctionsRestApi extends RestApi {
constructor(scope: Construct, id: string, props: StepFunctionsRestApiProps) {
if (props.defaultIntegration) {
throw new Error('Cannot specify "defaultIntegration" since Step Functions integration is automatically defined');
}
if ((props.stateMachine.node.defaultChild as sfn.CfnStateMachine).stateMachineType !== sfn.StateMachineType.EXPRESS) {
throw new Error('State Machine must be of type "EXPRESS". Please use StateMachineType.EXPRESS as the stateMachineType');
}
const stepfunctionsIntegration = StepFunctionsIntegration.startExecution(props.stateMachine, {
credentialsRole: props.role,
requestContext: props.requestContext,
path: props.path?? true,
querystring: props.querystring?? true,
headers: props.headers,
authorizer: props.authorizer,
useDefaultMethodResponses: props.useDefaultMethodResponses,
});
super(scope, id, props);
this.root.addMethod('ANY', stepfunctionsIntegration);
}
}