feat(lambda): support extra /etc/hosts entries on Lambda launch (#2073)

* feat(lambda): support extra /etc/hosts entries via FLOCI_SERVICES_LAMBDA_EXTRA_HOSTS

* feat(lambda): support ipv6 addresses.

---------

Co-authored-by: Florian Overfelt <joverfelt@meso-scale.com>
This commit is contained in:
floverfeltandFlorian Overfelt authored and GitHub committed 2026-08-04 21:50:25 -05:00
1 parent fa96b791b4
commit b543aa4baa
5 files changed
+71

No files matched your search

+1
View File
@@ -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 override
@@ -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);
@@ -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.
+1
View File
@@ -220,6 +220,7 @@ floci:
region-concurrency-limit: 1000
unreserved-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: false
apigateway:
@@ -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)");
}
@Test
void launchFunction_noAwsConfigPath_noBindMount() throws Exception {
Path codePath = Files.createDirectory(tempDir.resolve("no-aws-config"));