Light, fluffy, and always free - The AWS Local Emulator alternative
feat(lambda): support extra /etc/hosts entries on Lambda launch (#2073)
floverfeltb543aa4baad4
diff --git a/docs/services/lambda.md b/docs/services/lambda.mdindex 838501c92e..04f614ec63 100644--- a/docs/services/lambda.md+++ b/docs/services/lambda.md@@ -195,6 +195,7 @@ These AWS Lambda operations have no handler in Floci. Calls will return `404` or| `FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ENABLED` | `false` | Enable bind-mount hot-reload via `S3Bucket=hot-reload` || `FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS` | *(unset)* | Comma-separated allowlist of host paths that may be bind-mounted || `FLOCI_SERVICES_LAMBDA_DOCKER_NETWORK` | *(unset)* | Docker network to attach Lambda containers to (overrides `FLOCI_SERVICES_DOCKER_NETWORK`) |+| `FLOCI_SERVICES_LAMBDA_EXTRA_HOSTS` | *(unset)* | Comma-separated `hostname:ip` entries added to each Lambda container's `/etc/hosts`; `ip` may be `host-gateway`, mirroring `docker run --add-host` || `FLOCI_SERVICES_LAMBDA_DOCKER_HOST_OVERRIDE` | *(unset)* | Explicit host/IP that spawned Lambda containers use to reach Floci's Runtime API, bypassing auto-detection |### Runtime API host overridediff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.javaindex 069344f15f..29411562e9 100644--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java@@ -1343,6 +1343,15 @@ public interface EmulatorConfig {/** Docker network to attach Lambda containers to. Empty = default bridge. */Optional<String> dockerNetwork();+ /**+ * Extra /etc/hosts entries added to every Lambda container, as "hostname:ip" pairs.+ * The ip may be the literal "host-gateway" to map to the Docker host, mirroring+ * {@code docker run --add-host hostname:host-gateway}.+ *+ * Env var: FLOCI_SERVICES_LAMBDA_EXTRA_HOSTS (comma-separated)+ */+ Optional<List<String>> extraHosts();+/*** Concurrent executions ceiling applied per region. AWS Lambda's* "account-level" concurrency is in fact a per-region quota (default 1000);diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.javaindex 0925c9d04f..e0fa962d62 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java@@ -223,6 +223,18 @@ public class ContainerLauncher {specBuilder.withEmbeddedDns();+ // Inject extra hosts entries into the container if present. Split on the FIRST+ // colon, mirroring docker --add-host: hostnames cannot contain colons, but IPv6+ // addresses do (e.g. "db.internal:2001:db8::1").+ config.services().lambda().extraHosts().ifPresent(hosts -> hosts.forEach(entry -> {+ int sep = entry.indexOf(':');+ if (sep <= 0 || sep == entry.length() - 1) {+ LOG.warnv("Ignoring malformed lambda extra-hosts entry (expected hostname:ip): {0}", entry);+ return;+ }+ specBuilder.withExtraHost(entry.substring(0, sep), entry.substring(sep + 1));+ }));+// Whether /var/task is served from a shared read-only volume (large code) or copied// directly into this container (small code). Decided once here so both the spec (below)// and the create->start copy block agree.diff --git a/src/main/resources/application.yml b/src/main/resources/application.ymlindex cade7fb3f4..b695948fdd 100644--- a/src/main/resources/application.yml+++ b/src/main/resources/application.yml@@ -220,6 +220,7 @@ floci:region-concurrency-limit: 1000unreserved-concurrency-min: 100# aws-config-path: # Host path to bind-mount (read-only) at /opt/aws-config for real credential discovery+ # extra-hosts: # Extra /etc/hosts entries for Lambda containers, "hostname:ip" ("host-gateway" allowed as ip)hot-reload:enabled: falseapigateway:diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.javaindex e767f6f109..4de92c240c 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherTest.java@@ -88,6 +88,7 @@ class ContainerLauncherTest {when(config.services()).thenReturn(services);when(services.lambda()).thenReturn(lambda);when(lambda.dockerNetwork()).thenReturn(Optional.empty());+ lenient().when(lambda.extraHosts()).thenReturn(Optional.empty());lenient().when(lambda.awsConfigPath()).thenReturn(Optional.empty());when(config.docker()).thenReturn(docker);when(docker.logMaxSize()).thenReturn("10m");@@ -490,6 +491,53 @@ class ContainerLauncherTest {"AWS_SESSION_TOKEN should not be injected when awsConfigPath is set");}+ @Test+ void launchFunction_appliesConfiguredExtraHosts_skippingMalformedEntries() throws Exception {+ EmulatorConfig.LambdaServiceConfig lambda = config.services().lambda();+ when(lambda.extraHosts()).thenReturn(Optional.of(List.of(+ "localhost:host-gateway", "db.internal:10.0.0.5",+ "v6.internal:2001:db8::1", "v6end.internal:fd00::",+ "malformed", ":9.9.9.9", "trailing:")));++ Path codePath = Files.createDirectory(tempDir.resolve("extra-hosts"));++ LambdaFunction fn = new LambdaFunction();+ fn.setFunctionName("extra-hosts-fn");+ fn.setRuntime("nodejs20.x");+ fn.setHandler("index.handler");+ fn.setCodeLocalPath(codePath.toString());++ launcher.launch(fn);++ List<String> extraHosts = captureRealContainerSpec().extraHosts();+ assertTrue(extraHosts.contains("localhost:host-gateway"),+ "configured hostname:host-gateway entry should be applied");+ assertTrue(extraHosts.contains("db.internal:10.0.0.5"),+ "configured hostname:ip entry should be applied");+ assertTrue(extraHosts.contains("v6.internal:2001:db8::1"),+ "IPv6 addresses (containing colons) must survive the hostname/ip split");+ assertTrue(extraHosts.contains("v6end.internal:fd00::"),+ "IPv6 addresses ending in :: must not be classified as missing an ip");+ assertEquals(4, extraHosts.size(),+ "entries without a hostname and an ip must be skipped, not passed to Docker");+ }++ @Test+ void launchFunction_noExtraHostsByDefault() throws Exception {+ Path codePath = Files.createDirectory(tempDir.resolve("no-extra-hosts"));++ LambdaFunction fn = new LambdaFunction();+ fn.setFunctionName("no-extra-hosts-fn");+ fn.setRuntime("nodejs20.x");+ fn.setHandler("index.handler");+ fn.setCodeLocalPath(codePath.toString());++ launcher.launch(fn);++ assertTrue(captureRealContainerSpec().extraHosts().isEmpty(),+ "no extra hosts when the config is unset (non-Linux host in this test)");+ }+@Testvoid launchFunction_noAwsConfigPath_noBindMount() throws Exception {Path codePath = Files.createDirectory(tempDir.resolve("no-aws-config"));