-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
399 lines (363 loc) · 10.3 KB
/
template.yaml
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
SAM Template for serverless ordering API application
Globals:
Function:
Timeout: 3
Resources:
OrderTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: order-table
AttributeDefinitions:
- AttributeName: user_id
AttributeType: S
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: user_id
KeyType: HASH
- AttributeName: id
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
GetOrders:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/api/functions/get-orders/
Handler: app.getOrders
Runtime: nodejs18.x
Environment:
Variables:
ORDER_TABLE: !Ref OrderTable
Policies:
Statement:
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:Query
- dynamodb:Scan
Resource:
- !Sub
- "arn:aws:dynamodb:*:*:table/${Table}"
- { Table: !Ref OrderTable }
- Effect: Allow
Action:
- logs:*
Resource:
- "*"
GetSingleOrder:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/api/functions/get-single-order/
Handler: app.getOrderById
Runtime: nodejs18.x
Environment:
Variables:
ORDER_TABLE: !Ref OrderTable
Policies:
Statement:
- Effect: Allow
Action:
- dynamodb:GetItem
Resource:
- !Sub
- "arn:aws:dynamodb:*:*:table/${Table}"
- { Table: !Ref OrderTable }
- Effect: Allow
Action:
- logs:*
Resource:
- "*"
PostOrders:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/api/functions/post-orders/
Handler: app.postOrders
Runtime: nodejs18.x
Events:
SQSEventSource:
Type: SQS
Properties:
Queue: !GetAtt OrderQueue.Arn
BatchSize: 10
Environment:
Variables:
STATE_MACHINE_ARN: !Ref OrderManagementSFN
Policies:
Statement:
- Effect: Allow
Action:
- logs:*
Resource:
- "*"
- Effect: Allow
Action:
- states:StartExecution
Resource:
- !Sub
- "${StateMachine}"
- { StateMachine: !Ref OrderManagementSFN }
UpdateOrder:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/api/functions/update-order/
Handler: app.updateOrder
Runtime: nodejs18.x
Environment:
Variables:
ORDER_TABLE: !Ref OrderTable
Policies:
Statement:
- Effect: Allow
Action:
- dynamodb:UpdateItem
Resource:
- !Sub
- "arn:aws:dynamodb:*:*:table/${Table}"
- { Table: !Ref OrderTable }
- Effect: Allow
Action:
- logs:*
Resource:
- "*"
DeleteOrder:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/api/functions/delete-order/
Handler: app.deleteOrder
Runtime: nodejs18.x
Environment:
Variables:
ORDER_TABLE: !Ref OrderTable
Policies:
Statement:
- Effect: Allow
Action:
- dynamodb:DeleteItem
Resource:
- !Sub
- "arn:aws:dynamodb:*:*:table/${Table}"
- { Table: !Ref OrderTable }
- Effect: Allow
Action:
- logs:*
Resource:
- "*"
ProcessPaymentFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/manage/functions/process-payment/
Handler: app.processPayment
Runtime: nodejs18.x
SendOrderToRestaurantFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/manage/functions/send-order/
Handler: app.sendOrder
Runtime: nodejs18.x
ManageStateFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/manage/functions/manage-order-state/
Handler: app.manageState
Runtime: nodejs18.x
Environment:
Variables:
ORDER_TABLE: !Ref OrderTable
SNS_TOPIC_ARN: !Ref OrderTopic
Policies:
Statement:
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:Query
- dynamodb:Scan
- dynamodb:PutItem
- dynamodb:UpdateItem
Resource:
- !Sub
- "arn:aws:dynamodb:*:*:table/${Table}"
- { Table: !Ref OrderTable }
- Effect: Allow
Action:
- logs:*
Resource:
- "*"
- Effect: Allow
Action:
- SNS:Publish
Resource:
- "*"
OrderTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint: "test@email.com"
Protocol: "email"
TopicName: "order-topic"
OrderManagementSFN:
Type: AWS::Serverless::StateMachine
Properties:
DefinitionUri: src/manage/state-machine/processOrder.asl.json
DefinitionSubstitutions:
ManageStateArn: !GetAtt ManageStateFunction.Arn
ProcessPaymentArn: !GetAtt ProcessPaymentFunction.Arn
SendOrderToRestaurantArn: !GetAtt SendOrderToRestaurantFunction.Arn
Policies:
- LambdaInvokePolicy:
FunctionName: !Ref ManageStateFunction
- LambdaInvokePolicy:
FunctionName: !Ref ProcessPaymentFunction
- LambdaInvokePolicy:
FunctionName: !Ref SendOrderToRestaurantFunction
OrderApi:
Type: AWS::Serverless::Api
Properties:
StageName: Dev
Cors:
AllowMethods: "'POST, GET, UPDATE, DELETE'"
AllowHeaders: "'X-Forwarded-For'"
AllowOrigin: "'*'"
MaxAge: "'600'"
Auth:
DefaultAuthorizer: MyCognitoAuthorizer
Authorizers:
MyCognitoAuthorizer:
UserPoolArn: !GetAtt OrderCognitoPool.Arn
DefinitionBody:
"Fn::Transform":
Name: "AWS::Include"
Parameters:
Location: "./api.yaml"
ApiGwExecutionRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "apigateway.amazonaws.com"
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
ApiGwExecutionPolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName: "apigw-execution-policy"
PolicyDocument:
Version: "2012-10-17"
Statement:
Action:
- sqs:SendMessage
- lambda:*
Effect: Allow
Resource:
- !GetAtt GetOrders.Arn
- !GetAtt GetSingleOrder.Arn
- !GetAtt OrderQueue.Arn
- !GetAtt UpdateOrder.Arn
- !GetAtt DeleteOrder.Arn
Roles:
- Ref: "ApiGwExecutionRole"
OrderQueue:
Type: AWS::SQS::Queue
Properties:
RedrivePolicy:
deadLetterTargetArn: !GetAtt OrderDLQueue.Arn
maxReceiveCount: 5
OrderDLQueue:
Type: AWS::SQS::Queue
OrderCognitoPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: orders-pool
Policies:
PasswordPolicy:
MinimumLength: 8
UsernameAttributes:
- email
Schema:
- AttributeDataType: String
Name: email
Required: true
OrderCognitoPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
UserPoolId: !Ref OrderCognitoPool
ClientName: order-pool-client
ExplicitAuthFlows:
- ALLOW_ADMIN_USER_PASSWORD_AUTH
- ALLOW_REFRESH_TOKEN_AUTH
GenerateSecret: true
SupportedIdentityProviders:
- COGNITO
AllowedOAuthFlows:
- code
AllowedOAuthFlowsUserPoolClient: true
AllowedOAuthScopes:
- openid
- profile
- "order-api/delete_order"
CallbackURLs:
- "https://localhost/callback"
DependsOn: OrderCognitoResourceServer
OrderCognitoDomain:
Type: AWS::Cognito::UserPoolDomain
Properties:
Domain: !Sub "order-app-${AWS::AccountId}"
UserPoolId: !Ref OrderCognitoPool
OrderCognitoResourceServer:
Type: AWS::Cognito::UserPoolResourceServer
Properties:
Identifier: "order-api"
Name: "order-api"
Scopes:
- ScopeName: "delete_order"
ScopeDescription: "Delete Orders"
UserPoolId: !Ref OrderCognitoPool
Outputs:
DynamoDBTableName:
Description: "DynamoDB Table Name"
Value: !Ref OrderTable
GetOrders:
Description: "GetOrders Lambda Function ARN"
Value: !GetAtt GetOrders.Arn
GetSingleOrder:
Description: "GetSingleOrder Lambda Function ARN"
Value: !GetAtt GetSingleOrder.Arn
PostOrders:
Description: "PostOrders Lambda Function ARN"
Value: !GetAtt PostOrders.Arn
UpdateOrder:
Description: "UpdateOrder Lambda Function ARN"
Value: !GetAtt UpdateOrder.Arn
DeleteOrder:
Description: "DeleteOrder Lambda Function ARN"
Value: !GetAtt DeleteOrder.Arn
OrderApiEndpoint:
Description: "API Gateway endpoint URL for Dev stage for Order Get function"
Value: !Sub "https://${OrderApi}.execute-api.${AWS::Region}.amazonaws.com/Dev/orders/"
OrderQueueUrl:
Description: "URL of order queue"
Value: !Ref OrderQueue
OrderDLQueueUrl:
Description: "URL of order dead-letter queue"
Value: !Ref OrderDLQueue
CognitoClientID:
Description: "The client ID of the Cognito user pool"
Value: !Ref OrderCognitoPoolClient
CognitoUserPoolID:
Description: "ID of the Cognito userpool"
Value: !Ref OrderCognitoPool
CognitoDomain:
Description: "ID of the Cognito Domain"
Value: !Ref OrderCognitoDomain
CognitoResourceServer:
Description: "Cognito Resource Server"
Value: !Ref OrderCognitoResourceServer