Skip to content

Commit 0fa2685

Browse files
committed
Restore createIndexFunction handler
1 parent d4b5f58 commit 0fa2685

File tree

1 file changed

+6
-42
lines changed
  • packages/createIndexFunction

1 file changed

+6
-42
lines changed

packages/createIndexFunction/app.py

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
22
import logging
33
import os
4-
import time
54

65
import boto3
76
from botocore.exceptions import NoCredentialsError
@@ -33,29 +32,6 @@ def get_opensearch_client(endpoint):
3332
)
3433

3534

36-
def wait_for_index(opensearch_client, index_name, timeout=120, poll_interval=4):
37-
"""
38-
Waits for the OpenSearch index to exist and be at least 'yellow' health.
39-
"""
40-
logger.info(f"Polling for index '{index_name}' to exist and be ready...")
41-
start = time.time()
42-
while True:
43-
try:
44-
if opensearch_client.indices.exists(index=index_name):
45-
health = opensearch_client.cluster.health(index=index_name, wait_for_status="yellow", timeout="5s")
46-
status = health.get("status")
47-
logger.info(f"Index '{index_name}' exists, health: {status}")
48-
if status in ("yellow", "green"):
49-
return
50-
else:
51-
logger.info(f"Index '{index_name}' does not exist yet...")
52-
except Exception as exc:
53-
logger.info(f"Error checking index status: {exc}")
54-
if time.time() - start > timeout:
55-
raise TimeoutError(f"Timed out waiting for index '{index_name}' to become ready.")
56-
time.sleep(poll_interval)
57-
58-
5935
def handler(event, context):
6036
logger.info("Received event: %s", json.dumps(event, indent=2))
6137
print(event)
@@ -90,11 +66,11 @@ def handler(event, context):
9066
"space_type": "l2",
9167
},
9268
},
93-
"AMAZON_BEDROCK_METADATA": {
69+
"AMAZON_BEDROCK_METADATA\t": {
9470
"type": "text",
9571
"index": "false",
9672
},
97-
"AMAZON_BEDROCK_TEXT_CHUNK": {
73+
"AMAZON_BEDROCK_TEXT_CHUNK\t": {
9874
"type": "text",
9975
"index": "true",
10076
},
@@ -104,23 +80,11 @@ def handler(event, context):
10480
}
10581

10682
try:
107-
# Check if index exists first
108-
if not opensearch_client.indices.exists(index=params["index"]):
109-
logger.info(f"Creating index {params['index']}")
110-
opensearch_client.indices.create(
111-
index=params["index"], body=params["body"]
112-
)
113-
logger.info(f"Index {params['index']} creation initiated.")
114-
else:
115-
logger.info(f"Index {params['index']} already exists")
116-
# Wait for the index to be available and ready
117-
wait_for_index(opensearch_client, params["index"], timeout=300)
118-
logger.info(f"Index {params['index']} is ready.")
119-
time.sleep(10)
120-
logger.info(f"Index {params['index']} is ready.")
83+
opensearch_client.indices.create(
84+
index=params["index"], body=params["body"]
85+
)
12186
except Exception as e:
122-
logger.error(f"Error creating or waiting for index: {e}")
123-
raise e # Re-raise to fail the custom resource
87+
logger.error(e)
12488

12589
elif event["RequestType"] == "Delete":
12690
try:

0 commit comments

Comments
 (0)