Skip to content

Commit 0115242

Browse files
author
Daniel Lorch
committed
chore: make bucket creation optional, add KMS key, add dataset bucket
1 parent d278154 commit 0115242

File tree

1 file changed

+180
-44
lines changed

1 file changed

+180
-44
lines changed

plugins/dynamic-few-shot-lambda/template.yml

Lines changed: 180 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,27 @@ Parameters:
1818

1919
VectorBucketName:
2020
Type: String
21-
Default: "genaiidp-dynamic-few-shot"
21+
Default: ""
22+
Description: >-
23+
(Optional) Existing S3 vectors bucket used. Provide the name of an existing S3 vectors
24+
bucket here or leave blank to automatically create a new S3 vectors bucket.
2225
2326
VectorIndexName:
2427
Type: String
25-
Default: "documents"
26-
27-
VectorDimensions:
28-
Type: Number
29-
Default: 3072
28+
Default: ""
29+
Description: >-
30+
(Optional) Existing S3 vectors index used. Provide the name of an existing S3 vectors
31+
index here or leave blank to automatically create a new S3 vectors index.
3032
3133
ModelId:
3234
Type: String
3335
Default: "amazon.nova-2-multimodal-embeddings-v1:0"
36+
Description: Vector embedding model to use to create meaningful vector representations of documents
37+
38+
VectorDimensions:
39+
Type: Number
40+
Default: 3072
41+
Description: Vector embedding length to use, as defined by the embedding model in use
3442

3543
TopK:
3644
Type: Number
@@ -44,7 +52,14 @@ Parameters:
4452

4553
LambdaFunctionName:
4654
Type: String
47-
Default: "GENAIIDP-dynamic-few-shot"
55+
Default: "IDP-dynamic-few-shot"
56+
57+
DatasetBucketName:
58+
Type: String
59+
Default: ""
60+
Description: >-
61+
(Optional) Existing bucket used for dynamic few-shot datasets. Provide the name of
62+
an existing bucket here or leave blank to automatically create a new bucket.
4863
4964
# Logging configuration
5065
LogLevel:
@@ -82,20 +97,16 @@ Parameters:
8297
3653,
8398
]
8499

85-
GenAIIDPS3OutputBucketName:
86-
Type: String
87-
Description: "GenAIIDP S3OutputBucketName"
88-
89-
GenAIIDPS3WorkingBucketName:
90-
Type: String
91-
Description: "GenAIIDP WorkingBucket Name"
92-
93-
GenAIIDPCustomerManagedEncryptionKeyArn:
100+
# GenAI IDP parameters
101+
IDPS3LoggingBucketName:
94102
Type: String
95-
Description: "GenAIIDP CustomerManagedEncryptionKey ARN"
103+
Description: "IDP LoggingBucket Name"
96104

97105
Conditions:
98106
HasPermissionsBoundary: !Not [!Equals [!Ref PermissionsBoundaryArn, ""]]
107+
ShouldCreateVectorBucket: !Equals [ !Ref VectorBucketName, "" ]
108+
ShouldCreateVectorIndex: !Equals [ !Ref VectorIndexName, "" ]
109+
ShouldCreateDatasetBucket: !Equals [ !Ref DatasetBucketName, "" ]
99110

100111
Resources:
101112

