@@ -98,12 +98,15 @@ def handle_s3_vector_resources(event, context, properties):
9898 """Handle S3 Vector bucket and index operations."""
9999 request_type = event ['RequestType' ]
100100
101- bucket_name = properties .get ('BucketName' , '' )
101+ raw_bucket_name = properties .get ('BucketName' , '' )
102+ bucket_name = sanitize_bucket_name (raw_bucket_name )
102103 index_name = properties .get ('IndexName' , '' )
103104 embedding_model = properties .get ('EmbeddingModel' , '' )
104105 region = properties .get ('Region' , '' )
105106 kms_key_arn = properties .get ('KmsKeyArn' , '' )
106107
108+ logger .info (f"Raw bucket name: { raw_bucket_name } , Sanitized bucket name: { bucket_name } " )
109+
107110 # Initialize S3 Vectors client
108111 s3vectors_client = boto3 .client ('s3vectors' , region_name = region )
109112
@@ -114,7 +117,8 @@ def handle_s3_vector_resources(event, context, properties):
114117 elif request_type == 'Update' :
115118 logger .info (f"Updating S3 Vector bucket: { bucket_name } " )
116119 old_properties = event .get ('OldResourceProperties' , {})
117- old_bucket_name = old_properties .get ('BucketName' , '' )
120+ old_raw_bucket_name = old_properties .get ('BucketName' , '' )
121+ old_bucket_name = sanitize_bucket_name (old_raw_bucket_name )
118122 old_index_name = old_properties .get ('IndexName' , '' )
119123
120124 # If bucket or index name changed, delete old and create new
@@ -132,6 +136,50 @@ def handle_s3_vector_resources(event, context, properties):
132136 return {'Status' : 'Deleted' }
133137
134138
139+ def sanitize_bucket_name (bucket_name ):
140+ """
141+ Sanitize bucket name to comply with S3 bucket naming rules:
142+ - Must be lowercase letters, numbers, and hyphens only
143+ - Must be between 3 and 63 characters long
144+ - Must not start or end with a hyphen
145+ - Must not contain consecutive hyphens
146+ """
147+ if not bucket_name :
148+ return 'default-s3-vectors'
149+
150+ # Convert to lowercase
151+ sanitized = bucket_name .lower ()
152+
153+ # Replace invalid characters with hyphens
154+ import re
155+ sanitized = re .sub (r'[^a-z0-9\-]' , '-' , sanitized )
156+
157+ # Remove consecutive hyphens
158+ sanitized = re .sub (r'-+' , '-' , sanitized )
159+
160+ # Remove leading and trailing hyphens
161+ sanitized = sanitized .strip ('-' )
162+
163+ # Ensure minimum length
164+ if len (sanitized ) < 3 :
165+ sanitized = f"s3vectors-{ sanitized } "
166+
167+ # Ensure maximum length (S3 limit is 63 characters)
168+ if len (sanitized ) > 63 :
169+ sanitized = sanitized [:60 ] + "-kb"
170+
171+ # Ensure it doesn't start with hyphen (redundant but safe)
172+ if sanitized .startswith ('-' ):
173+ sanitized = 's3' + sanitized
174+
175+ # Ensure it doesn't end with hyphen (redundant but safe)
176+ if sanitized .endswith ('-' ):
177+ sanitized = sanitized [:- 1 ] + 'kb'
178+
179+ logger .info (f"Sanitized bucket name: { bucket_name } → { sanitized } " )
180+ return sanitized
181+
182+
135183def handle_knowledge_base_resources (event , context , properties ):
136184 """Handle Knowledge Base creation with S3 Vectors using Bedrock API."""
137185 request_type = event ['RequestType' ]
0 commit comments