Skip to content

Commit bbcd952

Browse files
authored
feat: New param to remove memory limit in invoke (#7892)
* 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
1 parent 462b4e0 commit bbcd952

File tree

17 files changed

+154
-13
lines changed

17 files changed

+154
-13
lines changed

samcli/commands/_utils/options.py

+13
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,19 @@ def mount_symlinks_option(f):
877877
return mount_symlinks_click_option()(f)
878878

879879

880+
def no_memory_limit_click_option():
881+
return click.option(
882+
"--no-memory-limit",
883+
default=False,
884+
is_flag=True,
885+
help="Remove the Memory limit during emulation. This runs the container without the --memory parameter",
886+
)
887+
888+
889+
def no_memory_limit_option(f):
890+
return no_memory_limit_click_option()(f)
891+
892+
880893
def terraform_plan_file_callback(ctx, param, provided_value):
881894
"""
882895
Callback for --terraform-plan-file to check if --hook-name is also specified

samcli/commands/local/cli_common/invoke_context.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def __init__(
102102
add_host: Optional[dict] = None,
103103
invoke_images: Optional[str] = None,
104104
mount_symlinks: Optional[bool] = False,
105+
no_mem_limit: Optional[bool] = False,
105106
) -> None:
106107
"""
107108
Initialize the context
@@ -202,6 +203,7 @@ def __init__(
202203
self._debug_function = debug_function
203204

204205
self._mount_symlinks: Optional[bool] = mount_symlinks
206+
self._no_mem_limit = no_mem_limit
205207

206208
# Note(xinhol): despite self._function_provider and self._stacks are initialized as None
207209
# they will be assigned with a non-None value in __enter__() and
@@ -412,9 +414,13 @@ def lambda_runtime(self) -> LambdaRuntime:
412414
self._container_manager,
413415
image_builder,
414416
mount_symlinks=self._mount_symlinks,
417+
no_mem_limit=self._no_mem_limit,
415418
),
416419
ContainersMode.COLD: LambdaRuntime(
417-
self._container_manager, image_builder, mount_symlinks=self._mount_symlinks
420+
self._container_manager,
421+
image_builder,
422+
mount_symlinks=self._mount_symlinks,
423+
no_mem_limit=self._no_mem_limit,
418424
),
419425
}
420426
return self._lambda_runtimes[self._containers_mode]

samcli/commands/local/cli_common/options.py

+7
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ def local_common_options(f):
9898
"If a function does not have invoke image specified, the default AWS SAM CLI "
9999
"emulation image will be used.",
100100
),
101+
click.option(
102+
"--no-memory-limit",
103+
default=False,
104+
is_flag=True,
105+
help="Removes the Memory limit during emulation. "
106+
"With this parameter, the underlying container will run without a --memory parameter",
107+
),
101108
]
102109

103110
# Reverse the list to maintain ordering of options in help text printed with --help

samcli/commands/local/invoke/cli.py

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def cli(
106106
skip_prepare_infra,
107107
terraform_plan_file,
108108
mount_symlinks,
109+
no_memory_limit,
109110
):
110111
"""
111112
`sam local invoke` command entry point
@@ -137,6 +138,7 @@ def cli(
137138
invoke_image,
138139
hook_name,
139140
mount_symlinks,
141+
no_memory_limit,
140142
) # pragma: no cover
141143

142144

@@ -165,6 +167,7 @@ def do_cli( # pylint: disable=R0914
165167
invoke_image,
166168
hook_name,
167169
mount_symlinks,
170+
no_mem_limit,
168171
):
169172
"""
170173
Implementation of the ``cli`` method, just separated out for unit testing purposes
@@ -214,6 +217,7 @@ def do_cli( # pylint: disable=R0914
214217
add_host=add_host,
215218
invoke_images=processed_invoke_images,
216219
mount_symlinks=mount_symlinks,
220+
no_mem_limit=no_mem_limit,
217221
) as context:
218222
# Invoke the function
219223
context.local_lambda_runner.invoke(

samcli/commands/local/invoke/core/options.py

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"add_host",
3737
"invoke_image",
3838
"mount_symlinks",
39+
"no_memory_limit",
3940
]
4041

4142
CONFIGURATION_OPTION_NAMES: List[str] = ["config_env", "config_file"] + SAVE_PARAMS_OPTIONS

samcli/commands/local/start_api/cli.py