@@ -126,12 +137,22 @@ Resources:
126137
- arm64
127138
Timeout: 300
128139
MemorySize: 512
129-
Description: Demo Lambda function for GenAI IDP dynamic few-shot prompting
140+
Description: Demo Lambda function for GenAI IDP dynamic few-shot prompting using S3 Vectors
130141
Environment:
131142
Variables:
132143
LOG_LEVEL: !Ref LogLevel
133-
S3VECTOR_BUCKET: !Ref VectorBucketName
134-
S3VECTOR_INDEX: !Ref VectorIndexName
144+
S3VECTOR_BUCKET: !If
145+
- ShouldCreateVectorBucket
146+
# Error: Requested attribute VectorBucketName must be a readonly property in schema for AWS::S3Vectors::VectorBucket
147+
# - !GetAtt DynamicFewShotVectorBucket.VectorBucketName
148+
- !Select [1, !Split ["/", !Ref DynamicFewShotVectorBucket]]
149+
- !Ref VectorBucketName
150+
S3VECTOR_INDEX: !If
151+
- ShouldCreateVectorIndex
152+
# Error: Requested attribute IndexName must be a readonly property in schema for AWS::S3Vectors::Index
153+
# - !GetAtt DocumentsIndex.IndexName
154+
- !Select [3, !Split ["/", !Ref DocumentsIndex]]
155+
- !Ref VectorIndexName
135156
S3VECTOR_DIMENSIONS: !Ref VectorDimensions
136157
MODEL_ID: !Ref ModelId
137158
TOP_K: !Ref TopK
@@ -142,9 +163,10 @@ Resources:
142163
Policies:
143164
- AWSLambdaBasicExecutionRole
144165
- S3ReadPolicy:
145-
BucketName: !Ref GenAIIDPS3OutputBucketName
146-
- S3ReadPolicy:
147-
BucketName: !Ref GenAIIDPS3WorkingBucketName
166+
BucketName: !If
167+
- ShouldCreateDatasetBucket
168+
- !Ref DatasetBucket
169+
- !Ref DatasetBucketName
148170
- Statement:
149171
- Effect: Allow
150172
Action: cloudwatch:PutMetricData
@@ -161,7 +183,13 @@ Resources:
161183
- s3vectors:GetVectors
162184
- s3vectors:QueryVectors
163185
Resource:
164-
- !Ref DynamicFewShotVectorIndex
186+
- !If
187+
- ShouldCreateVectorIndex
188+
- !Ref DocumentsIndex
189+
- !If
190+
- ShouldCreateVectorBucket
191+
- !Sub "${DynamicFewShotVectorBucket}/index/${DocumentsIndex}"
192+
- !Sub "arn:${AWS::Partition}:s3vectors:${AWS::Region}:${AWS::AccountId}:bucket/${VectorBucketName}/index/${DocumentsIndex}"
165193
- Effect: Allow
166194
Action:
167195
- kms:Encrypt
@@ -170,32 +198,27 @@ Resources:
170198
- kms:GenerateDataKey*
171199
- kms:DescribeKey
172200
Resource:
173-
- !Ref GenAIIDPCustomerManagedEncryptionKeyArn
201+
- !GetAtt CustomerManagedEncryptionKey.Arn
174202

175203
DynamicFewShotLogGroup:
176204
Type: AWS::Logs::LogGroup
177205
Properties:
178206
LogGroupName: !Sub "/aws/lambda/${LambdaFunctionName}"
179207
RetentionInDays: !Ref LogRetentionDays
180-
KmsKeyId: !GetAtt GenAIIDPCustomerManagedEncryptionKeyArn
208+
KmsKeyId: !GetAtt CustomerManagedEncryptionKey.Arn
181209

182210
DynamicFewShotVectorBucket:
183211
Type: AWS::S3Vectors::VectorBucket
184-
Metadata:
185-
cfn_nag:
186-
rules_to_suppress:
187-
- id: W84
188-
reason: "Demo function - KMS CMK not required, but can be added by customer for production use cases"
189-
# checkov:skip=CKV_AWS_158: "Demo function - KMS CMK not required, but can be added by customer for production use cases"
212+
Condition: ShouldCreateVectorBucket
190213
Properties:
191-
VectorBucketName: !Ref VectorBucketName
192214
EncryptionConfiguration:
193-
SseType: "AES256"
215+
SseType: "aws:kms"
216+
KmsKeyArn: !GetAtt CustomerManagedEncryptionKey.Arn
194217

