fix(lambda): limit a function container's memory and swap together to its MemorySize (#7704)

This commit is contained in:
Charles Swanberg authored and GitHub committed 2026-11-06 19:39:52 -04:00
1 parent 8b81638490
commit 8bc134cc84
2 files changed
+38 -1

No files matched your search

@@ -292,10 +292,14 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
env.add("AWS_LAMBDA_INITIALIZATION_TYPE=provisioned-concurrency");
}
// Memory and swap together at the function's MemorySize, so the container cannot swap
// and has what Lambda gives the function, whatever swap the host has.
long memoryBytes = (long) fn.getMemorySize() * 1024 * 1024;
ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(image)
.withName(containerName)
.withEnv(env)
.withMemoryMb(fn.getMemorySize())
.withMemoryBytes(memoryBytes)
.withMemorySwapBytes(memoryBytes)
.withDockerNetwork(config.services().lambda().dockerNetwork())
.withLogRotation()
.withLabels(ContainerStorageHelper.resourceIdentityLabels(
@@ -367,6 +367,39 @@ class ContainerLauncherTest {
.toList());
}
@Test
void launchLimitsMemoryAndSwapTogetherToTheDeclaredMemorySize() {
LambdaFunction fn = new LambdaFunction();
fn.setFunctionName("memory-fn");
fn.setHotReloadHostPath("/home/ci/code");
fn.setRuntime("nodejs20.x");
fn.setHandler("index.handler");
fn.setMemorySize(256);
launcher.launch(fn);
ContainerSpec spec = captureRealContainerSpec();
assertEquals(256L * 1024 * 1024, spec.memoryBytes());
assertEquals(256L * 1024 * 1024, spec.linuxOptions().memorySwapBytes(),
"memory plus swap equal to the memory limit: no swap");
}
@Test
void launchLimitsMemoryAndSwapTogetherToLambdasDefaultWhereNoneIsDeclared() {
LambdaFunction fn = new LambdaFunction();
fn.setFunctionName("default-memory-fn");
fn.setHotReloadHostPath("/home/ci/code");
fn.setRuntime("nodejs20.x");
fn.setHandler("index.handler");
launcher.launch(fn);
ContainerSpec spec = captureRealContainerSpec();
assertEquals(128L * 1024 * 1024, spec.memoryBytes());
assertEquals(128L * 1024 * 1024, spec.linuxOptions().memorySwapBytes(),
"memory plus swap equal to the memory limit: no swap");
}
@Test
void launchFunction_hotReloadMountsTheHostDirectoryReadOnlyAtVarTask() {
LambdaFunction fn = new LambdaFunction();