-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstack.ts
165 lines (143 loc) · 5.16 KB
/
stack.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import * as cdk from 'aws-cdk-lib';
import { join } from "path";
export class Part05StepFunctionsStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const api = new cdk.aws_apigateway.RestApi(this, 'api', {
restApiName: 'Part05Service',
});
const storeDB = new cdk.aws_dynamodb.Table(this, 'storeDB', {
partitionKey: {
name: 'PK',
type: cdk.aws_dynamodb.AttributeType.STRING,
},
sortKey: {
name: 'SK',
type: cdk.aws_dynamodb.AttributeType.STRING,
},
billingMode: cdk.aws_dynamodb.BillingMode.PAY_PER_REQUEST,
});
const isItemInStock = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'isItemInStock', {
entry: join(__dirname, 'isItemInStock', 'handler.ts'),
handler: 'handler',
runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: ['@aws-sdk'],
},
environment: {
TABLE_NAME: storeDB.tableName,
},
});
storeDB.grantReadData(isItemInStock);
const updateItemStock = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'updateItemStock', {
entry: join(__dirname, 'updateItemStock', 'handler.ts'),
handler: 'handler',
runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: ['@aws-sdk'],
},
environment: {
TABLE_NAME: storeDB.tableName,
},
});
storeDB.grantWriteData(updateItemStock);
const createOrder = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'createOrder', {
entry: join(__dirname, 'createOrder', 'handler.ts'),
handler: 'handler',
runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: ['@aws-sdk'],
},
environment: {
TABLE_NAME: storeDB.tableName,
},
});
storeDB.grantWriteData(createOrder);
const isItemInStockMappedTask = new cdk.aws_stepfunctions.Map(this, 'isItemInStockMappedTask', {
itemsPath: '$.order',
resultPath: cdk.aws_stepfunctions.JsonPath.DISCARD,
parameters: {
'item.$': '$$.Map.Item.Value',
},
}).iterator(new cdk.aws_stepfunctions_tasks.LambdaInvoke(this, 'isItemInStockTask', {
lambdaFunction: isItemInStock,
}));
const updateItemStockMappedTask = new cdk.aws_stepfunctions.Map(this, 'updateItemStockMappedTask', {
itemsPath: '$.order',
parameters: {
'item.$': '$$.Map.Item.Value',
},
}).iterator(new cdk.aws_stepfunctions_tasks.LambdaInvoke(this, 'updateItemStockTask', {
lambdaFunction: updateItemStock,
}));
const createOrderTask = new cdk.aws_stepfunctions_tasks.LambdaInvoke(this, 'createOrderTask', {
lambdaFunction: createOrder,
});
const parallelState = new cdk.aws_stepfunctions.Parallel(this, 'parallelState', {});
parallelState.branch(updateItemStockMappedTask, createOrderTask);
const definition = isItemInStockMappedTask.next(
parallelState
);
const myFirstStateMachine = new cdk.aws_stepfunctions.StateMachine(this, 'myFirstStateMachine', {
definition,
});
const invokeStateMachineRole = new cdk.aws_iam.Role(this, 'invokeStateMachineRole', {
assumedBy: new cdk.aws_iam.ServicePrincipal('apigateway.amazonaws.com'),
});
invokeStateMachineRole.addToPolicy(
new cdk.aws_iam.PolicyStatement({
actions: ['states:StartExecution'],
resources: [myFirstStateMachine.stateMachineArn],
})
);
const createOrderResource = api.root.addResource('create-order');
createOrderResource.addMethod('POST', new cdk.aws_apigateway.Integration({
type: cdk.aws_apigateway.IntegrationType.AWS,
integrationHttpMethod: 'POST',
uri: `arn:aws:apigateway:${cdk.Aws.REGION}:states:action/StartExecution`,
options: {
credentialsRole: invokeStateMachineRole,
requestTemplates: {
'application/json': `{
"input": "{\\"order\\": $util.escapeJavaScript($input.json('$'))}",
"stateMachineArn": "${myFirstStateMachine.stateMachineArn}"
}`,
},
integrationResponses: [
{
statusCode: '200',
responseTemplates: {
'application/json': `{
"statusCode": 200,
"body": { "message": "OK!" }"
}`,
},
},
],
},
}),
{
methodResponses: [
{
statusCode: '200',
},
],
});
const createStoreItem = new cdk.aws_lambda_nodejs.NodejsFunction(this, 'createStoreItem', {
entry: join(__dirname, 'createStoreItem', 'handler.ts'),
handler: 'handler',
runtime: cdk.aws_lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: ['@aws-sdk'],
},
environment: {
TABLE_NAME: storeDB.tableName,
},
});
storeDB.grantReadWriteData(createStoreItem);
const createStoreItemResource = api.root.addResource('create-store-item');
createStoreItemResource.addMethod('POST', new cdk.aws_apigateway.LambdaIntegration(createStoreItem));
}
}