forked from mjzone/sfn-crash-course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverless.yml
More file actions
156 lines (152 loc) · 4.84 KB
/
serverless.yml
File metadata and controls
156 lines (152 loc) · 4.84 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
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
service: stepfn-course
provider:
name: aws
runtime: nodejs12.x
iamRoleStatements:
- Effect: Allow
Action: dynamodb:*
Resource: arn:aws:dynamodb:us-east-1:<accountId>:table/bookTable
- Effect: Allow
Action: dynamodb:*
Resource: arn:aws:dynamodb:us-east-1:<accountId>:table/userTable
- Effect: Allow
Action: sns:*
Resource: arn:aws:sns:us-east-1:<accountId>:NotifyCourier
- Effect: Allow
Action: sqs:*
Resource: arn:aws:sqs:us-east-1:<accountId>:OrdersQueue
- Effect: Allow
Action: states:*
Resource: arn:aws:states:us-east-1:<accountId>:stateMachine:storeCheckoutFlow
plugins:
- serverless-step-functions
functions:
checkInventory:
handler: handler.checkInventory
calculateTotal:
handler: handler.calculateTotal
redeemPoints:
handler: handler.redeemPoints
billCustomer:
handler: handler.billCustomer
restoreRedeemPoints:
handler: handler.restoreRedeemPoints
restoreQuantity:
handler: handler.restoreQuantity
sqsWorker:
handler: handler.sqsWorker
events:
- sqs:
arn: arn:aws:sqs:us-east-1:<accountId>:OrdersQueue
batchSize: 1
stepFunctions:
stateMachines:
storeCheckoutFlow:
name: storeCheckoutFlow
definition:
StartAt: checkInventory
States:
checkInventory:
Type: Task
Resource:
Fn::GetAtt: [checkInventory, Arn]
Catch:
- ErrorEquals: [BookNotFound]
Next: BookNotFoundError
- ErrorEquals: [BookOutOfStock]
Next: BookOutOfStockError
ResultPath: "$.book"
Next: calculateTotal
calculateTotal:
Type: Task
Resource:
Fn::GetAtt: [calculateTotal, Arn]
ResultPath: "$.total"
Next: isRedeemNeeded
isRedeemNeeded:
Type: Choice
Choices:
- Variable: $.redeem
BooleanEquals: true
Next: RedeemPoints
Default: BillCustomer
RedeemPoints:
Type: Task
Resource:
Fn::GetAtt: [redeemPoints, Arn]
ResultPath: "$.total"
Catch:
- ErrorEquals: [States.ALL]
Next: RedeemPointsError
Next: BillCustomer
BillCustomer:
Type: Task
Resource:
Fn::GetAtt: [billCustomer, Arn]
ResultPath: "$.billingStatus"
Retry:
- ErrorEquals: [States.ALL]
MaxAttempts: 0
Catch:
- ErrorEquals: [States.ALL]
ResultPath: "$.customerBilling"
Next: BillingError
Next: PrepareOrder
PrepareOrder: #Add to the queue, lambda worker, will allocate the quirier by calling a 3rd party api and reduce the amount from the item
Type: Task
Resource: arn:aws:states:::sqs:sendMessage.waitForTaskToken
Parameters:
QueueUrl: https://sqs.us-east-1.amazonaws.com/<accountId>/OrdersQueue
MessageBody:
Input.$: "$"
Token.$: "$$.Task.Token" #From the context
ResultPath: "$.courierStatus"
Catch:
- ErrorEquals: [NoCourierAvailable]
ResultPath: "$.courierError"
Next: RefundCustomer
Next: DispatchOrder
DispatchOrder:
Type: Task
Resource: arn:aws:states:::sns:publish
Parameters:
TopicArn: arn:aws:sns:us-east-1:<accountId>:NotifyCourier
Message.$: "$"
Next: Dispatched
Dispatched:
Type: Pass
Result: "Your order will be dispatched in 24 hours"
End: true
RestoreRedeemPoints:
Type: Task
Resource:
Fn::GetAtt: [restoreRedeemPoints, Arn]
End: true
RestoreQuantity:
Type: Task
Resource:
Fn::GetAtt: [restoreQuantity, Arn]
ResultPath: "$.quantityRestoreStatus"
Next: RestoreRedeemPoints
RefundCustomer:
Type: Pass
Result: "Customer is refunded"
ResultPath: "$.refundStatus"
Next: RestoreQuantity
BookNotFoundError:
Type: Pass
Result: "No such book available"
End: true
BookOutOfStockError:
Type: Pass
Result: "Sorry, the books is out of stock"
End: true
RedeemPointsError:
Type: Pass
Result: "Error in redeeming points"
End: true
BillingError:
Type: Pass
Result: "Billing error"
ResultPath: "$.billingStatus"
Next: RestoreRedeemPoints