1212from werkzeug .routing import BaseConverter
1313
1414from samcli .commands .local .cli_common .durable_context import DurableContext
15- from samcli .commands .local .lib .exceptions import TenantIdValidationError , UnsupportedInlineCodeError
15+ from samcli .commands .local .lib .exceptions import (
16+ TenantIdValidationError ,
17+ UnsupportedInlineCodeError ,
18+ InvalidIntermediateImageError ,
19+ )
20+ from samcli .lib .providers .provider import Function
1621from samcli .lib .utils .invocation_type import EVENT
1722from samcli .lib .utils .name_utils import InvalidFunctionNameException , normalize_sam_function_identifier
1823from samcli .lib .utils .stream_writer import StreamWriter
@@ -244,42 +249,41 @@ def _invoke_request_handler(self, function_name):
244249 # Extract durable execution name from headers
245250 durable_execution_name = flask_request .headers .get ("X-Amz-Durable-Execution-Name" )
246251
252+ headers = {"Content-Type" : "application/json" }
253+
254+ try :
255+ function = self .lambda_runner .get_function (function_name , tenant_id )
256+ except (InvalidFunctionNameException , TenantIdValidationError , InvalidIntermediateImageError ) as e :
257+ LOG .error ("Validation error: %s" , str (e ))
258+ return LambdaErrorResponses .validation_exception (str (e ))
259+ except FunctionNotFound :
260+ normalized_function_name = normalize_sam_function_identifier (function_name )
261+ LOG .debug ("%s was not found to invoke." , normalized_function_name )
262+ return LambdaErrorResponses .resource_not_found (normalized_function_name )
263+ except UnsupportedInlineCodeError :
264+ return LambdaErrorResponses .not_implemented_locally (
265+ "Inline code is not supported for sam local commands. Please write your code in a separate file."
266+ )
267+
247268 arguments = {
248269 "function_name" : function_name ,
249270 "request_data" : request_data ,
250271 "invocation_type" : invocation_type ,
251272 "durable_execution_name" : durable_execution_name ,
252273 "tenant_id" : tenant_id ,
274+ "function" : function ,
253275 }
254276
255- headers = {"Content-Type" : "application/json" }
256-
257277 if invocation_type == EVENT :
258- # Validate function exists before submitting async task
259- if validation_error := self ._validate_function_for_invocation (function_name ):
260- return validation_error
261-
262- # LocalLambdaService is blocking, so threads used by ThreadPoolExecutor
263- # will be cleaned up when Python exits by the ThreadPoolExecutor implementation
264278 self .executor .submit (self ._invoke_async_lambda , ** arguments )
265279 return self .service_response ("" , headers , 202 )
266-
267280 try :
268281 invoke_headers , stdout_stream_string , stdout_stream_bytes = self ._invoke_lambda (** arguments )
269- except (InvalidFunctionNameException , TenantIdValidationError ) as e :
270- LOG .error ("Validation error: %s" , str (e ))
271- return LambdaErrorResponses .validation_exception (str (e ))
272282 except UnsupportedInvocationType as e :
273283 LOG .warning (
274- "invocation-type: %s is not supported. Event and RequestResponse are only supported." , invocation_type
284+ "invocation-type: %s is not supported. Only Event and RequestResponse are supported." , invocation_type
275285 )
276286 return LambdaErrorResponses .not_implemented_locally (str (e ))
277- except FunctionNotFound :
278- return self ._handle_function_not_found (function_name )
279- except UnsupportedInlineCodeError :
280- return LambdaErrorResponses .not_implemented_locally (
281- "Inline code is not supported for sam local commands. Please write your code in a separate file."
282- )
283287 except DockerContainerCreationFailedException as ex :
284288 return LambdaErrorResponses .container_creation_failed (ex .message )
285289
@@ -296,47 +300,6 @@ def _invoke_request_handler(self, function_name):
296300
297301 return self .service_response (lambda_response , headers , 200 )
298302
299- def _validate_function_for_invocation (self , function_name : str ) -> Optional [Response ]:
300- """
301- Validates that a function exists and can be invoked.
302-
303- Parameters
304- ----------
305- function_name : str
306- Name or ARN of the function to validate
307-
308- Returns
309- -------
310- Flask.Response or None
311- Error response if validation fails, None if validation succeeds
312- """
313- try :
314- self .lambda_runner .get_function (function_name )
315- return None
316- except FunctionNotFound :
317- return self ._handle_function_not_found (function_name )
318- except InvalidFunctionNameException as e :
319- LOG .error ("Validation error: %s" , str (e ))
320- return LambdaErrorResponses .validation_exception (str (e ))
321-
322- def _handle_function_not_found (self , function_name : str ) -> Response :
323- """
324- Handles FunctionNotFound exception by returning appropriate error response.
325-
326- Parameters
327- ----------
328- function_name : str
329- Name or ARN of the function that was not found
330-
331- Returns
332- -------
333- Flask.Response
334- Error response for function not found
335- """
336- normalized_function_name = normalize_sam_function_identifier (function_name )
337- LOG .debug ("%s was not found to invoke." , normalized_function_name )
338- return LambdaErrorResponses .resource_not_found (normalized_function_name )
339-
340303 def _invoke_async_lambda (self , function_name : str , ** kwargs ) -> None :
341304 """
342305 Wrapper for _invoke_lambda that runs in an async context (Event invocation type)
@@ -353,6 +316,7 @@ def _invoke_lambda(
353316 invocation_type : str ,
354317 durable_execution_name : Optional [str ],
355318 tenant_id : Optional [str ],
319+ function : Optional [Function ],
356320 ) -> Tuple [Optional [Dict [str , str ]], io .StringIO , io .BytesIO ]:
357321 """
358322 Invokes a Lambda function and returns the result
@@ -370,6 +334,7 @@ def _invoke_lambda(
370334 invocation_type = invocation_type ,
371335 durable_execution_name = durable_execution_name ,
372336 tenant_id = tenant_id ,
337+ function = function ,
373338 stdout = stdout_stream_writer ,
374339 stderr = self .stderr ,
375340 )
0 commit comments