@@ -14,10 +14,45 @@ def get_lambda_response(
14
14
lambda_arn : str ,
15
15
payload : Optional [str ] = None ,
16
16
client : Optional [LambdaClient ] = None ,
17
+ raise_on_error : bool = True ,
17
18
) -> Tuple [InvocationResponseTypeDef , datetime ]:
19
+ """Invoke function synchronously
20
+
21
+ Parameters
22
+ ----------
23
+ lambda_arn : str
24
+ Lambda function ARN to invoke
25
+ payload : Optional[str], optional
26
+ JSON payload for Lambda invocation, by default None
27
+ client : Optional[LambdaClient], optional
28
+ Boto3 Lambda SDK client, by default None
29
+ raise_on_error : bool, optional
30
+ Whether to raise exception upon invocation error, by default True
31
+
32
+ Returns
33
+ -------
34
+ Tuple[InvocationResponseTypeDef, datetime]
35
+ Function response and approximate execution time
36
+
37
+ Raises
38
+ ------
39
+ RuntimeError
40
+ Function invocation error details
41
+ """
18
42
client = client or boto3 .client ("lambda" )
19
43
payload = payload or ""
20
44
execution_time = datetime .utcnow ()
45
+
46
+ response : InvocationResponseTypeDef = client .invoke (
47
+ FunctionName = lambda_arn ,
48
+ InvocationType = "RequestResponse" ,
49
+ Payload = payload ,
50
+ )
51
+ has_error = response .get ("FunctionError" , "" ) == "Unhandled"
52
+ if has_error and raise_on_error :
53
+ error_payload = response ["Payload" ].read ().decode ()
54
+ raise RuntimeError (f"Function failed invocation: { error_payload } " )
55
+
21
56
return client .invoke (FunctionName = lambda_arn , InvocationType = "RequestResponse" , Payload = payload ), execution_time
22
57
23
58
0 commit comments