-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtemplate.yaml
More file actions
433 lines (414 loc) · 14.9 KB
/
Copy pathtemplate.yaml
File metadata and controls
433 lines (414 loc) · 14.9 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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Scanner - Lambda Durable Function triggered by S3 via EventBridge
Parameters:
AdminEmail:
Type: String
Description: Email address for the initial admin user
AllowedPattern: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
ConstraintDescription: Must be a valid email address
Globals:
Function:
Runtime: nodejs24.x
Architectures:
- arm64
Timeout: 900
MemorySize: 512
Tracing: Active
Api:
Cors:
AllowMethods: "'GET,POST,PUT,DELETE,OPTIONS'"
AllowHeaders: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
AllowOrigin: "'*'"
MaxAge: "'600'"
Resources:
# ============================================
# NESTED STACKS
# ============================================
AuthStack:
Type: AWS::Serverless::Application
Properties:
Location: stacks/auth.yaml
Parameters:
AdminEmail: !Ref AdminEmail
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
StorageStack:
Type: AWS::Serverless::Application
Properties:
Location: stacks/storage.yaml
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
ServicesStack:
Type: AWS::Serverless::Application
Properties:
Location: stacks/services.yaml
Parameters:
BucketArn: !GetAtt StorageStack.Outputs.BucketArn
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
SocketsStack:
Type: AWS::Serverless::Application
Properties:
Location: stacks/sockets.yaml
Parameters:
UserPoolId: !GetAtt AuthStack.Outputs.UserPoolId
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
# ============================================
# API GATEWAY
# ============================================
ScannerApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
Cors:
AllowMethods: "'GET,POST,PUT,DELETE,OPTIONS'"
AllowHeaders: "'Content-Type,Authorization'"
AllowOrigin: "'*'"
Auth:
DefaultAuthorizer: CognitoAuthorizer
AddDefaultAuthorizerToCorsPreflight: false
Authorizers:
CognitoAuthorizer:
UserPoolArn: !GetAtt AuthStack.Outputs.UserPoolArn
# ============================================
# LAMBDA FUNCTIONS
# ============================================
# Durable function that orchestrates video scanning workflow:
# - Triggered by S3 uploads to raw/ prefix
# - Runs Transcribe for audio transcription
# - Runs Rekognition for text detection in video
# - Analyzes content with Comprehend (toxicity, sentiment, PII)
# - Waits for human approval/rejection
# - Generates AI summary with Bedrock
# - Publishes real-time updates via AppSync
ScannerFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/scanner/
Handler: index.handler
Description: Scanner durable function triggered by S3 events
AutoPublishAlias: live
LoggingConfig:
LogFormat: JSON
ApplicationLogLevel: INFO
Environment:
Variables:
POWERTOOLS_SERVICE_NAME: scanner
POWERTOOLS_LOG_LEVEL: INFO
SCANNER_TABLE: !GetAtt StorageStack.Outputs.TableName
REKOGNITION_ROLE_ARN: !GetAtt ServicesStack.Outputs.RekognitionServiceRoleArn
REKOGNITION_SNS_TOPIC_ARN: !GetAtt ServicesStack.Outputs.RekognitionNotificationTopicArn
BEDROCK_MODEL_ID: global.amazon.nova-2-lite-v1:0
APPSYNC_EVENTS_API_URL: !Sub 'https://${SocketsStack.Outputs.AppSyncHttpEndpoint}/event'
DurableConfig:
ExecutionTimeout: 604800
RetentionPeriodInDays: 30
Policies:
- S3ReadPolicy:
BucketName: !GetAtt StorageStack.Outputs.BucketName
- S3WritePolicy:
BucketName: !GetAtt StorageStack.Outputs.BucketName
- DynamoDBCrudPolicy:
TableName: !GetAtt StorageStack.Outputs.TableName
- Statement:
# Transcribe - Scoped to transcription jobs in this account/region
- Effect: Allow
Action:
- transcribe:StartTranscriptionJob
- transcribe:GetTranscriptionJob
- transcribe:TagResource
Resource: !Sub 'arn:aws:transcribe:${AWS::Region}:${AWS::AccountId}:transcription-job/*'
# Comprehend - Resource-level permissions not supported by AWS
# Must use '*' as per AWS service limitations
- Effect: Allow
Action:
- comprehend:DetectToxicContent
- comprehend:DetectSentiment
- comprehend:DetectPiiEntities
Resource: '*'
# Rekognition - Resource-level permissions not supported by AWS
# Must use '*' as per AWS service limitations
- Effect: Allow
Action:
- rekognition:StartTextDetection
- rekognition:GetTextDetection
Resource: '*'
# IAM PassRole - Scoped to specific Rekognition service role
- Effect: Allow
Action:
- iam:PassRole
Resource: !GetAtt ServicesStack.Outputs.RekognitionServiceRoleArn
# Bedrock - Scoped to specific Nova model
# Need both inference profile and foundation model ARNs
# SDK may resolve inference profile to foundation model
- Effect: Allow
Action:
- bedrock:InvokeModel
Resource:
- arn:aws:bedrock:::foundation-model/amazon.nova-2-lite-v1:0
- arn:aws:bedrock:::inference-profile/global.amazon.nova-2-lite-v1:0
- arn:aws:bedrock:us-west-2:088483494489:inference-profile/global.amazon.nova-2-lite-v1:0
- arn:aws:bedrock:us-west-2::foundation-model/amazon.nova-2-lite-v1:0
# - !Sub 'arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:inference-profile/global.amazon.nova-2-lite-v1:0'
# - !Sub 'arn:aws:bedrock:${AWS::Region}::foundation-model/amazon.nova-2-lite-v1:0'
# AppSync - Scoped to specific API
- Effect: Allow
Action:
- appsync:EventPublish
Resource: !Sub 'arn:aws:appsync:${AWS::Region}:${AWS::AccountId}:apis/${SocketsStack.Outputs.AppSyncApiId}/*'
Events:
S3ObjectCreated:
Type: EventBridgeRule
Properties:
Pattern:
source:
- aws.s3
detail-type:
- Object Created
detail:
bucket:
name:
- !GetAtt StorageStack.Outputs.BucketName
object:
key:
- prefix: raw/
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: false
Target: es2022
Sourcemap: true
EntryPoints:
- index.ts
# Unified callback handler that receives completion events and sends them to durable executions:
# - Transcribe job completions (via EventBridge)
# - Rekognition job completions (via SNS)
# - Human approval/rejection decisions (via API Gateway)
# - Looks up callback tokens in DynamoDB and notifies the waiting durable function
CallbackFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/callback/
Handler: index.handler
Description: Unified callback handler for Transcribe, Rekognition, and Approval events
Environment:
Variables:
POWERTOOLS_SERVICE_NAME: callback
POWERTOOLS_LOG_LEVEL: INFO
SCANNER_TABLE: !GetAtt StorageStack.Outputs.TableName
Policies:
- DynamoDBCrudPolicy:
TableName: !GetAtt StorageStack.Outputs.TableName
- Statement:
- Effect: Allow
Action:
- lambda:SendDurableExecutionCallbackSuccess
- lambda:SendDurableExecutionCallbackFailure
Resource:
- !GetAtt ScannerFunction.Arn
- !Sub '${ScannerFunction.Arn}:*'
Events:
ApprovalApi:
Type: Api
Properties:
Path: /approval
Method: POST
RestApiId: !Ref ScannerApi
TranscribeComplete:
Type: EventBridgeRule
Properties:
Pattern:
source:
- aws.transcribe
detail-type:
- Transcribe Job State Change
detail:
TranscriptionJobStatus:
- COMPLETED
- FAILED
RekognitionSNS:
Type: SNS
Properties:
Topic: !GetAtt ServicesStack.Outputs.RekognitionNotificationTopicArn
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: false
Target: es2022
Sourcemap: true
EntryPoints:
- index.ts
# API handler for all scan-related operations:
# - GET /scans - List user's scans
# - GET /scans/{scanId} - Get single scan details
# - POST /scans/upload - Generate presigned URL for video upload
# - POST /scans/{scanId}/video-url - Generate presigned URL to view video
# - GET /admin/scans/pending - List scans awaiting approval (admin only)
ApiScansFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/api-scans/
Handler: index.handler
Description: Consolidated API for all scan-related operations
Environment:
Variables:
POWERTOOLS_SERVICE_NAME: api-scans
POWERTOOLS_LOG_LEVEL: INFO
TABLE_NAME: !GetAtt StorageStack.Outputs.TableName
BUCKET_NAME: !GetAtt StorageStack.Outputs.BucketName
Policies:
- DynamoDBCrudPolicy:
TableName: !GetAtt StorageStack.Outputs.TableName
- S3ReadPolicy:
BucketName: !GetAtt StorageStack.Outputs.BucketName
- S3WritePolicy:
BucketName: !GetAtt StorageStack.Outputs.BucketName
Events:
# List user's scans
ListScans:
Type: Api
Properties:
RestApiId: !Ref ScannerApi
Path: /scans
Method: GET
# Get single scan
GetScan:
Type: Api
Properties:
RestApiId: !Ref ScannerApi
Path: /scans/{scanId}
Method: GET
# List pending reviews (admin)
ListPending:
Type: Api
Properties:
RestApiId: !Ref ScannerApi
Path: /admin/scans/pending
Method: GET
# Generate upload presigned URL
Upload:
Type: Api
Properties:
RestApiId: !Ref ScannerApi
Path: /scans/upload
Method: POST
# Generate video presigned URL
VideoUrl:
Type: Api
Properties:
RestApiId: !Ref ScannerApi
Path: /scans/{scanId}/video-url
Method: POST
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: false
Target: es2022
Sourcemap: true
EntryPoints:
- index.ts
# API handler for user profile and admin user management:
# - GET /profile - Get current user's profile
# - PUT /profile - Update current user's profile
# - POST /admin/users/invite - Invite new user (admin only)
# - GET /admin/users - List all users (admin only)
# - DELETE /admin/users/{username} - Delete user (admin only)
# - Publishes user events to AppSync for real-time updates
ApiUsersFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/api-users/
Handler: index.handler
Description: Consolidated API for user profile and admin user management
Environment:
Variables:
POWERTOOLS_SERVICE_NAME: api-users
POWERTOOLS_LOG_LEVEL: INFO
TABLE_NAME: !GetAtt StorageStack.Outputs.TableName
USER_POOL_ID: !GetAtt AuthStack.Outputs.UserPoolId
APPSYNC_EVENTS_API_URL: !Sub 'https://${SocketsStack.Outputs.AppSyncHttpEndpoint}/event'
Policies:
- DynamoDBCrudPolicy:
TableName: !GetAtt StorageStack.Outputs.TableName
- Statement:
- Effect: Allow
Action:
- cognito-idp:AdminCreateUser
- cognito-idp:ListUsers
- cognito-idp:AdminDeleteUser
Resource: !GetAtt AuthStack.Outputs.UserPoolArn
- Effect: Allow
Action:
- appsync:EventPublish
Resource: !Sub 'arn:aws:appsync:${AWS::Region}:${AWS::AccountId}:apis/${SocketsStack.Outputs.AppSyncApiId}/*'
Events:
# User profile endpoints
GetProfile:
Type: Api
Properties:
RestApiId: !Ref ScannerApi
Path: /profile
Method: GET
UpdateProfile:
Type: Api
Properties:
RestApiId: !Ref ScannerApi
Path: /profile
Method: PUT
# Admin user management endpoints
InviteUser:
Type: Api
Properties:
RestApiId: !Ref ScannerApi
Path: /admin/users/invite
Method: POST
ListUsers:
Type: Api
Properties:
RestApiId: !Ref ScannerApi
Path: /admin/users
Method: GET
DeleteUser:
Type: Api
Properties:
RestApiId: !Ref ScannerApi
Path: /admin/users/{username}
Method: DELETE
Metadata:
BuildMethod: esbuild
BuildProperties:
Minify: false
Target: es2022
Sourcemap: true
EntryPoints:
- index.ts
Outputs:
ApiEndpoint:
Description: NUXT_PUBLIC_API_ENDPOINT
Value: !Sub 'https://${ScannerApi}.execute-api.${AWS::Region}.amazonaws.com/prod'
UserPoolId:
Description: NUXT_PUBLIC_USER_POOL_ID
Value: !GetAtt AuthStack.Outputs.UserPoolId
UserPoolClientId:
Description: NUXT_PUBLIC_USER_POOL_CLIENT_ID
Value: !GetAtt AuthStack.Outputs.UserPoolClientId
AppSyncHttpEndpoint:
Description: NUXT_PUBLIC_APPSYNC_HTTP_ENDPOINT
Value: !Sub 'https://${SocketsStack.Outputs.AppSyncHttpEndpoint}/event'
AppSyncRealtimeEndpoint:
Description: NUXT_PUBLIC_APPSYNC_REALTIME_ENDPOINT
Value: !Sub 'wss://${SocketsStack.Outputs.AppSyncRealtimeEndpoint}/event/realtime'
Region:
Description: NUXT_PUBLIC_REGION
Value: !Ref AWS::Region
FrontendEnvFile:
Description: Copy this to frontend/.env
Value: !Sub |
NUXT_PUBLIC_API_ENDPOINT=https://${ScannerApi}.execute-api.${AWS::Region}.amazonaws.com/prod
NUXT_PUBLIC_USER_POOL_ID=${AuthStack.Outputs.UserPoolId}
NUXT_PUBLIC_USER_POOL_CLIENT_ID=${AuthStack.Outputs.UserPoolClientId}
NUXT_PUBLIC_APPSYNC_HTTP_ENDPOINT=https://${SocketsStack.Outputs.AppSyncHttpEndpoint}/event
NUXT_PUBLIC_APPSYNC_REALTIME_ENDPOINT=wss://${SocketsStack.Outputs.AppSyncRealtimeEndpoint}/event/realtime
NUXT_PUBLIC_REGION=${AWS::Region}