195-
DynamicFewShotVectorIndex:
218+
DocumentsIndex:
196219
Type: AWS::S3Vectors::Index
220+
Condition: ShouldCreateVectorIndex
197221
Properties:
198-
IndexName: !Ref VectorIndexName
199222
DataType: "float32"
200223
Dimension: !Ref VectorDimensions
201224
DistanceMetric: "cosine"
@@ -204,7 +227,111 @@ Resources:
204227
- "classPrompt"
205228
- "attributesPrompt"
206229
- "imagePath"
207-
VectorBucketArn: !Ref DynamicFewShotVectorBucket
230+
VectorBucketName: !If
231+
- ShouldCreateVectorBucket
232+
- !Ref AWS::NoValue
233+
- VectorBucketName
234+
VectorBucketArn: !If
235+
- ShouldCreateVectorBucket
236+
- !Ref DynamicFewShotVectorBucket
237+
- !Ref AWS::NoValue
238+
239+
DatasetBucket:
240+
Type: AWS::S3::Bucket
241+
Condition: ShouldCreateDatasetBucket
242+
DeletionPolicy: RetainExceptOnCreate
243+
Properties:
244+
BucketEncryption:
245+
ServerSideEncryptionConfiguration:
246+
- ServerSideEncryptionByDefault:
247+
SSEAlgorithm: aws:kms
248+
KMSMasterKeyID: !Ref CustomerManagedEncryptionKey
249+
PublicAccessBlockConfiguration:
250+
BlockPublicAcls: true
251+
BlockPublicPolicy: true
252+
IgnorePublicAcls: true
253+
RestrictPublicBuckets: true
254+
VersioningConfiguration:
255+
Status: Enabled
256+
LoggingConfiguration:
257+
DestinationBucketName: !Ref IDPS3LoggingBucketName
258+
LogFilePrefix: fewshot-dataset-bucket-logs/
259+
260+
DatasetBucketPolicy:
261+
Type: AWS::S3::BucketPolicy
262+
Condition: ShouldCreateDatasetBucket
263+
Properties:
264+
Bucket: !Ref DatasetBucket
265+
PolicyDocument:
266+
Version: "2012-10-17"
267+
Statement:
268+
- Sid: EnforceSSLOnly
269+
Effect: Deny
270+
Principal: "*"
271+
Action: "s3:*"
272+
Resource:
273+
- !Sub "${DatasetBucket.Arn}/*"
274+
- !Sub "${DatasetBucket.Arn}"
275+
Condition:
276+
Bool:
277+
"aws:SecureTransport": false
278+
279+
CustomerManagedEncryptionKey:
280+
Type: AWS::KMS::Key
281+
Metadata:
282+
security-matrix:
283+
rules_to_suppress:
284+
- id: IAM-005
285+
reason: "No cross-account access - only same account root and AWS services"
286+
- id: KMS-007
287+
reason: "KMS monitoring not required for this IDP solution - comprehensive CloudWatch monitoring already in place"
288+
- id: KMS-002
289+
reason: "kms:* permission for account root is standard pattern for administrative access to KMS keys"
290+
Properties:
291+
Description: KMS key for encryption of dynamic few-shot resources
292+
EnableKeyRotation: true
293+
KeyPolicy:
294+
Version: "2012-10-17"
295+
Statement:
296+
- Sid: Enable IAM User Permissions
297+
Effect: Allow
298+
Principal:
299+
AWS: !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:root"
300+
Action: kms:*
301+
Resource: "*"
302+
- Sid: Allow lambda to access the Keys
303+
Effect: Allow
304+
Principal:
305+
AWS: !Sub "arn:${AWS::Partition}:iam::${AWS::AccountId}:root"
306+
Action:
307+
- kms:Encrypt
308+
- kms:Decrypt
309+
- kms:ReEncrypt*
310+
- kms:GenerateDataKey*
311+
- kms:DescribeKey
312+
Resource: "*"
313+
- Sid: Allow CloudWatch Logs to use the key
314+
Effect: Allow
315+
Principal:
316+
Service: !Sub "logs.${AWS::URLSuffix}"
317+
Action:
318+
- kms:Encrypt
319+
- kms:Decrypt
320+
- kms:ReEncrypt*
321+
- kms:GenerateDataKey*
322+
- kms:DescribeKey
323+
Resource: "*"
324+
- Sid: Allow S3 Vectors indexing service to use the key
325+
Effect: Allow
326+
Principal:
327+
Service: !Sub "indexing.s3vectors.${AWS::URLSuffix}"
328+
Action:
329+
- kms:Encrypt
330+
- kms:Decrypt
331+
- kms:ReEncrypt*
332+
- kms:GenerateDataKey*
333+
- kms:DescribeKey
334+
Resource: "*"
208335

209336
Outputs:
210337

@@ -220,17 +347,26 @@ Outputs:
220347
Description: CloudWatch Log Group for monitoring demo Lambda execution
221348
Value: !Ref DynamicFewShotLogGroup
222349

223-
DynamicFewShotVectorBucketArn:
350+
VectorBucketName:
224351
Description: S3 Vectors bucket for dynamic few-shot examples
225-
Value: !Ref DynamicFewShotVectorBucket
352+
Value: !If
353+
- ShouldCreateVectorBucket
354+
- !Select [1, !Split ["/", !Ref DynamicFewShotVectorBucket]]
355+
- !Ref VectorBucketName
226356

227-
DynamicFewShotVectorIndexArn:
357+
VectorIndexName:
228358
Description: S3 Vectors index for dynamic few-shot examples
229-
Value: !Ref DynamicFewShotVectorIndex
359+
Value: !If
360+
- ShouldCreateVectorIndex
361+
- !Select [3, !Split ["/", !Ref DocumentsIndex]]
362+
- !Ref VectorIndexName
230363

231-
DynamicFewShotDatasetBucket:
232-
Description: S3 Bucket for example data sets
233-
Value: !Ref DynamicFewShotDatasetBucket
364+
DatasetBucket:
365+
Description: S3 bucket for example data sets
366+
Value: !If
367+
- ShouldCreateDatasetBucket
368+
- !Ref DatasetBucket
369+
- !Ref DatasetBucketName
234370

235371
UsageInstructions:
236372
Description: How to use this Lambda in your IDP configuration

0 commit comments

Comments
 (0)