Light, fluffy, and always free - The AWS Local Emulator alternative
fix(lambdaweb): give the container the memory the process sees on AWS (#6869)
TATSUNO “Taz” Yasuhiro9217533e7633
diff --git a/docs/services/lambda-web-functions.md b/docs/services/lambda-web-functions.mdindex 2da2f844f6..366ff2d615 100644--- a/docs/services/lambda-web-functions.md+++ b/docs/services/lambda-web-functions.md@@ -71,7 +71,7 @@ The `Host` must match the domain exactly, as on AWS: another case or another porA CloudFront distribution of this Floci can use the endpoint as a custom origin, named by its `domainName` without the port as on AWS. Under an `always-amz-auth` origin access control, CloudFront signs the requests to an `IamAuth` endpoint, and the function's resource policy has to allow `cloudfront.amazonaws.com` when IAM enforcement is on. Its `aws:RequestedRegion` is the region that serves the request, as on AWS. See [CloudFront](cloudfront.md).-An execution environment is a `public.ecr.aws/lambda/nodejs:24` container that a request starts when every environment of the endpoint is full. The code is the revision's S3 object unpacked into `/var/task`. Node starts `/var/runtime/index.js` with the flags AWS uses, as on AWS, and that runtime imports `index.js` or the file `AWS_LAMBDA_NODEJS_ENTRYPOINT` names. So `require.main` is not the app's module, as on AWS. A `--max-old-space-size` or `--max-semi-space-size` in `NODE_OPTIONS` takes the place of both default heap flags, as on AWS. Other flags there do not replace a default flag, so `--max-http-header-size` in `NODE_OPTIONS` has no effect, and a flag Node does not allow in `NODE_OPTIONS` fails the start with `502 Runtime.ExitError`, as on AWS. The app sees `AWS_LAMBDA_HTTP_ENDPOINT=0.0.0.0:3000`, `AWS_LAMBDA_INITIALIZATION_TYPE=native-http`, `AWS_EXECUTION_ENV`, `AWS_LAMBDA_FUNCTION_MEMORY_SIZE=2048`, `AWS_LAMBDA_LOG_LEVEL`, `AWS_LAMBDA_METADATA_API`, `AWS_LAMBDA_METADATA_TOKEN`, `AWS_LAMBDA_RUNTIME_API`, `AWS_ACCOUNT_ID`, `AWS_REGION`, credentials and `AWS_ENDPOINT_URL` for Floci, `NODE_PATH`, and its own variables. The server has 9 seconds to listen. An environment that sees no request for `floci.services.lambda.container-idle-timeout-seconds` is stopped with `SIGTERM` and one second, as on scale to zero. Node runs under Docker's init, so a server without a `SIGTERM` handler stops at once, as on AWS. A process that exits between requests is replaced by a fresh environment on the next request.+An execution environment is a `public.ecr.aws/lambda/nodejs:24` container that a request starts when every environment of the endpoint is full. The code is the revision's S3 object unpacked into `/var/task`. Node starts `/var/runtime/index.js` with the flags AWS uses, as on AWS, and that runtime imports `index.js` or the file `AWS_LAMBDA_NODEJS_ENTRYPOINT` names. So `require.main` is not the app's module, as on AWS. A `--max-old-space-size` or `--max-semi-space-size` in `NODE_OPTIONS` takes the place of both default heap flags, as on AWS. The container has 2519064576 bytes of memory, the total the process sees on AWS, so Node sizes the heap flag left out as on AWS. Other flags there do not replace a default flag, so `--max-http-header-size` in `NODE_OPTIONS` has no effect, and a flag Node does not allow in `NODE_OPTIONS` fails the start with `502 Runtime.ExitError`, as on AWS. The app sees `AWS_LAMBDA_HTTP_ENDPOINT=0.0.0.0:3000`, `AWS_LAMBDA_INITIALIZATION_TYPE=native-http`, `AWS_EXECUTION_ENV`, `AWS_LAMBDA_FUNCTION_MEMORY_SIZE=2048`, `AWS_LAMBDA_LOG_LEVEL`, `AWS_LAMBDA_METADATA_API`, `AWS_LAMBDA_METADATA_TOKEN`, `AWS_LAMBDA_RUNTIME_API`, `AWS_ACCOUNT_ID`, `AWS_REGION`, credentials and `AWS_ENDPOINT_URL` for Floci, `NODE_PATH`, and its own variables. The server has 9 seconds to listen. An environment that sees no request for `floci.services.lambda.container-idle-timeout-seconds` is stopped with `SIGTERM` and one second, as on scale to zero. Node runs under Docker's init, so a server without a `SIGTERM` handler stops at once, as on AWS. A process that exits between requests is replaced by a fresh environment on the next request.Each endpoint scales on its own, as on AWS. An environment takes up to the revision's `maxConcurrencyPerEnvironment` requests at once, the least busy one first, so requests rotate over the environments. A request that finds every environment full starts another one, or waits for one that is starting and has room. When the endpoint already runs `maxEnvironments`, an idle environment of another revision makes room, and without one the request is refused with `429` and `Platform.EndpointMaxEnvThrottle`. A `rateLimit` admits a burst of up to `rateLimit` requests that refills at `rateLimit` per second, and refuses the rest with `429` and `Platform.EndpointRequestThrottle`. AWS adjusts the concurrency of each environment under `maxConcurrencyPerEnvironment` from the measured load, so a burst there can spread over more environments than in Floci, and the account quotas are not applied.diff --git a/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebEnvironmentManager.java b/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebEnvironmentManager.javaindex 13a35306c6..61868edbe5 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebEnvironmentManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebEnvironmentManager.java@@ -83,7 +83,8 @@ public class LambdaWebEnvironmentManager implements ContainerTeardown {static final String TASK_DIR = "/var/task";static final String RUNTIME_DIR = "/var/runtime";private static final String RUNTIME_RESOURCE = "lambdaweb/index.mjs";- static final int MEMORY_MB = 2048;+ // The total the process sees on AWS. Node sizes a heap flag that NODE_OPTIONS leaves out from it.+ static final long CONTAINER_MEMORY_BYTES = 2519064576L;static final Duration LISTEN_TIMEOUT = Duration.ofSeconds(9);static final Duration SUMMARY_PERIOD = Duration.ofSeconds(60);static final List<String> NODE_FLAGS = List.of("--expose-gc", "--max-http-header-size", "135168",@@ -578,7 +579,7 @@ public class LambdaWebEnvironmentManager implements ContainerTeardown {.withEntrypoint(List.of("/var/lang/bin/node")).withCmd(cmd).withWorkingDir(TASK_DIR)- .withMemoryMb(MEMORY_MB)+ .withMemoryBytes(CONTAINER_MEMORY_BYTES).withLoopbackPortBinding(APP_PORT, 0).withInit().withDockerNetwork(config.services().lambda().dockerNetwork())@@ -704,7 +705,7 @@ public class LambdaWebEnvironmentManager implements ContainerTeardown {env.add("AWS_LAMBDA_HTTP_ENDPOINT=0.0.0.0:" + APP_PORT);env.add("AWS_LAMBDA_INITIALIZATION_TYPE=native-http");env.add("AWS_EXECUTION_ENV=AWS_Lambda_" + revision.buildConfig.runtime + "-lite");- env.add("AWS_LAMBDA_FUNCTION_MEMORY_SIZE=" + MEMORY_MB);+ env.add("AWS_LAMBDA_FUNCTION_MEMORY_SIZE=2048");env.add("AWS_LAMBDA_LOG_LEVEL=" + revision.serviceConfig.applicationLogLevel);// AWS names the metadata server as the runtime API too, and it answers the runtime paths with 404.env.add("AWS_LAMBDA_METADATA_API=" + metadataApi);diff --git a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneDockerIntegrationTest.javaindex 7e870e530c..cafdc4b463 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneDockerIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneDockerIntegrationTest.java@@ -127,7 +127,7 @@ class LambdaWebDataPlaneDockerIntegrationTest {env: {endpoint: process.env.AWS_LAMBDA_HTTP_ENDPOINT, init: process.env.AWS_LAMBDA_INITIALIZATION_TYPE,exec: process.env.AWS_EXECUTION_ENV, account: process.env.AWS_ACCOUNT_ID,region: process.env.AWS_REGION, custom: process.env.CUSTOM},- execArgv: process.execArgv}));+ execArgv: process.execArgv, heapSizeLimit: require('node:v8').getHeapStatistics().heap_size_limit}));});}).listen(Number(port), host);""";@@ -767,7 +767,8 @@ class LambdaWebDataPlaneDockerIntegrationTest {given().header("Host", domain).when().get("/").then().statusCode(200).body("execArgv", contains("--expose-gc", "--max-http-header-size", "135168",- "--no-experimental-require-module", "--max-old-space-size=1536"));+ "--no-experimental-require-module", "--max-old-space-size=1536"))+ .body("heapSizeLimit", equalTo(1811939328));}@Test