-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.yml
299 lines (279 loc) · 8.67 KB
/
serverless.yml
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
service: carts-service
frameworkVersion: "2"
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: 20201221
region: eu-west-1
stage: ${opt:stage, "dev"}
apiGateway:
shouldStartNameWithService: true
iam:
role:
statements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
- dynamodb:BatchWriteItem
Resource: '*'
environment:
CARTS_TABLE: carts-table-${self:provider.stage}
PRODUCT_DELETED_QUEUE: product-deleted-queue-${self:provider.stage}.fifo
PRODUCT_DELETED_DEAD: dead-product-deleted-queue-${self:provider.stage}.fifo
PRODUCT_EDITED_QUEUE: product-edited-queue-${self:provider.stage}.fifo
PRODUCT_EDITED_DEAD: dead-product-edited-queue-${self:provider.stage}.fifo
PAYED_CART_QUEUE: payed-cart-queue-${self:provider.stage}.fifo
PAYED_CART_DEAD: dead-payed-cart-queue-${self:provider.stage}.fifo
DYNAMODB_CONFIG_FILE_PATH: configs/${self:provider.stage}-dynamo.yml
package:
individually: true
plugins:
- serverless-webpack
- serverless-dynamodb-local
- serverless-dynamodb-seed
- serverless-offline
functions:
addProductToCart:
handler: src/endpoints/addProduct.default
description: New content of the buyer's cart
events:
- http:
path: cart
method: POST
cors: true
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
getCartToken:
handler: src/endpoints/getCartToken.default
description: Obtain information about a user personal cart
events:
- http:
path: cart
method: GET
cors: true
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
updateProductQuantity:
handler: src/endpoints/updateProductQuantity.default
description: Update the product quantity
events:
- http:
path: cart/{productId}
method: PATCH
cors: true
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
deleteProductFromCart:
handler: src/endpoints/deleteProduct.default
description: Delete the rpoduct from the cart
events:
- http:
path: cart/{productId}
method: DELETE
cors: true
authorizer:
type: COGNITO_USER_POOLS
authorizerId:
Ref: ApiGatewayAuthorizer
emptyCart:
handler: src/endpoints/emptyCart.default
description: Empty the cart after successful payment
events:
- sqs:
arn:
!GetAtt 'PayedCartQueue.Arn'
removeDeletedProduct:
handler: src/endpoints/removeDeletedProduct.default
description: Delete all the products deleted from the vendor
events:
- sqs:
arn:
!GetAtt 'ProductDeletedQueue.Arn'
updateEditedProduct:
handler: src/endpoints/updateEditedProduct.default
description: Update all the products edited from the vendor
events:
- sqs:
arn:
!GetAtt 'ProductEditedQueue.Arn'
custom:
webpack:
webpackConfig: ./webpack.config.js
includeModules: true
dynamodb:
stages:
- local
start:
port: 8000
inMemory: true
migrate: true
seed: true
convertEmptyValues: true
migration:
dir: ./offline
seed:
cart:
sources:
- table: ${self:provider.environment.CARTS_TABLE}
sources: [./offline/seeds/carts.json]
resources:
Resources:
ProductEditedQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:provider.environment.PRODUCT_EDITED_QUEUE}
FifoQueue: true
DelaySeconds: 0
VisibilityTimeout: 30
RedrivePolicy:
maxReceiveCount: 1
deadLetterTargetArn:
!GetAtt 'DeadEditedProduct.Arn'
DeadEditedProduct:
Type: "AWS::SQS::Queue"
Properties:
QueueName: ${self:provider.environment.PRODUCT_EDITED_DEAD}
FifoQueue: true
MessageRetentionPeriod: 604800 #7 days, should be enough time to fix an issue and retry the messages again in case of a critical issue
SqsEditedProductPolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- Ref: ProductEditedQueue
PolicyDocument:
Statement:
- Effect: Allow
Principal: '*'
Action:
- sqs:SendMessage
Resource:
!GetAtt 'ProductEditedQueue.Arn'
Condition:
ArnEquals:
'aws:SourceArn': arn:aws:sns:eu-west-1:501082649462:ProductEdited.fifo
ProductEditedTopicSubscription:
Type: AWS::SNS::Subscription
Properties:
Endpoint:
!GetAtt 'ProductEditedQueue.Arn'
Protocol: "sqs"
TopicArn: arn:aws:sns:eu-west-1:501082649462:ProductEdited.fifo
PayedCartQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:provider.environment.PAYED_CART_QUEUE}
FifoQueue: true
DelaySeconds: 0
VisibilityTimeout: 30
RedrivePolicy:
maxReceiveCount: 1
deadLetterTargetArn:
!GetAtt 'DeadPayedCart.Arn'
DeadPayedCart:
Type: "AWS::SQS::Queue"
Properties:
QueueName: ${self:provider.environment.PAYED_CART_DEAD}
FifoQueue: true
MessageRetentionPeriod: 604800 #7 days, should be enough time to fix an issue and retry the messages again in case of a critical issue
SqsPayedCartPolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- Ref: PayedCartQueue
PolicyDocument:
Statement:
- Effect: Allow
Principal: '*'
Action:
- sqs:SendMessage
Resource:
!GetAtt 'PayedCartQueue.Arn'
Condition:
ArnEquals:
'aws:SourceArn': arn:aws:sns:eu-west-1:501082649462:PayedCart.fifo
PayedCartTopicSubscription:
Type: AWS::SNS::Subscription
Properties:
Endpoint:
!GetAtt 'PayedCartQueue.Arn'
Protocol: "sqs"
TopicArn: arn:aws:sns:eu-west-1:501082649462:PayedCart.fifo
ProductDeletedQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${self:provider.environment.PRODUCT_DELETED_QUEUE}
FifoQueue: true
DelaySeconds: 0
VisibilityTimeout: 30
RedrivePolicy:
maxReceiveCount: 1
deadLetterTargetArn:
!GetAtt 'DeadDeletedProduct.Arn'
DeadDeletedProduct:
Type: "AWS::SQS::Queue"
Properties:
QueueName: ${self:provider.environment.PRODUCT_DELETED_DEAD}
FifoQueue: true
MessageRetentionPeriod: 604800 #7 days, should be enough time to fix an issue and retry the messages again in case of a critical issue
SqsDeletedProductPolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- Ref: ProductDeletedQueue
PolicyDocument:
Statement:
- Effect: Allow
Principal: '*'
Action:
- sqs:SendMessage
Resource:
!GetAtt 'ProductDeletedQueue.Arn'
Condition:
ArnEquals:
'aws:SourceArn': arn:aws:sns:eu-west-1:501082649462:ProductDeleted.fifo
ProductDeletedTopicSubscription:
Type: AWS::SNS::Subscription
Properties:
Endpoint:
!GetAtt 'ProductDeletedQueue.Arn'
Protocol: "sqs"
TopicArn: arn:aws:sns:eu-west-1:501082649462:ProductDeleted.fifo
CartsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:provider.environment.CARTS_TABLE}
AttributeDefinitions:
- AttributeName: cartId
AttributeType: S
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: cartId
KeyType: HASH
- AttributeName: id
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
ApiGatewayAuthorizer:
Type: AWS::ApiGateway::Authorizer
Properties:
AuthorizerResultTtlInSeconds: 300
IdentitySource: method.request.header.Authorization
Name: cognito-${self:provider.stage}
RestApiId:
Ref: ApiGatewayRestApi
Type: COGNITO_USER_POOLS
ProviderARNs:
- !ImportValue ${self:provider.stage}-user-pool-arn