-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStateMachineEndpoint.ts
More file actions
75 lines (69 loc) · 2.86 KB
/
StateMachineEndpoint.ts
File metadata and controls
75 lines (69 loc) · 2.86 KB
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
import {IResource, PassthroughBehavior, StepFunctionsIntegration} from "aws-cdk-lib/aws-apigateway"
import {IRole} from "aws-cdk-lib/aws-iam"
import {HttpMethod} from "aws-cdk-lib/aws-lambda"
import {Construct} from "constructs"
import {stateMachineRequestTemplate} from "./templates/stateMachineRequest.js"
import {stateMachine200ResponseTemplate, stateMachineErrorResponseTemplate} from "./templates/stateMachineResponses.js"
import {ExpressStateMachine} from "../StateMachine.js"
/** Parameters used to create an API endpoint backed by a Step Functions Express workflow. */
export interface StateMachineEndpointProps {
/** Parent API resource under which the state machine endpoint is added. */
parentResource: IResource
/** Path segment used to create the child API resource. */
readonly resourceName: string
/** HTTP verb bound to the Step Functions integration. */
readonly method: HttpMethod
/** Invocation role used by API Gateway when starting workflow executions. */
restApiGatewayRole: IRole
/** State machine wrapper construct providing the target workflow ARN and integration target. */
stateMachine: ExpressStateMachine
}
/** Adds an API Gateway resource/method that starts an Express Step Functions execution. */
export class StateMachineEndpoint extends Construct {
/** API resource created by this construct. */
resource: IResource
/** Wires request and response mapping templates for JSON and FHIR payload flows. */
public constructor(scope: Construct, id: string, props: StateMachineEndpointProps) {
super(scope, id)
const requestTemplate = stateMachineRequestTemplate(props.stateMachine.stateMachine.stateMachineArn)
const resource = props.parentResource.addResource(props.resourceName)
resource.addMethod(props.method, StepFunctionsIntegration.startExecution(props.stateMachine.stateMachine, {
credentialsRole: props.restApiGatewayRole,
passthroughBehavior: PassthroughBehavior.WHEN_NO_MATCH,
requestTemplates: {
"application/json": requestTemplate,
"application/fhir+json": requestTemplate
},
integrationResponses: [
{
statusCode: "200",
responseTemplates: {
"application/json": stateMachine200ResponseTemplate
}
},
{
statusCode: "400",
selectionPattern: String.raw`^4\d{2}.*`,
responseTemplates: {
"application/json": stateMachineErrorResponseTemplate("400")
}
},
{
statusCode: "500",
selectionPattern: String.raw`^5\d{2}.*`,
responseTemplates: {
"application/json": stateMachineErrorResponseTemplate("500")
}
}
]
}), {
methodResponses: [
{statusCode: "200"},
{statusCode: "400"},
{statusCode: "408"},
{statusCode: "500"}
]
})
this.resource = resource
}
}