forked from aws-samples/aiml-genai-multimodal-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudformation.yaml
861 lines (806 loc) · 28.5 KB
/
cloudformation.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
TEXTEXTRACTOUTPUTPREFIX:
Type: String
Default: pdfoutputs
Description: Enter the Text Extract Output Prefix
SourceCodeBucket:
Type: String
Default: fsi-multimodal-code
Description: Enter the name of the Text Extract Source Bucket
SourceCodeprefix:
Type: String
Default: code/
Description: Enter the name of the Text Extract Source Bucket
AUDIOTRANSCRIPTSDESTBUCKETPREFIX:
Type: String
Default: audiooutputs/
Description: Enter the name of the Text Extract Source Bucket
Resources:
AthenaQueryResultBucket:
Type: AWS::S3::Bucket
Properties:
PublicAccessBlockConfiguration:
BlockPublicAcls: true
IgnorePublicAcls: true
BlockPublicPolicy: true
RestrictPublicBuckets: true
LambdaZipsBucket:
Type: AWS::S3::Bucket
Properties:
PublicAccessBlockConfiguration:
BlockPublicAcls: true
IgnorePublicAcls: true
BlockPublicPolicy: true
RestrictPublicBuckets: true
QueryStagingBucket:
Type: 'AWS::S3::Bucket'
Properties:
PublicAccessBlockConfiguration:
BlockPublicAcls: true
IgnorePublicAcls: true
BlockPublicPolicy: true
RestrictPublicBuckets: true
MultimodalOutputBucketResource:
Type: 'AWS::S3::Bucket'
Properties:
PublicAccessBlockConfiguration:
BlockPublicAcls: true
IgnorePublicAcls: true
BlockPublicPolicy: true
RestrictPublicBuckets: true
TextractArchiveBucketResource:
Type: 'AWS::S3::Bucket'
Properties:
PublicAccessBlockConfiguration:
BlockPublicAcls: true
IgnorePublicAcls: true
BlockPublicPolicy: true
RestrictPublicBuckets: true
TextractRawOutputBucketResource:
Type: 'AWS::S3::Bucket'
Properties:
PublicAccessBlockConfiguration:
BlockPublicAcls: true
IgnorePublicAcls: true
BlockPublicPolicy: true
RestrictPublicBuckets: true
TextractSourceBucketResource:
Type: 'AWS::S3::Bucket'
Properties:
PublicAccessBlockConfiguration:
BlockPublicAcls: true
IgnorePublicAcls: true
BlockPublicPolicy: true
RestrictPublicBuckets: true
AudioTranscriptsSourceBucketResource:
Type: 'AWS::S3::Bucket'
Properties:
PublicAccessBlockConfiguration:
BlockPublicAcls: true
IgnorePublicAcls: true
BlockPublicPolicy: true
RestrictPublicBuckets: true
StockDataSourceBucket:
Type: 'AWS::S3::Bucket'
Properties:
PublicAccessBlockConfiguration:
BlockPublicAcls: true
IgnorePublicAcls: true
BlockPublicPolicy: true
RestrictPublicBuckets: true
AthenaWorkGroup:
Type: AWS::Athena::WorkGroup
Properties:
Name: fsi-workgroup
# Add additional properties if needed, such as State, Description, etc.
WorkGroupConfiguration:
ResultConfiguration:
OutputLocation: !Sub "s3://${AthenaQueryResultBucket}/query-results/"
MultiModalSagemakerNotebookInstance:
Type: AWS::SageMaker::NotebookInstance
Properties:
NotebookInstanceName: MultiModalSagemakerInstance
InstanceType: ml.t2.medium
RoleArn: !GetAtt SageMakerNotebookRole.Arn
AthenaPermissionsPolicy:
Type: 'AWS::IAM::ManagedPolicy'
Properties:
ManagedPolicyName: AthenaPermissionsPolicy
Description: Custom policy for Athena permissions
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: VisualEditor0
Effect: Allow
Action:
- athena:GetTableMetadata
Resource:
- !Sub "arn:aws:athena:${AWS::Region}:${AWS::AccountId}:workgroup/primary"
S3PermissionsPolicy:
Type: 'AWS::IAM::ManagedPolicy'
Properties:
ManagedPolicyName: S3PermissionsPolicy
Description: Custom policy for S3 permissions
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: VisualEditor0
Effect: Allow
Action:
- s3:PutObject
- s3:GetObject
- s3:AbortMultipartUpload
- s3:GetBucketLocation
Resource:
- Fn::Sub: "arn:aws:s3:::${StockDataSourceBucket}/*"
- Fn::Sub: "arn:aws:s3:::${QueryStagingBucket}/*"
- Fn::Sub: "arn:aws:s3:::${StockDataSourceBucket}/*"
- Fn::Sub: "arn:aws:s3:::${AudioTranscriptsSourceBucketResource}/*"
- Fn::Sub: "arn:aws:s3:::${TextractSourceBucketResource}/*"
- Fn::Sub: "arn:aws:s3:::${TextractArchiveBucketResource}/*"
- Fn::Sub: "arn:aws:s3:::${MultimodalOutputBucketResource}/*"
- Fn::Sub: "arn:aws:s3:::${StockDataSourceBucket}"
- Fn::Sub: "arn:aws:s3:::${QueryStagingBucket}"
- Fn::Sub: "arn:aws:s3:::${StockDataSourceBucket}/*"
- Fn::Sub: "arn:aws:s3:::${AudioTranscriptsSourceBucketResource}"
- Fn::Sub: "arn:aws:s3:::${TextractSourceBucketResource}"
- Fn::Sub: "arn:aws:s3:::${TextractArchiveBucketResource}"
- Fn::Sub: "arn:aws:s3:::${MultimodalOutputBucketResource}"
SageMakerNotebookRole:
Type: AWS::IAM::Role
Properties:
RoleName: SageMakerRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: sagemaker.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonSageMakerFullAccess
- arn:aws:iam::aws:policy/service-role/AWSLambdaRole
- arn:aws:iam::aws:policy/SecretsManagerReadWrite
- arn:aws:iam::aws:policy/AWSCloudFormationReadOnlyAccess
- arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole
- arn:aws:iam::aws:policy/AmazonAthenaFullAccess
- !Ref FSIS3AccessPolicy
- !Ref DynamoDBChatHistoryPolicy
- !Ref KendraPolicy
KendraPolicy:
Type: "AWS::IAM::ManagedPolicy"
Properties:
ManagedPolicyName: KendraPolicy
Description: Managed policy for Kendra access
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: KendraPermissions
Effect: Allow
Action:
- kendra:*
Resource:
- !GetAtt KendraDataSource.Arn
- !GetAtt KendraIndex.Arn
FSITextract:
DependsOn: CopyZips
Type: 'AWS::Lambda::Function'
Properties:
FunctionName: FSI-TextractProcessingFunction
Runtime: python3.10
Timeout: 900
Handler: lambda_function.lambda_handler
Role: !GetAtt FSITextractRole.Arn
Code:
S3Bucket: !Ref 'LambdaZipsBucket'
S3Key: !Sub '${SourceCodeprefix}FSI-TextractProcessingFunction-b6054c5e-0286-4cfb-a2f3-55d408a9972c.zip'
Environment:
Variables:
MULTIMODAL_OUTPUT_BUCKET: !Ref MultimodalOutputBucketResource
TEXTEXTRACT_ARCHIVE_BUCKET: !Ref TextractArchiveBucketResource
TEXTEXTRACT_OUTPUT_PREFIX: !Ref TEXTEXTRACTOUTPUTPREFIX
TEXTEXTRACT_RAW_OUTPUT_BUCKET: !Ref TextractRawOutputBucketResource
TEXTEXTRACT_ROLE_ARN: !GetAtt TextExractRole.Arn
TEXTEXTRACT_SOURCE_BUCKET: !Ref TextractSourceBucketResource
FSISentimentDetecttion:
DependsOn: CopyZips
Type: 'AWS::Lambda::Function'
Properties:
FunctionName: FSI-SentimentDetecttion
Runtime: python3.10
Timeout: 600
Handler: lambda_function.lambda_handler
Role: !GetAtt FSISentimentDetecttionRole.Arn
Code:
S3Bucket: !Ref 'LambdaZipsBucket'
S3Key: !Sub '${SourceCodeprefix}FSI-SentimentDetecttion-82a6bf78-2382-479e-b112-5d96d99561ef.zip'
FSISentimentDetecttionRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: FSI-SentimentDetecttion-Role
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: 'sts:AssumeRole'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/ComprehendReadOnly
FSIKeyPhrasesDetection:
DependsOn: CopyZips
Type: 'AWS::Lambda::Function'
Properties:
FunctionName: FSI-KeyPhrasesDetection
Runtime: python3.10
Timeout: 600
Handler: lambda_function.lambda_handler
Role: !GetAtt FSIKeyPhrasesDetectionRole.Arn
Code:
S3Bucket: !Ref 'LambdaZipsBucket'
S3Key: !Sub '${SourceCodeprefix}FSI-KeyPhrasesDetection-6b3c773e-4dbc-4930-b569-0f4ad3cfb9c9.zip'
Environment:
Variables:
AUDIOTRANSCRIPTS_SOURCE_BUCKET : !Ref MultimodalOutputBucketResource
AUDIOTRANSCRIPTS_SOURCE_KEY : 'q1-2023-earnings-call.mp3.txt'
FSIKeyPhrasesDetectionRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: FSI-KeyPhrasesDetection-Role
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: 'sts:AssumeRole'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AWSLambdaExecute
- arn:aws:iam::aws:policy/ComprehendReadOnly
- arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
FSITranscribe:
DependsOn: CopyZips
Type: 'AWS::Lambda::Function'
Properties:
FunctionName: FSI-Transcribe
Runtime: python3.10
Timeout: 600
Handler: lambda_function.lambda_handler
Role: !GetAtt FSITranscribeRole.Arn
Code:
S3Bucket: !Ref 'LambdaZipsBucket'
S3Key: !Sub '${SourceCodeprefix}FSI-Transcribe-df01dca6-8e3c-46cc-b9a9-aa9e33215abc.zip'
Environment:
Variables:
AUDIOTRANSCRIPTS_DEST_BUCKET_PREFIX : !Ref AUDIOTRANSCRIPTSDESTBUCKETPREFIX
MULTIMODAL_OUTPUT_BUCKET : !Ref MultimodalOutputBucketResource
AUDIOTRANSCRIPTS_SOURCE_BUCKET : !Ref AudioTranscriptsSourceBucketResource
FSITranscribeRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: FSI-Transcribe-Role
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: 'sts:AssumeRole'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AWSLambdaExecute
- arn:aws:iam::aws:policy/AmazonTranscribeFullAccess
- arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
- !Ref FSIS3AccessPolicy
FSIS3AccessPolicy:
Type: 'AWS::IAM::ManagedPolicy'
Properties:
ManagedPolicyName: FSIS3AccessPolicy
Description: Custom policy for FSI Textract Role
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: S3Permissions
Effect: Allow
Action:
- s3:*
Resource:
- Fn::Sub: "arn:aws:s3:::${StockDataSourceBucket}/*"
- Fn::Sub: "arn:aws:s3:::${QueryStagingBucket}/*"
- Fn::Sub: "arn:aws:s3:::${StockDataSourceBucket}/*"
- Fn::Sub: "arn:aws:s3:::${AudioTranscriptsSourceBucketResource}/*"
- Fn::Sub: "arn:aws:s3:::${TextractSourceBucketResource}/*"
- Fn::Sub: "arn:aws:s3:::${TextractArchiveBucketResource}/*"
- Fn::Sub: "arn:aws:s3:::${TextractRawOutputBucketResource}/*"
- Fn::Sub: "arn:aws:s3:::${MultimodalOutputBucketResource}/*"
- Fn::Sub: "arn:aws:s3:::${StockDataSourceBucket}"
- Fn::Sub: "arn:aws:s3:::${QueryStagingBucket}"
- Fn::Sub: "arn:aws:s3:::${StockDataSourceBucket}"
- Fn::Sub: "arn:aws:s3:::${AudioTranscriptsSourceBucketResource}"
- Fn::Sub: "arn:aws:s3:::${TextractSourceBucketResource}"
- Fn::Sub: "arn:aws:s3:::${TextractArchiveBucketResource}"
- Fn::Sub: "arn:aws:s3:::${TextractRawOutputBucketResource}"
- Fn::Sub: "arn:aws:s3:::${MultimodalOutputBucketResource}"
FSITextractRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: FSI-Textract-Role
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: 'sts:AssumeRole'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/AmazonSNSFullAccess
- arn:aws:iam::aws:policy/AmazonSQSFullAccess
- arn:aws:iam::aws:policy/AmazonTextractFullAccess
- !Ref FSIS3AccessPolicy
FSITextractAsyncInvocationFunction:
DependsOn: CopyZips
Type: 'AWS::Lambda::Function'
Properties:
FunctionName: FSI-TextractAsyncInvocationFunction
Runtime: python3.10
Timeout: 600
Handler: lambda_function.lambda_handler
Role: !GetAtt FSITextractAsyncInvocationFunctionRole.Arn
Code:
S3Bucket: !Ref 'LambdaZipsBucket'
S3Key: !Sub '${SourceCodeprefix}FSI-TextractAsyncInvocationFunction-759363ea-7b4b-411c-a96f-b137a595387b.zip'
FSITextractAsyncInvocationFunctionRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: FSI-TextractAsyncInvocationFunction-Role
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: 'sts:AssumeRole'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/service-role/AWSLambdaRole
TextExractRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: TextExractRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: textract.amazonaws.com
Action: 'sts:AssumeRole'
Condition:
StringEquals:
aws:SourceAccount: !Ref AWS::AccountId
ArnLike:
aws:SourceArn: !Sub 'arn:aws:textract:*:${AWS::AccountId}:*'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/AmazonTextractFullAccess
- arn:aws:iam::aws:policy/service-role/AmazonTextractServiceRole
- !Ref FSIS3AccessPolicy
KendraIndex:
DependsOn: SampleKendraIndexServiceRole
Type: 'AWS::Kendra::Index'
Properties:
Name: FSIKendraIndex
RoleArn: !GetAtt SampleKendraIndexServiceRole.Arn
Description: FSI Kendra Index
Edition: DEVELOPER_EDITION
CapacityUnits:
QueryCapacityUnits: 1
StorageCapacityUnits: 1
KendraDataSource:
DependsOn: KendraDataSourceRole
Type: 'AWS::Kendra::DataSource'
Properties:
DataSourceConfiguration:
S3Configuration:
BucketName: !Ref MultimodalOutputBucketResource
Description: 'FSI Index'
IndexId: !Ref KendraIndex
Name: 'FSIDatasource'
RoleArn: !GetAtt KendraDataSourceRole.Arn
Schedule: "cron(* 0 * * ? *)"
Type: 'S3'
KendraDataSourcePolicy:
Type: 'AWS::IAM::ManagedPolicy'
Properties:
ManagedPolicyName: KendraDataSourcePolicy
Description: Custom policy for Kendra data source
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:GetObject
- s3:ListBucket
Resource:
- Fn::Sub: "arn:aws:s3:::${StockDataSourceBucket}/*"
- Fn::Sub: "arn:aws:s3:::${QueryStagingBucket}/*"
- Fn::Sub: "arn:aws:s3:::${StockDataSourceBucket}/*"
- Fn::Sub: "arn:aws:s3:::${AudioTranscriptsSourceBucketResource}/*"
- Fn::Sub: "arn:aws:s3:::${TextractSourceBucketResource}/*"
- Fn::Sub: "arn:aws:s3:::${TextractArchiveBucketResource}/*"
- Fn::Sub: "arn:aws:s3:::${MultimodalOutputBucketResource}/*"
- Effect: Allow
Action:
- kendra:BatchPutDocument
- kendra:BatchDeleteDocument
Resource: "*"
KendraDataSourceRole:
DependsOn: KendraDataSourcePolicy
Type: 'AWS::IAM::Role'
Properties:
RoleName: MyKendraDataSourceRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: [kendra.amazonaws.com]
Action: ['sts:AssumeRole']
ManagedPolicyArns:
- !Ref KendraDataSourcePolicy
- !Ref FSIS3AccessPolicy
KendraIndexServicePolicy:
Type: 'AWS::IAM::ManagedPolicy'
Properties:
ManagedPolicyName: KendraIndexServicePolicy
Description: Custom policy for Kendra Index Service
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- cloudwatch:PutMetricData
Resource:
- !Sub "arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/kendra/${AWS::StackName}/*"
Condition:
StringEquals:
cloudwatch:namespace: AWS/Kendra
- Effect: Allow
Action:
- logs:DescribeLogGroups
Resource:
- !Sub "arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/kendra/${AWS::StackName}/*"
- Effect: Allow
Action:
- logs:CreateLogGroup
Resource:
- !Sub arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/kendra/*
- Effect: Allow
Action:
- logs:DescribeLogStreams
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/kendra/*:log-stream:*
SampleKendraIndexServiceRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
Effect: Allow
Principal:
Service: kendra.amazonaws.com
Action: sts:AssumeRole
Path: /
ManagedPolicyArns:
- !Ref KendraIndexServicePolicy
multiagentllmstockdata:
Type: 'AWS::S3::Bucket'
Properties:
PublicAccessBlockConfiguration:
BlockPublicAcls: true
IgnorePublicAcls: true
BlockPublicPolicy: true
RestrictPublicBuckets: true
blogstockpricesdb:
Type: AWS::Glue::Database
Properties:
DatabaseInput:
Name: blog-stock-prices-db
Description: "Database for Stock information"
CatalogId: !Ref AWS::AccountId
gluetable:
Type: AWS::Glue::Table
Properties:
DatabaseName: !Ref blogstockpricesdb
CatalogId: !Ref AWS::AccountId
TableInput:
Name: stock_prices
Parameters: { "classification" : "csv" }
StorageDescriptor:
Location: !Sub "s3://${multiagentllmstockdata}/"
InputFormat: "org.apache.hadoop.mapred.TextInputFormat"
OutputFormat: "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat"
SerdeInfo:
Parameters: { "separatorChar" : "," }
SerializationLibrary: "org.apache.hadoop.hive.serde2.OpenCSVSerde"
StoredAsSubDirectories: false
Columns:
- Name: date
Type: string
- Name: AAAA
Type: double
- Name: FF
Type: double
- Name: BBBB
Type: double
- Name: ZZZZ
Type: double
- Name: GG
Type: double
- Name: DDD
Type: double
- Name: WWW
Type: double
- Name: CCC
Type: double
- Name: GGMM
Type: double
- Name: TTT
Type: double
- Name: UUU
Type: double
- Name: SSSS
Type: double
- Name: XXX
Type: double
- Name: RRR
Type: double
- Name: YYY
Type: double
- Name: MM
Type: double
- Name: PPP
Type: double
- Name: JJJ
Type: double
- Name: SSXX
Type: double
stockpricescrawlerrole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service: "glue.amazonaws.com"
Action: "sts:AssumeRole"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole"
GlueCrawlerPolicy:
Type: "AWS::IAM::Policy"
Properties:
PolicyName: "your-glue-crawler-policy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action: "glue:*"
Resource:
- !Sub "arn:${AWS::Partition}:glue:${AWS::Region}:${AWS::AccountId}:database/${blogstockpricesdb}"
- !Sub "arn:${AWS::Partition}:glue:${AWS::Region}:${AWS::AccountId}:table/${blogstockpricesdb}/${gluetable}"
Roles:
- !Ref stockpricescrawlerrole
ChatHistoryTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: chat_history
AttributeDefinitions:
- AttributeName: SessionId
AttributeType: S
KeySchema:
- AttributeName: SessionId
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
DynamoDBChatHistoryRole:
Type: AWS::IAM::Role
Properties:
RoleName: DynamoDBChatHistoryRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: ec2.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- !Ref DynamoDBChatHistoryPolicy
DynamoDBChatHistoryPolicy:
DependsOn: ChatHistoryTable
Type: AWS::IAM::ManagedPolicy
Properties:
ManagedPolicyName: DynamoDBChatHistoryPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- "dynamodb:GetItem"
- "dynamodb:PutItem"
- "dynamodb:UpdateItem"
- "dynamodb:DeleteItem"
Resource:
- !GetAtt ChatHistoryTable.Arn
stockpricescrawler:
Type: AWS::Glue::Crawler
Properties:
Name: stockpricescrawler
Role: !GetAtt stockpricescrawlerrole.Arn
#Classifiers: none, use the default classifier
Description: AWS Glue crawler to crawl stock data
#Schedule: none, use default run-on-demand
DatabaseName: !Ref blogstockpricesdb
Targets:
S3Targets:
# Public S3 bucket with the flights data
- Path: !Sub "s3://${multiagentllmstockdata}/"
SchemaChangePolicy:
UpdateBehavior: "UPDATE_IN_DATABASE"
DeleteBehavior: "LOG"
Configuration: "{\"Version\":1.0,\"CrawlerOutput\":{\"Partitions\":{\"AddOrUpdateBehavior\":\"InheritFromTable\"},\"Tables\":{\"AddOrUpdateBehavior\":\"MergeNewColumns\"}}}"
CopyZips:
Type: Custom::CopyZips
Properties:
ServiceToken: !GetAtt 'CopyZipsFunction.Arn'
DestBucket: !Ref 'LambdaZipsBucket'
SourceBucket: !Ref 'SourceCodeBucket'
Prefix: !Ref SourceCodeprefix
Objects:
- FSI-TextractProcessingFunction-b6054c5e-0286-4cfb-a2f3-55d408a9972c.zip
- FSI-KeyPhrasesDetection-6b3c773e-4dbc-4930-b569-0f4ad3cfb9c9.zip
- FSI-SentimentDetecttion-82a6bf78-2382-479e-b112-5d96d99561ef.zip
- FSI-Transcribe-df01dca6-8e3c-46cc-b9a9-aa9e33215abc.zip
- FSI-TextractAsyncInvocationFunction-759363ea-7b4b-411c-a96f-b137a595387b.zip
CopyZipsRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- !Ref LambdaCopyPolicy
Path: /
LambdaCopyPolicy:
Type: 'AWS::IAM::ManagedPolicy'
Properties:
ManagedPolicyName: LambdaCopyPolicy
Description: Custom policy for Lambda copy function
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:GetObject
Resource:
- !Sub 'arn:aws:s3:::${SourceCodeBucket}/${SourceCodeprefix}*'
- Effect: Allow
Action:
- s3:PutObject
- s3:DeleteObject
Resource:
- !Sub 'arn:aws:s3:::${LambdaZipsBucket}/${SourceCodeprefix}*'
CopyZipsFunction:
Type: AWS::Lambda::Function
Properties:
Description: Copies objects from a source S3 bucket to a destination
Handler: index.handler
Runtime: python3.10
Role: !GetAtt 'CopyZipsRole.Arn'
Timeout: 240
Code:
ZipFile: |
import json
import logging
import threading
import boto3
import cfnresponse
def copy_objects(source_bucket, dest_bucket, prefix, objects):
s3 = boto3.client('s3')
for o in objects:
key = prefix + o
copy_source = {
'Bucket': source_bucket,
'Key': key
}
print(('copy_source: %s' % copy_source))
print(('dest_bucket = %s'%dest_bucket))
print(('key = %s' %key))
s3.copy_object(CopySource=copy_source, Bucket=dest_bucket,
Key=key)
def delete_objects(bucket, prefix, objects):
s3 = boto3.client('s3')
objects = {'Objects': [{'Key': prefix + o} for o in objects]}
s3.delete_objects(Bucket=bucket, Delete=objects)
def timeout(event, context):
logging.error('Execution is about to time out, sending failure response to CloudFormation')
cfnresponse.send(event, context, cfnresponse.FAILED, {}, None)
def handler(event, context):
# make sure we send a failure to CloudFormation if the function
# is going to timeout
timer = threading.Timer((context.get_remaining_time_in_millis()
/ 1000.00) - 0.5, timeout, args=[event, context])
timer.start()
print(('Received event: %s' % json.dumps(event)))
status = cfnresponse.SUCCESS
try:
source_bucket = event['ResourceProperties']['SourceBucket']
dest_bucket = event['ResourceProperties']['DestBucket']
prefix = event['ResourceProperties']['Prefix']
objects = event['ResourceProperties']['Objects']
if event['RequestType'] == 'Delete':
delete_objects(dest_bucket, prefix, objects)
else:
copy_objects(source_bucket, dest_bucket, prefix, objects)
except Exception as e:
logging.error('Exception: %s' % e, exc_info=True)
status = cfnresponse.FAILED
finally:
timer.cancel()
cfnresponse.send(event, context, status, {}, None)
Outputs:
MultimodalOutputBucket:
Value:
Fn::Sub: "s3://${MultimodalOutputBucketResource}/"
PDFSourceBucket:
Value:
Fn::Sub: "s3://${TextractSourceBucketResource}/"
AudioSourceBucket:
Value:
Fn::Sub: "s3://${AudioTranscriptsSourceBucketResource}/"
FSITextractLambdaFunction:
Value:
!GetAtt FSITextract.Arn
FSISentimentDetecttionLambdaFunction:
Value:
!GetAtt FSISentimentDetecttion.Arn
FSIKeyPhrasesDetectionLambdaFunction:
Value:
!GetAtt FSIKeyPhrasesDetection.Arn
FSITranscribeLambdaFunction:
Value:
!GetAtt FSITranscribe.Arn
KendraIndexId:
Value:
!GetAtt KendraIndex.Id
KendraDataSourceId:
Value:
!GetAtt KendraDataSource.Id
MultiAgentllmStockData:
Value:
Fn::Sub: "s3://${multiagentllmstockdata}/"
stockpricesdb:
Value: !Ref blogstockpricesdb
NotebookInstanceName:
Value: !Ref MultiModalSagemakerNotebookInstance
Description: Name of the SageMaker notebook instance
QueryStagingBucket:
Value:
Fn::Sub: "s3://${QueryStagingBucket}/"
StockDataSourceBucket:
Value:
Fn::Sub: "s3://${StockDataSourceBucket}/"
ChatHistoryTableName:
Value: !Ref ChatHistoryTable
Description: The name of the created DynamoDB table for chat history
AWSRegion:
Value: !Ref AWS::Region
Description: The AWS region where the stack is being created