+4
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ def cli(
141141
terraform_plan_file,
142142
ssl_cert_file,
143143
ssl_key_file,
144+
no_memory_limit,
144145
):
145146
"""
146147
`sam local start-api` command entry point
@@ -176,6 +177,7 @@ def cli(
176177
hook_name,
177178
ssl_cert_file,
178179
ssl_key_file,
180+
no_memory_limit,
179181
) # pragma: no cover
180182

181183

@@ -208,6 +210,7 @@ def do_cli( # pylint: disable=R0914
208210
hook_name,
209211
ssl_cert_file,
210212
ssl_key_file,
213+
no_mem_limit,
211214
):
212215
"""
213216
Implementation of the ``cli`` method, just separated out for unit testing purposes
@@ -253,6 +256,7 @@ def do_cli( # pylint: disable=R0914
253256
container_host_interface=container_host_interface,
254257
invoke_images=processed_invoke_images,
255258
add_host=add_host,
259+
no_mem_limit=no_mem_limit,
256260
) as invoke_context:
257261
ssl_context = (ssl_cert_file, ssl_key_file) if ssl_cert_file else None
258262
service = LocalApiService(

samcli/commands/local/start_api/core/options.py

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"skip_pull_image",
3636
"docker_network",
3737
"force_image_build",
38+
"no_memory_limit",
3839
"warm_containers",
3940
"shutdown",
4041
"container_host",

samcli/commands/local/start_lambda/cli.py

+4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def cli(
101101
hook_name,
102102
skip_prepare_infra,
103103
terraform_plan_file,
104+
no_memory_limit,
104105
):
105106
"""
106107
`sam local start-lambda` command entry point
@@ -132,6 +133,7 @@ def cli(
132133
add_host,
133134
invoke_image,
134135
hook_name,
136+
no_memory_limit,
135137
) # pragma: no cover
136138

137139

@@ -160,6 +162,7 @@ def do_cli( # pylint: disable=R0914
160162
add_host,
161163
invoke_image,
162164
hook_name,
165+
no_mem_limit,
163166
):
164167
"""
165168
Implementation of the ``cli`` method, just separated out for unit testing purposes
@@ -205,6 +208,7 @@ def do_cli( # pylint: disable=R0914
205208
container_host_interface=container_host_interface,
206209
add_host=add_host,
207210
invoke_images=processed_invoke_images,
211+
no_mem_limit=no_mem_limit,
208212
) as invoke_context:
209213
service = LocalLambdaService(lambda_invoke_context=invoke_context, port=port, host=host)
210214
service.start()

samcli/commands/local/start_lambda/core/options.py

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"container_host_interface",
3838
"add_host",
3939
"invoke_image",
40+
"no_memory_limit",
4041
]
4142

4243
ARTIFACT_LOCATION_OPTIONS: List[str] = [

samcli/local/lambdafn/runtime.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class LambdaRuntime:
3535

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

38-
def __init__(self, container_manager, image_builder, mount_symlinks=False):
38+
def __init__(self, container_manager, image_builder, mount_symlinks=False, no_mem_limit=False):
3939
"""
4040
Initialize the Local Lambda runtime
4141
@@ -53,6 +53,7 @@ def __init__(self, container_manager, image_builder, mount_symlinks=False):
5353
self._temp_uncompressed_paths_to_be_cleaned = []
5454
self._lock = threading.Lock()
5555
self._mount_symlinks = mount_symlinks
56+
self._no_mem_limit = no_mem_limit
5657

5758
def create(
5859
self, function_config, debug_context=None, container_host=None, container_host_interface=None, extra_hosts=None
@@ -106,7 +107,7 @@ def create(
106107
layers,
107108
self._image_builder,
108109
function_config.architecture,
109-
memory_mb=function_config.memory,
110+
memory_mb=(None if self._no_mem_limit else function_config.memory),
110111
env_vars=env_vars,
111112
debug_options=debug_context,
112113
container_host=container_host,
@@ -394,7 +395,7 @@ class WarmLambdaRuntime(LambdaRuntime):
394395
warm containers life cycle.
395396
"""
396397

397-
def __init__(self, container_manager, image_builder, observer=None, mount_symlinks=False):
398+
def __init__(self, container_manager, image_builder, observer=None, mount_symlinks=False, no_mem_limit=False):
398399
"""
399400
Initialize the Local Lambda runtime
400401
@@ -412,7 +413,7 @@ def __init__(self, container_manager, image_builder, observer=None, mount_symlin
412413

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

415-
super().__init__(container_manager, image_builder, mount_symlinks=mount_symlinks)
416+
super().__init__(container_manager, image_builder, mount_symlinks=mount_symlinks, no_mem_limit=no_mem_limit)
416417

417418
def create(
418419
self, function_config, debug_context=None, container_host=None, container_host_interface=None, extra_hosts=None

0 commit comments

Comments
 (0)