-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbedrock-KB.yaml
579 lines (548 loc) · 18.9 KB
/
bedrock-KB.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
#
# Create Bedrock Knowledge Base
#
Description: >
Create a Bedrock Knowledge Base and an OpenSearch Serverless collection (uksb-firp0rmi6s) (tag: KnowledgeBase)
Parameters:
pKnowledgeBaseBucketName:
Description: Input the existing bucket name (not the ARN or URL) to be used for your knowledge base content
Type: String
pInputDocumentUploadFolderPrefix:
Type: String
# MinLength: 1
# MaxLength: 63
# AllowedPattern: ^[a-z0-9](-*[a-z0-9])*
# ConstraintDescription: Must be lowercase or numbers with a length of 1-63 characters
pEmbedModel:
Type: String
Description: Embedding models available on Amazon Bedrock
AllowedValues:
- amazon.titan-embed-text-v2:0
- amazon.titan-embed-text-v1
- cohere.embed-english-v3
- cohere.embed-multilingual-v3
Default: amazon.titan-embed-text-v2:0
pChunkingStrategy:
Type: String
Description: Chunking breaks down the text into smaller segments before embedding. The chunking strategy can't be modified after you create the Knowledge Base
AllowedValues:
- Default chunking
- Fixed-size chunking
- No chunking
Default: Fixed-size chunking
pMaxTokens:
Type: Number
MinValue: 20
MaxValue: 8192
Default: 600
Description: Maximum number of tokens per chunk (between 20 and 8192 tokens)
pOverlapPercentage:
Type: Number
MinValue: 1
MaxValue: 99
Default: 10
Description: Approximate percentage of overlapped tokens between two consecutive chunks (typical overlap is around 10% - 20%)
pIndexName:
Type: String
MinLength: 1
MaxLength: 63
Default: bedrock-knowledge-base-default-index
AllowedPattern: ^[a-z0-9](-*[a-z0-9])*
ConstraintDescription: Must be lowercase or numbers with a length of 1-63 characters
pVectorFieldName:
Type: String
Default: bedrock-knowledge-base-default-vector
pMetaDataFieldName:
Type: String
Default: AMAZON_BEDROCK_METADATA
pTextFieldName:
Type: String
Default: AMAZON_BEDROCK_TEXT_CHUNK
pArtifactsBucket:
Type: String
Description: The name (not the URL or ARN) of the S3 bucket where you staged the CloudFormation stack artifacts
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Knowledge Base Data Location
Parameters:
- pKnowledgeBaseBucketName
- pInputDocumentUploadFolderPrefix
- Label:
default: Embedding Model
Parameters:
- pEmbedModel
- Label:
default: Document Chunking
Parameters:
- pChunkingStrategy
- pMaxTokens
- pOverlapPercentage
- Label:
default: Index Details
Parameters:
- pIndexName
- pVectorFieldName
- pMetaDataFieldName
- pTextFieldName
- Label:
default: CloudFormation Stack Artifacts
Parameters:
- pArtifactsBucket
ParameterLabels:
pKnowledgeBaseBucketName:
default: S3 bucket where you will store your content
pInputDocumentUploadFolderPrefix:
default: S3 prefix for your content (optional)
pEmbedModel:
default: Choose an embedding model
pChunkingStrategy:
default: Choose a chunking strategy (default, fixed-size, or none)
pMaxTokens:
default: For fixed-size chunking, choose a maximum number of tokens per chunk
pOverlapPercentage:
default: For fixed-size chunking, choose an overlap percentage between chunks
pIndexName:
default: Index name to be created in the vector store
pVectorFieldName:
default: Vector field name
pMetaDataFieldName:
default: Metadata field name
pTextFieldName:
default: Text field name
pArtifactsBucket:
default: S3 bucket with CloudFormation stack artifacts
Conditions:
HasInputDocumentUploadFolderPrefix:
Fn::Not:
- Fn::Equals:
- ''
- Ref: pInputDocumentUploadFolderPrefix
IsChunkingStrategyFixed:
Fn::Equals:
- Ref: pChunkingStrategy
- Fixed-size chunking
IsChunkingStrategyDefault:
Fn::Equals:
- Ref: pChunkingStrategy
- Default chunking
IsChunkingStrategyNoChunking:
Fn::Equals:
- Ref: pChunkingStrategy
- No chunking
IsChunkingStrategyFixedOrDefault:
Fn::Or:
- Condition: IsChunkingStrategyFixed
- Condition: IsChunkingStrategyDefault
Mappings:
S3Path:
OpenSearchPyLayerMapping:
Name: 'opensearch/opensearchpy-layer.zip'
OpenSearchInitLambdaMapping:
Name: 'opensearch/custom-resource-lambda.zip'
BucketName:
eu-west-1:
Name: 'lex-usecases-eu-west-1'
ap-northeast-1:
Name: 'lex-usecases-ap-northeast-1'
ap-southeast-1:
Name: 'lex-usecases-ap-southeast-1'
ap-southeast-2:
Name: 'lex-usecases-ap-southeast-2'
eu-central-1:
Name: 'lex-usecases-eu-central-1'
us-east-1:
Name: 'lex-usecases-us-east-1'
us-east-2:
Name: 'lex-usecases-us-east-2'
us-west-2:
Name: 'lex-usecases-us-west-2'
Resources:
#
# OpenSearch Serverless Collection
#
OSSCollection:
Type: 'AWS::OpenSearchServerless::Collection'
Properties:
Name: !Sub
- 'kb-collection-${ID}'
- ID: !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref "AWS::StackId"]]]]
Type: VECTORSEARCH
Description: Bedrock Knowledge Base collection
DependsOn: OSSEncryptionPolicy
#
# OpenSearch Serverless resources
#
OSSEncryptionPolicy:
Type: 'AWS::OpenSearchServerless::SecurityPolicy'
Properties:
Name: !Sub
- 'encryption-policy-${ID}'
- ID: !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref "AWS::StackId"]]]]
Type: encryption
Description: Encryption policy for Bedrock Knowledge Base collection
Policy: !Sub
- '{"Rules":[{"ResourceType":"collection","Resource":["collection/${CollName}"]}],"AWSOwnedKey":true}'
- CollName: !Sub
- 'kb-collection-${ID}'
- ID: !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref "AWS::StackId"]]]]
OSSNetworkPolicy:
Type: AWS::OpenSearchServerless::SecurityPolicy
DependsOn:
- OSSEncryptionPolicy
Properties:
Name: !Sub
- 'network-policy-${ID}'
- ID: !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref "AWS::StackId"]]]]
Type: network
Policy: !Sub
- '[{"Description": "Public access for Bedrock Knowledge Base collection",
"Rules": [{"ResourceType": "dashboard", "Resource": ["collection/${CollName}"]},
{"ResourceType": "collection", "Resource": ["collection/${CollName}"]}],
"AllowFromPublic": true}]'
- CollName: !Sub
- 'kb-collection-${ID}'
- ID: !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref "AWS::StackId"]]]]
OSSDataAccessPolicy:
Type: AWS::OpenSearchServerless::AccessPolicy
DependsOn:
- OSSNetworkPolicy
Properties:
Name: !Sub
- 'data-policy-${ID}'
- ID: !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref "AWS::StackId"]]]]
Type: data
Policy: !Sub
- '[{"Rules": [{"Resource": ["collection/${CollName}"], "Permission":
["aoss:CreateCollectionItems", "aoss:UpdateCollectionItems", "aoss:DescribeCollectionItems"],
"ResourceType": "collection"}, {"ResourceType": "index", "Resource": ["index/${CollName}/*"],
"Permission": ["aoss:CreateIndex", "aoss:DescribeIndex", "aoss:ReadDocument",
"aoss:WriteDocument", "aoss:UpdateIndex", "aoss:DeleteIndex"]}],
"Principal": ["arn:aws:iam::${AWS::AccountId}:role/${KnowledgeBaseServiceRole}"]}]'
- CollName: !Sub
- 'kb-collection-${ID}'
- ID: !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref "AWS::StackId"]]]]
#
# Custom resource code to initialize OpenSearch Serverless index
#
OpenSearchPyLayer:
Type: AWS::Lambda::LayerVersion
Properties:
LayerName: !Sub
- 'lambda-layer-${ID}'
- ID: !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref "AWS::StackId"]]]]
CompatibleRuntimes:
- python3.8
- python3.9
- python3.10
- python3.11
Content:
# S3Bucket: !FindInMap [BucketName, !Ref "AWS::Region", 'Name']
S3Bucket: !Ref pArtifactsBucket
S3Key: !FindInMap [S3Path, 'OpenSearchPyLayerMapping', 'Name']
Description: opensearchpy layer including requests, requests-aws4auth, and boto3-1.34.82
LicenseInfo: Apache-2.0
OpenSearchLambdaExecutionRole:
Type: AWS::IAM::Role
DependsOn:
- OpenSearchPyLayer
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: OSSLambdaRoleDefaultPolicy # Reference: https://docs.aws.amazon.com/service-authorization/latest/reference/list_awsx-ray.html
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- xray:PutTelemetryRecords
- xray:PutTraceSegments
Resource: '*' # no resource specification applicable
- PolicyName: AllowLambdaLogs # Reference: https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazoncloudwatchlogs.html
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: arn:aws:logs:*:*:*
- PolicyName: AllowS3 # Reference: https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazons3.html
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:PutObject*
- s3:GetObject*
- s3:DeleteObject*
Resource:
Fn::Sub: arn:aws:s3:::*
- PolicyName: AOSSPermissionsPart1 # References: https://docs.aws.amazon.com/opensearch-service/latest/developerguide/security-iam-serverless.html, https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonopensearchserverless.html
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- aoss:BatchGetCollection
- aoss:CreateSecurityPolicy
- aoss:CreateAccessPolicy
- aoss:UpdateAccessPolicy
- aoss:ListCollections
- aoss:GetAccessPolicy
- aoss:ListCollections
- aoss:CreateCollection
Resource: '*' # no resource specification applicable
- PolicyName: AOSSPermissionsPart2
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- aoss:DeleteCollection
- aoss:UpdateCollection
- aoss:APIAccessAll
Resource:
Fn::Sub: arn:aws:aoss:${AWS::Region}:${AWS::AccountId}:collection/*
Metadata:
cfn_nag:
rules_to_suppress:
- id: W11
reason: Specified permissions do not appy to specific resources
CreateOSSIndexLambdaFunction:
Type: AWS::Lambda::Function
DependsOn:
- OpenSearchLambdaExecutionRole
Properties:
FunctionName: !Sub
- 'opensearch-setup-${ID}'
- ID: !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref "AWS::StackId"]]]]
Handler: oss_handler.lambda_handler
MemorySize: 1024
Role:
Fn::GetAtt:
- OpenSearchLambdaExecutionRole
- Arn
Runtime: python3.11
Timeout: 840
Code:
# S3Bucket: !FindInMap [BucketName, !Ref "AWS::Region", 'Name']
S3Bucket: !Ref pArtifactsBucket
S3Key: !FindInMap [S3Path, 'OpenSearchInitLambdaMapping', 'Name']
Layers:
- Ref: OpenSearchPyLayer
- !Sub arn:aws:lambda:${AWS::Region}:017000801446:layer:AWSLambdaPowertoolsPythonV2:68
Environment:
Variables:
POWERTOOLS_SERVICE_NAME: InfraSetupLambda
POWERTOOLS_METRICS_NAMESPACE: InfraSetupLambda-NameSpace
POWERTOOLS_LOG_LEVEL: INFO
Metadata:
cfn_nag:
rules_to_suppress:
- id: W89
reason: Customer can use VPC if desired
- id: W92
reason: One-time use Lambda for CloudFormation custom resource, no reserved concurrency needed
checkov:
skip:
- id: CKV_AWS_117
comment: Customer can use VPC if desired
- id: CKV_AWS_116
comment: One-time use Lambda for CloudFormation custom resource, no DLQ needed
- id: CKV_AWS_115
comment: One-time use Lambda for CloudFormation custom resource, no reserved concurrency needed
- id: CKV_AWS_173
comment: No sensitive data in environment variables
InvokeCreateOSSIndexLambdaFunction:
Type: Custom::InvokeCreateOSSIndexLambdaFunction
DependsOn:
- CreateOSSIndexLambdaFunction
- OSSDataAccessPolicy
Version: '1.0'
Properties:
ServiceToken: !GetAtt CreateOSSIndexLambdaFunction.Arn
collection_endpoint: !GetAtt OSSCollection.CollectionEndpoint
data_access_policy_name: !Sub
- 'data-policy-${ID}'
- ID: !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref "AWS::StackId"]]]]
index_name:
Ref: pIndexName
embedding_model_id:
Ref: pEmbedModel
#
# IAM Role used by the Bedrock service to access S3, OpenSearch, and embedding models
#
KnowledgeBaseServiceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- bedrock.amazonaws.com
Action:
- sts:AssumeRole
Condition:
StringEquals:
aws:SourceAccount: !Sub ${AWS::AccountId}
ArnLike:
aws:SourceArn: !Sub arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:knowledge-base/*
Policies:
- PolicyName: bedrock-invoke-model
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: BedrockInvokeModel
Effect: Allow
Action:
- bedrock:InvokeModel
Resource: !Sub arn:aws:bedrock:${AWS::Region}::foundation-model/*
- PolicyName: oss-api-access
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: OpenSearchServerlessAPIAccessAll
Effect: Allow
Action:
- aoss:APIAccessAll
Resource: !GetAtt OSSCollection.Arn
- PolicyName: s3-read-objects
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: S3ListBucket
Effect: Allow
Action:
- s3:ListBucket
Resource: arn:aws:s3:::*
- Sid: S3GetObject
Effect: Allow
Action:
- s3:GetObject
Resource: !Sub arn:aws:s3:::${pKnowledgeBaseBucketName}/*
#
# Bedrock Knowedge Base resources
#
KnowledgeBase:
Type: AWS::Bedrock::KnowledgeBase
DependsOn:
- InvokeCreateOSSIndexLambdaFunction
Properties:
Description: Bedrock Knowledge Base
KnowledgeBaseConfiguration:
Type: VECTOR
VectorKnowledgeBaseConfiguration:
EmbeddingModelArn:
Fn::Sub: arn:aws:bedrock:${AWS::Region}::foundation-model/${pEmbedModel}
Name: !Sub
- 'knowledge-base-${ID}'
- ID: !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref "AWS::StackId"]]]]
RoleArn: !GetAtt KnowledgeBaseServiceRole.Arn
StorageConfiguration:
OpensearchServerlessConfiguration:
CollectionArn: !GetAtt OSSCollection.Arn
FieldMapping:
MetadataField:
Ref: pMetaDataFieldName
TextField:
Ref: pTextFieldName
VectorField:
Ref: pVectorFieldName
VectorIndexName:
Ref: pIndexName
Type: OPENSEARCH_SERVERLESS
KnowledgeBaseDataSource:
Type: AWS::Bedrock::DataSource
DependsOn:
- KnowledgeBase
- InvokeCreateOSSIndexLambdaFunction
Properties:
DataSourceConfiguration:
Type: S3
S3Configuration:
BucketArn:
Fn::Sub: arn:aws:s3:::${pKnowledgeBaseBucketName}
InclusionPrefixes:
Fn::If:
- HasInputDocumentUploadFolderPrefix
- Ref: pInputDocumentUploadFolderPrefix
- Ref: AWS::NoValue
Description: KnowKnowledge Base Data Source
KnowledgeBaseId:
Ref: KnowledgeBase
Name: !Sub
- 'kb-datasource-${ID}'
- ID: !Select [4, !Split ['-', !Select [2, !Split ['/', !Ref "AWS::StackId"]]]]
VectorIngestionConfiguration:
ChunkingConfiguration:
ChunkingStrategy:
Fn::If:
- IsChunkingStrategyFixedOrDefault
- FIXED_SIZE
- NONE
FixedSizeChunkingConfiguration:
MaxTokens:
Fn::If:
- IsChunkingStrategyFixed
- Ref: pMaxTokens
- Fn::If:
- IsChunkingStrategyDefault
- 300
- Ref: AWS::NoValue
OverlapPercentage:
Fn::If:
- IsChunkingStrategyFixed
- Ref: pOverlapPercentage
- Fn::If:
- IsChunkingStrategyDefault
- 20
- Ref: AWS::NoValue
Outputs:
KnowledgeBaseBucketName:
Value: !Ref pKnowledgeBaseBucketName
Description: Knowledge Base bucket name
Export:
Name: !Sub "${AWS::StackName}-KnowledgeBaseBucketName"
KnowledgeBaseID:
Value: !Ref KnowledgeBase
Description: Knowledge Base ID
Export:
Name: !Sub "${AWS::StackName}-KnowledgeBaseID:"
OpenSearchCollectionName:
Value: !Ref OSSCollection
Description: OpenSearch collection
Export:
Name: !Sub "${AWS::StackName}-OpenSearchCollectionName"
OpenSearchCollectionArn:
Value: !GetAtt OSSCollection.Arn
Description: OpenSearch collection ARN
Export:
Name: !Sub "${AWS::StackName}-OpenSearchCollectionArn"
OpenSearchCollectionEndpoint:
Value: !GetAtt OSSCollection.CollectionEndpoint
Description: OpenSearch collection endpoint
Export:
Name: !Sub "${AWS::StackName}-OpenSearchCollectionEndpoint"
OpenSearchCollectionDashboard:
Value: !GetAtt OSSCollection.DashboardEndpoint
Description: OpenSearch collection dashboard endpoint
Export:
Name: !Sub "${AWS::StackName}-OpenSearchCollectionDashboard"
OpenSearchIndexStatus:
Value: !GetAtt InvokeCreateOSSIndexLambdaFunction.reason
Description: 'OpenSearch Index Creation Status'
Export:
Name: !Sub "${AWS::StackName}-OpenSearchIndexStatus"