@@ -98,6 +98,7 @@ def __init__(
9898 invoke_images : Optional [str ] = None ,
9999 mount_symlinks : Optional [bool ] = False ,
100100 no_mem_limit : Optional [bool ] = False ,
101+ function_logical_ids : Optional [Tuple [str , ...]] = None ,
101102 ) -> None :
102103 """
103104 Initialize the context
@@ -107,7 +108,7 @@ def __init__(
107108 template_file str
108109 Name or path to template
109110 function_identifier str
110- Identifier of the function to invoke
111+ Identifier of the single function to invoke (used by 'sam local invoke' command)
111112 env_vars_file str
112113 Path to a file containing values for environment variables
113114 docker_volume_basedir str
@@ -154,10 +155,27 @@ def __init__(
154155 Optional. A dictionary that defines the custom invoke image URI of each function
155156 mount_symlinks bool
156157 Optional. Indicates if symlinks should be mounted inside the container
158+ function_logical_ids tuple(str)
159+ Optional. Tuple of function logical IDs to filter and make available for local execution.
160+ Used by 'sam local start-api' and 'sam local start-lambda' commands to limit which
161+ functions from the template are exposed. If not provided, all functions are available.
157162 """
158163
159164 self ._template_file = template_file
160165 self ._function_identifier = function_identifier
166+ self ._function_logical_ids = function_logical_ids
167+
168+ # Validate that function_identifier and function_logical_ids aren't both provided
169+ # function_identifier is for 'sam local invoke' (single function)
170+ # function_logical_ids is for 'sam local start-*' commands (multiple function filter)
171+ if self ._function_identifier and self ._function_logical_ids :
172+ LOG .warning (
173+ "Both function_identifier and function_logical_ids were provided. "
174+ "function_identifier is used for 'sam local invoke' to specify a single function, "
175+ "while function_logical_ids is used for 'sam local start-api/start-lambda' to filter functions. "
176+ "function_identifier will take precedence for single function invocation."
177+ )
178+
161179 self ._env_vars_file = env_vars_file
162180 self ._docker_volume_basedir = docker_volume_basedir
163181 self ._docker_network = docker_network
@@ -241,10 +259,18 @@ def __enter__(self) -> "InvokeContext":
241259 if self ._docker_volume_basedir :
242260 _function_providers_args [self ._containers_mode ].append (True )
243261
262+ # Add function_logical_ids parameter to both provider types
263+ _function_providers_kwargs : Dict [str , Any ] = {}
264+ if self ._function_logical_ids is not None :
265+ _function_providers_kwargs ["function_logical_ids" ] = self ._function_logical_ids
266+
244267 self ._function_provider = _function_providers_class [self ._containers_mode ](
245- * _function_providers_args [self ._containers_mode ]
268+ * _function_providers_args [self ._containers_mode ], ** _function_providers_kwargs
246269 )
247270
271+ # Validate function logical IDs after provider is initialized
272+ self ._validate_function_logical_ids ()
273+
248274 self ._env_vars_value = self ._get_env_vars_value (self ._env_vars_file )
249275 self ._container_env_vars_value = self ._get_env_vars_value (self ._container_env_vars_file )
250276 self ._log_file_handle = self ._setup_log_file (self ._log_file )
@@ -448,6 +474,34 @@ def _clean_running_containers_and_related_resources(self) -> None:
448474 cast (WarmLambdaRuntime , self .lambda_runtime ).clean_running_containers_and_related_resources ()
449475 cast (RefreshableSamFunctionProvider , self ._function_provider ).stop_observer ()
450476
477+ def _validate_function_logical_ids (self ) -> None :
478+ """
479+ Validates that all provided function logical IDs exist in the template.
480+ Raises InvalidFunctionNamesException with helpful error message if validation fails.
481+ """
482+ if not self ._function_logical_ids :
483+ return # No filtering requested
484+
485+ # Get all available function names/IDs from the provider
486+ all_functions_set = set ()
487+ for func in self ._function_provider .get_all ():
488+ all_functions_set .add (func .name )
489+ all_functions_set .add (func .function_id )
490+ if func .functionname :
491+ all_functions_set .add (func .functionname )
492+
493+ # Check for invalid IDs
494+ invalid_ids = set (self ._function_logical_ids ) - all_functions_set
495+
496+ if invalid_ids :
497+ available_ids = sorted (all_functions_set )
498+ from samcli .commands .local .cli_common .user_exceptions import InvalidFunctionNamesException
499+
500+ raise InvalidFunctionNamesException (
501+ f"Invalid function logical ID(s): { ', ' .join (sorted (invalid_ids ))} \n \n "
502+ f"Available functions in template:\n " + "\n " .join (available_ids )
503+ )
504+
451505 def _add_account_id_to_global (self ) -> None :
452506 """
453507 Attempts to get the Account ID from the current session
@@ -556,6 +610,21 @@ def initialized_container_ids(self) -> List[str]:
556610 """
557611 return self ._initialized_container_ids
558612
613+ @property
614+ def function_logical_ids (self ) -> Optional [Tuple [str , ...]]:
615+ """
616+ Returns the tuple of function logical IDs to filter, if provided.
617+
618+ This is used by 'sam local start-api' and 'sam local start-lambda' commands
619+ to limit which functions from the template are made available for local execution.
620+ This is different from function_identifier which is used by 'sam local invoke'
621+ to specify a single function to invoke.
622+
623+ Returns:
624+ Optional[Tuple[str, ...]]: Tuple of function logical IDs or None if no filter
625+ """
626+ return self ._function_logical_ids
627+
559628 @property
560629 def stdout (self ) -> StreamWriter :
561630 """
0 commit comments