Skip to content

Commit

Permalink
feat: New param to remove memory limit in invoke (#7892)
Browse files Browse the repository at this point in the history
* feat: New param to remove memory limit in invoke

`--no-memory-limit` removes the memory limitation in the container
during local invoke, even when there's a memory configured in the
SAM template.

* Add `--no-memory-limit` to start-lambda and start-api
  • Loading branch information
valerena authored Feb 15, 2025
1 parent 462b4e0 commit bbcd952
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 13 deletions.
13 changes: 13 additions & 0 deletions samcli/commands/_utils/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,19 @@ def mount_symlinks_option(f):
return mount_symlinks_click_option()(f)


def no_memory_limit_click_option():
return click.option(
"--no-memory-limit",
default=False,
is_flag=True,
help="Remove the Memory limit during emulation. This runs the container without the --memory parameter",
)


def no_memory_limit_option(f):
return no_memory_limit_click_option()(f)


def terraform_plan_file_callback(ctx, param, provided_value):
"""
Callback for --terraform-plan-file to check if --hook-name is also specified
Expand Down
8 changes: 7 additions & 1 deletion samcli/commands/local/cli_common/invoke_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def __init__(
add_host: Optional[dict] = None,
invoke_images: Optional[str] = None,
mount_symlinks: Optional[bool] = False,
no_mem_limit: Optional[bool] = False,
) -> None:
"""
Initialize the context
Expand Down Expand Up @@ -202,6 +203,7 @@ def __init__(
self._debug_function = debug_function

self._mount_symlinks: Optional[bool] = mount_symlinks
self._no_mem_limit = no_mem_limit

# Note(xinhol): despite self._function_provider and self._stacks are initialized as None
# they will be assigned with a non-None value in __enter__() and
Expand Down Expand Up @@ -412,9 +414,13 @@ def lambda_runtime(self) -> LambdaRuntime:
self._container_manager,
image_builder,
mount_symlinks=self._mount_symlinks,
no_mem_limit=self._no_mem_limit,
),
ContainersMode.COLD: LambdaRuntime(
self._container_manager, image_builder, mount_symlinks=self._mount_symlinks
self._container_manager,
image_builder,
mount_symlinks=self._mount_symlinks,
no_mem_limit=self._no_mem_limit,
),
}
return self._lambda_runtimes[self._containers_mode]
Expand Down
7 changes: 7 additions & 0 deletions samcli/commands/local/cli_common/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ def local_common_options(f):
"If a function does not have invoke image specified, the default AWS SAM CLI "
"emulation image will be used.",
),
click.option(
"--no-memory-limit",
default=False,
is_flag=True,
help="Removes the Memory limit during emulation. "
"With this parameter, the underlying container will run without a --memory parameter",
),
]

# Reverse the list to maintain ordering of options in help text printed with --help
Expand Down
4 changes: 4 additions & 0 deletions samcli/commands/local/invoke/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def cli(
skip_prepare_infra,
terraform_plan_file,
mount_symlinks,
no_memory_limit,
):
"""
`sam local invoke` command entry point
Expand Down Expand Up @@ -137,6 +138,7 @@ def cli(
invoke_image,
hook_name,
mount_symlinks,
no_memory_limit,
) # pragma: no cover


Expand Down Expand Up @@ -165,6 +167,7 @@ def do_cli( # pylint: disable=R0914
invoke_image,
hook_name,
mount_symlinks,
no_mem_limit,
):
"""
Implementation of the ``cli`` method, just separated out for unit testing purposes
Expand Down Expand Up @@ -214,6 +217,7 @@ def do_cli( # pylint: disable=R0914
add_host=add_host,
invoke_images=processed_invoke_images,
mount_symlinks=mount_symlinks,
no_mem_limit=no_mem_limit,
) as context:
# Invoke the function
context.local_lambda_runner.invoke(
Expand Down
1 change: 1 addition & 0 deletions samcli/commands/local/invoke/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"add_host",
"invoke_image",
"mount_symlinks",
"no_memory_limit",
]

