1010import time
1111import boto3
1212from opensearchpy import OpenSearch , RequestsHttpConnection , AWSV4SignerAuth
13- from aws_lambda_powertools import Logger
1413from aws_lambda_powertools .utilities .typing import LambdaContext
15- from app .config .config import AWS_REGION
16-
17- logger = Logger (service = "createIndexFunction" )
14+ from app .config .config import AWS_REGION , logger
1815
1916
2017def get_opensearch_client (endpoint ):
@@ -23,7 +20,7 @@ def get_opensearch_client(endpoint):
2320 """
2421 # Determine service type: AOSS (Serverless) or ES (managed)
2522 service = "aoss" if "aoss" in endpoint else "es"
26- logger .debug (f "Connecting to OpenSearch service: { service } at { endpoint } " )
23+ logger .debug ("Connecting to OpenSearch service" , extra = { " service" : service , " endpoint" : endpoint } )
2724 return OpenSearch (
2825 hosts = [{"host" : endpoint , "port" : 443 }],
2926 http_auth = AWSV4SignerAuth (
@@ -45,22 +42,22 @@ def wait_for_index_aoss(opensearch_client, index_name, timeout=300, poll_interva
4542 AOSS has eventual consistency, so we need to poll until the index
4643 is fully created and mappings are available.
4744 """
48- logger .info (f "Waiting for index ' { index_name } ' to be available in AOSS..." )
45+ logger .info ("Waiting for index to be available in AOSS" , extra = { "index_name" : index_name } )
4946 start = time .time ()
5047 while True :
5148 try :
5249 if opensearch_client .indices .exists (index = index_name ):
5350 # Verify mappings are also available (not just index existence)
5451 mapping = opensearch_client .indices .get_mapping (index = index_name )
5552 if mapping and index_name in mapping :
56- logger .info (f "Index ' { index_name } ' exists and mappings are ready." )
53+ logger .info ("Index exists and mappings are ready" , extra = { "index_name" : index_name } )
5754 return True
5855 else :
59- logger .info (f "Index ' { index_name } ' does not exist yet..." )
56+ logger .info ("Index does not exist yet" , extra = { "index_name" : index_name } )
6057 except Exception as exc :
61- logger .info (f "Still waiting for index ' { index_name } ': { exc } " )
58+ logger .info ("Still waiting for index" , extra = { " index_name" : index_name , "error" : str ( exc )} )
6259 if time .time () - start > timeout :
63- logger .error (f "Timed out waiting for index ' { index_name } ' to be available." )
60+ logger .error ("Timed out waiting for index to be available" , extra = { "index_name" : index_name } )
6461 return False
6562 time .sleep (poll_interval )
6663
@@ -109,18 +106,18 @@ def create_and_wait_for_index(client, index_name):
109106
110107 try :
111108 if not client .indices .exists (index = params ["index" ]):
112- logger .info (f "Creating index { params [' index' ] } " )
109+ logger .info ("Creating index" , extra = { "index_name" : params [" index" ]} )
113110 client .indices .create (index = params ["index" ], body = params ["body" ])
114- logger .info (f "Index { params [' index' ] } creation initiated." )
111+ logger .info ("Index creation initiated" , extra = { "index_name" : params [" index" ]} )
115112 else :
116- logger .info (f "Index { params [' index' ] } already exists" )
113+ logger .info ("Index already exists" , extra = { "index_name" : params [" index" ]} )
117114
118115 if not wait_for_index_aoss (client , params ["index" ]):
119116 raise RuntimeError (f"Index { params ['index' ]} failed to appear in time" )
120117
121- logger .info (f "Index { params [ 'index' ] } is ready and active." )
118+ logger .info ("Index is ready and active" , extra = { "index_name" : params [ "index" ]} )
122119 except Exception as e :
123- logger .error (f "Error creating or waiting for index: { e } " )
120+ logger .error ("Error creating or waiting for index" , extra = { "error" : str ( e )} )
124121 raise e
125122
126123
@@ -181,10 +178,10 @@ def handler(event: dict, context: LambdaContext) -> dict:
181178 try :
182179 if client .indices .exists (index = index_name ):
183180 client .indices .delete (index = index_name )
184- logger .info (f "Deleted index { index_name } " )
181+ logger .info ("Deleted index" , extra = { " index_name" : index_name } )
185182 except Exception as e :
186183 # Don't fail deletion if index cleanup fails
187- logger .error (f "Error deleting index: { e } " )
184+ logger .error ("Error deleting index" , extra = { "error" : str ( e )} )
188185 return {
189186 "PhysicalResourceId" : event .get ("PhysicalResourceId" , f"index-{ index_name } " ),
190187 "Status" : "SUCCESS" ,
@@ -193,5 +190,5 @@ def handler(event: dict, context: LambdaContext) -> dict:
193190 raise ValueError (f"Invalid request type: { request_type } " )
194191
195192 except Exception as e :
196- logger .error (f "Error processing request: { e } " )
193+ logger .error ("Error processing request" , extra = { "error" : str ( e )} )
197194 raise
0 commit comments