Skip to content

Commit 680ffaa

Browse files
committed
Replace module-level boto3 client with lazy initialization
1 parent a655bdf commit 680ffaa

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

packages/syncKnowledgeBaseFunction/app/handler.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
from app.config.config import KNOWLEDGEBASE_ID, DATA_SOURCE_ID
66

77
logger = Logger(service="syncKnowledgeBaseFunction")
8-
bedrock_agent = boto3.client("bedrock-agent")
8+
9+
10+
def get_bedrock_agent():
11+
"""Get or create bedrock agent client"""
12+
return boto3.client("bedrock-agent")
913

1014

1115
@logger.inject_lambda_context
@@ -71,6 +75,7 @@ def handler(event, context):
7175
)
7276

7377
ingestion_start_time = time.time()
78+
bedrock_agent = get_bedrock_agent()
7479
response = bedrock_agent.start_ingestion_job(
7580
knowledgeBaseId=KNOWLEDGEBASE_ID,
7681
dataSourceId=DATA_SOURCE_ID,

packages/syncKnowledgeBaseFunction/tests/test_handler.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
@pytest.fixture
88
def mock_env():
99
"""Mock environment variables"""
10-
env_vars = {"KNOWLEDGEBASE_ID": "test-kb-id", "DATA_SOURCE_ID": "test-ds-id"}
10+
env_vars = {"KNOWLEDGEBASE_ID": "test-kb-id", "DATA_SOURCE_ID": "test-ds-id", "AWS_REGION": "eu-west-2"}
1111
with patch.dict(os.environ, env_vars):
1212
yield env_vars
1313

@@ -63,11 +63,12 @@ def multiple_s3_event():
6363
}
6464

6565

66-
@patch("app.handler.bedrock_agent")
66+
@patch("app.handler.get_bedrock_agent")
6767
@patch("time.time")
68-
def test_handler_success(mock_time, mock_bedrock, mock_env, lambda_context, s3_event):
68+
def test_handler_success(mock_time, mock_get_bedrock, mock_env, lambda_context, s3_event):
6969
"""Test successful handler execution"""
7070
mock_time.side_effect = [1000, 1001, 1002, 1003]
71+
mock_bedrock = mock_get_bedrock.return_value
7172
mock_bedrock.start_ingestion_job.return_value = {
7273
"ingestionJob": {"ingestionJobId": "job-123", "status": "STARTING"}
7374
}
@@ -85,11 +86,12 @@ def test_handler_success(mock_time, mock_bedrock, mock_env, lambda_context, s3_e
8586
)
8687

8788

88-
@patch("app.handler.bedrock_agent")
89+
@patch("app.handler.get_bedrock_agent")
8990
@patch("time.time")
90-
def test_handler_multiple_files(mock_time, mock_bedrock, mock_env, lambda_context, multiple_s3_event):
91+
def test_handler_multiple_files(mock_time, mock_get_bedrock, mock_env, lambda_context, multiple_s3_event):
9192
"""Test handler with multiple S3 records"""
9293
mock_time.side_effect = [1000, 1001, 1002, 1003, 1004, 1005]
94+
mock_bedrock = mock_get_bedrock.return_value
9395
mock_bedrock.start_ingestion_job.return_value = {
9496
"ingestionJob": {"ingestionJobId": "job-123", "status": "STARTING"}
9597
}
@@ -103,15 +105,16 @@ def test_handler_multiple_files(mock_time, mock_bedrock, mock_env, lambda_contex
103105
assert mock_bedrock.start_ingestion_job.call_count == 2
104106

105107

106-
@patch("app.handler.bedrock_agent")
108+
@patch("app.handler.get_bedrock_agent")
107109
@patch("time.time")
108-
def test_handler_conflict_exception(mock_time, mock_bedrock, mock_env, lambda_context, s3_event):
110+
def test_handler_conflict_exception(mock_time, mock_get_bedrock, mock_env, lambda_context, s3_event):
109111
"""Test handler with ConflictException (job already running)"""
110112
mock_time.side_effect = [1000, 1001, 1002]
111113
error = ClientError(
112114
error_response={"Error": {"Code": "ConflictException", "Message": "Job already running"}},
113115
operation_name="StartIngestionJob",
114116
)
117+
mock_bedrock = mock_get_bedrock.return_value
115118
mock_bedrock.start_ingestion_job.side_effect = error
116119

117120
from app.handler import handler
@@ -122,15 +125,16 @@ def test_handler_conflict_exception(mock_time, mock_bedrock, mock_env, lambda_co
122125
assert "Files uploaded successfully - processing by existing ingestion job" in result["body"]
123126

124127

125-
@patch("app.handler.bedrock_agent")
128+
@patch("app.handler.get_bedrock_agent")
126129
@patch("time.time")
127-
def test_handler_aws_error(mock_time, mock_bedrock, mock_env, lambda_context, s3_event):
130+
def test_handler_aws_error(mock_time, mock_get_bedrock, mock_env, lambda_context, s3_event):
128131
"""Test handler with other AWS error"""
129132
mock_time.side_effect = [1000, 1001, 1002]
130133
error = ClientError(
131134
error_response={"Error": {"Code": "AccessDenied", "Message": "Access denied"}},
132135
operation_name="StartIngestionJob",
133136
)
137+
mock_bedrock = mock_get_bedrock.return_value
134138
mock_bedrock.start_ingestion_job.side_effect = error
135139

136140
from app.handler import handler
@@ -141,11 +145,12 @@ def test_handler_aws_error(mock_time, mock_bedrock, mock_env, lambda_context, s3
141145
assert "AWS error: AccessDenied - Access denied" in result["body"]
142146

143147

144-
@patch("app.handler.bedrock_agent")
148+
@patch("app.handler.get_bedrock_agent")
145149
@patch("time.time")
146-
def test_handler_unexpected_error(mock_time, mock_bedrock, mock_env, lambda_context, s3_event):
150+
def test_handler_unexpected_error(mock_time, mock_get_bedrock, mock_env, lambda_context, s3_event):
147151
"""Test handler with unexpected error"""
148-
mock_time.side_effect = [1000, 1001, 1002] # Added extra time call
152+
mock_time.side_effect = [1000, 1001, 1002]
153+
mock_bedrock = mock_get_bedrock.return_value
149154
mock_bedrock.start_ingestion_job.side_effect = Exception("Unexpected error")
150155

151156
from app.handler import handler

0 commit comments

Comments
 (0)