CONFIGURATION_OPTION_NAMES: List[str] = ["config_env", "config_file"] + SAVE_PARAMS_OPTIONS
Expand Down
4 changes: 4 additions & 0 deletions samcli/commands/local/start_api/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def cli(
terraform_plan_file,
ssl_cert_file,
ssl_key_file,
no_memory_limit,
):
"""
`sam local start-api` command entry point
Expand Down Expand Up @@ -176,6 +177,7 @@ def cli(
hook_name,
ssl_cert_file,
ssl_key_file,
no_memory_limit,
) # pragma: no cover


Expand Down Expand Up @@ -208,6 +210,7 @@ def do_cli( # pylint: disable=R0914
hook_name,
ssl_cert_file,
ssl_key_file,
no_mem_limit,
):
"""
Implementation of the ``cli`` method, just separated out for unit testing purposes
Expand Down Expand Up @@ -253,6 +256,7 @@ def do_cli( # pylint: disable=R0914
container_host_interface=container_host_interface,
invoke_images=processed_invoke_images,
add_host=add_host,
no_mem_limit=no_mem_limit,
) as invoke_context:
ssl_context = (ssl_cert_file, ssl_key_file) if ssl_cert_file else None
service = LocalApiService(
Expand Down
1 change: 1 addition & 0 deletions samcli/commands/local/start_api/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"skip_pull_image",
"docker_network",
"force_image_build",
"no_memory_limit",
"warm_containers",
"shutdown",
"container_host",
Expand Down
4 changes: 4 additions & 0 deletions samcli/commands/local/start_lambda/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def cli(
hook_name,
skip_prepare_infra,
terraform_plan_file,
no_memory_limit,
):
"""
`sam local start-lambda` command entry point
Expand Down Expand Up @@ -132,6 +133,7 @@ def cli(
add_host,
invoke_image,
hook_name,
no_memory_limit,
) # pragma: no cover


Expand Down Expand Up @@ -160,6 +162,7 @@ def do_cli( # pylint: disable=R0914
add_host,
invoke_image,
hook_name,
no_mem_limit,
):
"""
Implementation of the ``cli`` method, just separated out for unit testing purposes
Expand Down Expand Up @@ -205,6 +208,7 @@ def do_cli( # pylint: disable=R0914
container_host_interface=container_host_interface,
add_host=add_host,
invoke_images=processed_invoke_images,
no_mem_limit=no_mem_limit,
) as invoke_context:
service = LocalLambdaService(lambda_invoke_context=invoke_context, port=port, host=host)
service.start()
Expand Down
1 change: 1 addition & 0 deletions samcli/commands/local/start_lambda/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"container_host_interface",
"add_host",
"invoke_image",
"no_memory_limit",
]

ARTIFACT_LOCATION_OPTIONS: List[str] = [
Expand Down
9 changes: 5 additions & 4 deletions samcli/local/lambdafn/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class LambdaRuntime:

SUPPORTED_ARCHIVE_EXTENSIONS = (".zip", ".jar", ".ZIP", ".JAR")

def __init__(self, container_manager, image_builder, mount_symlinks=False):
def __init__(self, container_manager, image_builder, mount_symlinks=False, no_mem_limit=False):
"""
Initialize the Local Lambda runtime
Expand All @@ -53,6 +53,7 @@ def __init__(self, container_manager, image_builder, mount_symlinks=False):
self._temp_uncompressed_paths_to_be_cleaned = []
self._lock = threading.Lock()
self._mount_symlinks = mount_symlinks
self._no_mem_limit = no_mem_limit

def create(
self, function_config, debug_context=None, container_host=None, container_host_interface=None, extra_hosts=None
Expand Down Expand Up @@ -106,7 +107,7 @@ def create(
layers,
self._image_builder,
function_config.architecture,
memory_mb=function_config.memory,
memory_mb=(None if self._no_mem_limit else function_config.memory),
env_vars=env_vars,
debug_options=debug_context,
container_host=container_host,
Expand Down Expand Up @@ -394,7 +395,7 @@ class WarmLambdaRuntime(LambdaRuntime):
warm containers life cycle.
"""

def __init__(self, container_manager, image_builder, observer=None, mount_symlinks=False):
def __init__(self, container_manager, image_builder, observer=None, mount_symlinks=False, no_mem_limit=False):
"""
Initialize the Local Lambda runtime
Expand All @@ -412,7 +413,7 @@ def __init__(self, container_manager, image_builder, observer=None, mount_symlin

self._observer = observer if observer else LambdaFunctionObserver(self._on_code_change)

super().__init__(container_manager, image_builder, mount_symlinks=mount_symlinks)
super().__init__(container_manager, image_builder, mount_symlinks=mount_symlinks, no_mem_limit=no_mem_limit)

def create(
self, function_config, debug_context=None, container_host=None, container_host_interface=None, extra_hosts=None
Expand Down
Loading

0 comments on commit bbcd952

Please sign in to comment.