-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinvoker.py
More file actions
322 lines (277 loc) · 12.7 KB
/
invoker.py
File metadata and controls
322 lines (277 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from __future__ import annotations
import json
from dataclasses import dataclass
from threading import Lock
from typing import TYPE_CHECKING, Any, Protocol
from uuid import uuid4
import boto3 # type: ignore
from aws_durable_execution_sdk_python.execution import (
DurableExecutionInvocationInput,
DurableExecutionInvocationInputWithClient,
DurableExecutionInvocationOutput,
InitialExecutionState,
)
from aws_durable_execution_sdk_python_testing.exceptions import (
DurableFunctionsTestError,
InvalidParameterValueException,
ResourceNotFoundException,
)
from aws_durable_execution_sdk_python_testing.model import LambdaContext
if TYPE_CHECKING:
from collections.abc import Callable
from aws_durable_execution_sdk_python_testing.client import InMemoryServiceClient
from aws_durable_execution_sdk_python_testing.execution import Execution
@dataclass(frozen=True)
class InvokeResponse:
"""Response from invoking a durable function."""
invocation_output: DurableExecutionInvocationOutput
request_id: str
def create_test_lambda_context() -> LambdaContext:
# Create client context as a dictionary, not as objects
# LambdaContext.__init__ expects dictionaries and will create the objects internally
client_context_dict = {
"custom": {"test_key": "test_value"},
"env": {"platform": "test", "make": "test", "model": "test"},
"client": {
"installation_id": "test-installation-123",
"app_title": "TestApp",
"app_version_name": "1.0.0",
"app_version_code": "100",
"app_package_name": "com.test.app",
},
}
cognito_identity_dict = {
"cognitoIdentityId": "test-cognito-identity-123",
"cognitoIdentityPoolId": "us-west-2:test-pool-456",
}
return LambdaContext(
aws_request_id="test-invoke-12345",
client_context=client_context_dict,
identity=cognito_identity_dict,
invoked_function_arn="arn:aws:lambda:us-west-2:123456789012:function:test-function",
tenant_id="test-tenant-789",
)
class Invoker(Protocol):
def create_invocation_input(
self, execution: Execution
) -> DurableExecutionInvocationInput: ... # pragma: no cover
def invoke(
self,
function_name: str,
input: DurableExecutionInvocationInput,
endpoint_url: str | None = None,
) -> InvokeResponse: ... # pragma: no cover
def update_endpoint(
self, endpoint_url: str, region_name: str
) -> None: ... # pragma: no cover
class InProcessInvoker(Invoker):
def __init__(self, handler: Callable, service_client: InMemoryServiceClient):
self.handler = handler
self.service_client = service_client
def create_invocation_input(
self, execution: Execution
) -> DurableExecutionInvocationInput:
return DurableExecutionInvocationInputWithClient(
durable_execution_arn=execution.durable_execution_arn,
# TODO: this needs better logic - use existing if not used yet, vs create new
checkpoint_token=execution.get_new_checkpoint_token(),
initial_execution_state=InitialExecutionState(
operations=execution.operations,
next_marker="",
),
service_client=self.service_client,
)
def invoke(
self,
function_name: str, # noqa: ARG002
input: DurableExecutionInvocationInput,
endpoint_url: str | None = None, # noqa: ARG002
) -> InvokeResponse:
# TODO: reasses if function_name will be used in future
input_with_client = DurableExecutionInvocationInputWithClient.from_durable_execution_invocation_input(
input, self.service_client
)
context = create_test_lambda_context()
response_dict = self.handler(input_with_client, context)
output = DurableExecutionInvocationOutput.from_dict(response_dict)
return InvokeResponse(
invocation_output=output, request_id=context.aws_request_id
)
def update_endpoint(self, endpoint_url: str, region_name: str) -> None:
"""No-op for in-process invoker."""
class LambdaInvoker(Invoker):
def __init__(self, lambda_client: Any) -> None:
self.lambda_client = lambda_client
# Maps execution_arn -> endpoint for that execution
# Maps endpoint -> client to reuse clients across executions
self._execution_endpoints: dict[str, str] = {}
self._endpoint_clients: dict[str, Any] = {}
self._current_endpoint: str = "" # Track current endpoint for new executions
self._lock = Lock()
@staticmethod
def create(endpoint_url: str, region_name: str) -> LambdaInvoker:
"""Create with the boto lambda client."""
invoker = LambdaInvoker(
boto3.client("lambda", endpoint_url=endpoint_url, region_name=region_name)
)
invoker._current_endpoint = endpoint_url
invoker._endpoint_clients[endpoint_url] = invoker.lambda_client
return invoker
def update_endpoint(self, endpoint_url: str, region_name: str) -> None:
"""Update the Lambda client endpoint."""
# Cache client by endpoint to reuse across executions
with self._lock:
if endpoint_url not in self._endpoint_clients:
self._endpoint_clients[endpoint_url] = boto3.client(
"lambda", endpoint_url=endpoint_url, region_name=region_name
)
self.lambda_client = self._endpoint_clients[endpoint_url]
self._current_endpoint = endpoint_url
def _get_client_for_execution(
self,
durable_execution_arn: str,
lambda_endpoint: str | None = None,
region_name: str | None = None,
) -> Any:
"""Get the appropriate client for this execution."""
# Use provided endpoint or fall back to cached endpoint for this execution
if lambda_endpoint:
if lambda_endpoint not in self._endpoint_clients:
self._endpoint_clients[lambda_endpoint] = boto3.client(
"lambda",
endpoint_url=lambda_endpoint,
region_name=region_name or "us-east-1",
)
return self._endpoint_clients[lambda_endpoint]
# Fallback to cached endpoint
if durable_execution_arn not in self._execution_endpoints:
with self._lock:
if durable_execution_arn not in self._execution_endpoints:
self._execution_endpoints[durable_execution_arn] = (
self._current_endpoint
)
endpoint = self._execution_endpoints[durable_execution_arn]
# If no endpoint configured, fall back to default client
if not endpoint:
return self.lambda_client
return self._endpoint_clients[endpoint]
def create_invocation_input(
self, execution: Execution
) -> DurableExecutionInvocationInput:
return DurableExecutionInvocationInput(
durable_execution_arn=execution.durable_execution_arn,
checkpoint_token=execution.get_new_checkpoint_token(),
initial_execution_state=InitialExecutionState(
operations=execution.operations,
next_marker="",
),
)
def invoke(
self,
function_name: str,
input: DurableExecutionInvocationInput,
endpoint_url: str | None = None,
) -> InvokeResponse:
"""Invoke AWS Lambda function and return durable execution result.
Args:
function_name: Name of the Lambda function to invoke
input: Durable execution invocation input
endpoint_url: Lambda endpoint url
Returns:
InvokeResponse: Response containing invocation output and request ID
Raises:
ResourceNotFoundException: If function does not exist
InvalidParameterValueException: If parameters are invalid
DurableFunctionsTestError: For other invocation failures
"""
# Parameter validation
if not function_name or not function_name.strip():
msg = "Function name is required"
raise InvalidParameterValueException(msg)
# Get the client for this execution
client = self._get_client_for_execution(
input.durable_execution_arn, endpoint_url
)
try:
# Invoke AWS Lambda function using standard invoke method
response = client.invoke(
FunctionName=function_name,
InvocationType="RequestResponse", # Synchronous invocation
Payload=json.dumps(input.to_json_dict()),
)
# Check HTTP status code
status_code = response.get("StatusCode")
if status_code not in (200, 202, 204):
msg = f"Lambda invocation failed with status code: {status_code}"
raise DurableFunctionsTestError(msg)
# Check for function errors
if "FunctionError" in response:
error_payload = response["Payload"].read().decode("utf-8")
msg = f"Lambda invocation failed with status {status_code}: {error_payload}"
raise DurableFunctionsTestError(msg)
# Parse response payload
response_payload = response["Payload"].read().decode("utf-8")
response_dict = json.loads(response_payload)
# Extract request ID from response headers (x-amzn-RequestId or x-amzn-request-id)
headers = response.get("ResponseMetadata", {}).get("HTTPHeaders", {})
request_id = (
headers.get("x-amzn-RequestId")
or headers.get("x-amzn-request-id")
or f"local-{uuid4()}"
)
# Convert to DurableExecutionInvocationOutput
output = DurableExecutionInvocationOutput.from_dict(response_dict)
return InvokeResponse(invocation_output=output, request_id=request_id)
except client.exceptions.ResourceNotFoundException as e:
msg = f"Function not found: {function_name}"
raise ResourceNotFoundException(msg) from e
except client.exceptions.InvalidParameterValueException as e:
msg = f"Invalid parameter: {e}"
raise InvalidParameterValueException(msg) from e
except (
client.exceptions.TooManyRequestsException,
client.exceptions.ServiceException,
client.exceptions.ResourceConflictException,
client.exceptions.InvalidRequestContentException,
client.exceptions.RequestTooLargeException,
client.exceptions.UnsupportedMediaTypeException,
client.exceptions.InvalidRuntimeException,
client.exceptions.InvalidZipFileException,
client.exceptions.ResourceNotReadyException,
client.exceptions.SnapStartTimeoutException,
client.exceptions.SnapStartNotReadyException,
client.exceptions.SnapStartException,
client.exceptions.RecursiveInvocationException,
) as e:
msg = f"Lambda invocation failed: {e}"
raise DurableFunctionsTestError(msg) from e
except (
client.exceptions.InvalidSecurityGroupIDException,
client.exceptions.EC2ThrottledException,
client.exceptions.EFSMountConnectivityException,
client.exceptions.SubnetIPAddressLimitReachedException,
client.exceptions.EC2UnexpectedException,
client.exceptions.InvalidSubnetIDException,
client.exceptions.EC2AccessDeniedException,
client.exceptions.EFSIOException,
client.exceptions.ENILimitReachedException,
client.exceptions.EFSMountTimeoutException,
client.exceptions.EFSMountFailureException,
) as e:
msg = f"Lambda infrastructure error: {e}"
raise DurableFunctionsTestError(msg) from e
except (
client.exceptions.KMSAccessDeniedException,
client.exceptions.KMSDisabledException,
client.exceptions.KMSNotFoundException,
client.exceptions.KMSInvalidStateException,
) as e:
msg = f"Lambda KMS error: {e}"
raise DurableFunctionsTestError(msg) from e
except Exception as e:
# Handle any remaining exceptions, including custom ones like DurableExecutionAlreadyStartedException
if "DurableExecutionAlreadyStartedException" in str(type(e)):
msg = f"Durable execution already started: {e}"
raise DurableFunctionsTestError(msg) from e
msg = f"Unexpected error during Lambda invocation: {e}"
raise DurableFunctionsTestError(msg) from e