fix(lambda): inject log group/stream and Floci endpoint env vars into Lambda containers (#640)

* fix(lambda): inject log, endpoint, and hostname env vars into Lambda containers

Three missing environment variables prevented Lambda functions from running
correctly with newer runtimes and from reaching back into Floci:

and AWS_LAMBDA_LOG_STREAM_NAME to be set before the runtime initialises. Floci
computed both values but only used them for log streaming — they were never added
to the container's env list, causing a hard PlatformError on boot.

their container. FLOCI_HOSTNAME, FLOCI_ENDPOINT, and AWS_ENDPOINT_URL are now
injected so any AWS SDK client instantiated inside the function uses Floci
automatically, without requiring code changes.

When the embedded DNS server is active (Floci running in Docker), FLOCI_HOSTNAME
is set to the configured hostname (e.g. "floci") rather than a raw IP, because
the DNS server is already wired into each Lambda container as its resolver and
will resolve that name correctly. In local dev mode (no embedded DNS), the
Docker-reachable host address is used as a fallback.

Closes #628
Closes #629

* feat(lambda): implement PutFunctionEventInvokeConfig and sibling endpoints (#632)

Adds the five event-invoke-config REST endpoints that Terraform's aws_lambda_function_event_invoke_config resource requires:

- PUT /2019-09-25/functions/{name}/event-invoke-config — replaces config
- POST — partial update (merge)
- GET — retrieve config, 404 if absent
- DELETE — remove config, 404 if absent
- GET .../event-invoke-config/list — list all qualifiers

New files: FunctionEventInvokeConfig model (with @RegisterForReflection and correct AWS JSON field names), LambdaEventInvokeController, and LambdaEventInvokeConfigTest (10
unit tests). Storage is an in-memory ConcurrentHashMap keyed by region:functionArn:qualifier.

Also fixes the ContainerLauncherTest and LambdaImageConfigTest setUp methods that were missing config.baseUrl() stubs required by the FLOCI_HOSTNAME/FLOCI_ENDPOINT env-var
injection added in the branch.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-04-29 00:02:07 -05:00
1 parent 1cccf43f7b
commit 3960b6e20e
7 files changed
+496 -12

No files matched your search

@@ -0,0 +1,148 @@
package io.github.hectorvent.floci.services.lambda;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.lambda.model.FunctionEventInvokeConfig;
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
import io.github.hectorvent.floci.services.lambda.zip.CodeStore;
import io.github.hectorvent.floci.services.lambda.zip.ZipExtractor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
class LambdaEventInvokeConfigTest {
private static final String REGION = "us-east-1";
private LambdaService service;
@BeforeEach
void setUp() {
LambdaFunctionStore store = new LambdaFunctionStore(new InMemoryStorage<String, LambdaFunction>());
WarmPool warmPool = new WarmPool();
CodeStore codeStore = new CodeStore(Path.of("target/test-data/lambda-code"));
RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");
service = new LambdaService(store, warmPool, codeStore, new ZipExtractor(), regionResolver);
service.createFunction(REGION, Map.of(
"FunctionName", "test-fn",
"PackageType", "Image",
"Role", "arn:aws:iam::000000000000:role/test-role",
"Code", Map.of("ImageUri", "public.ecr.aws/lambda/nodejs:20")
));
}
@Test
void putCreatesConfig() {
Map<String, Object> req = new HashMap<>();
req.put("MaximumRetryAttempts", 1);
req.put("MaximumEventAgeInSeconds", 3600);
FunctionEventInvokeConfig cfg = service.putEventInvokeConfig(REGION, "test-fn", null, req);
assertEquals(1, cfg.getMaximumRetryAttempts());
assertEquals(3600, cfg.getMaximumEventAgeInSeconds());
assertTrue(cfg.getFunctionArn().endsWith(":$LATEST"));
assertTrue(cfg.getLastModifiedSeconds() > 0);
}
@Test
void putReplacesExistingConfig() {
service.putEventInvokeConfig(REGION, "test-fn", null, Map.of("MaximumRetryAttempts", 2));
Map<String, Object> req2 = new HashMap<>();
req2.put("MaximumRetryAttempts", 0);
FunctionEventInvokeConfig cfg = service.putEventInvokeConfig(REGION, "test-fn", null, req2);
assertEquals(0, cfg.getMaximumRetryAttempts());
assertNull(cfg.getMaximumEventAgeInSeconds());
}
@Test
void getReturnsStoredConfig() {
service.putEventInvokeConfig(REGION, "test-fn", null, Map.of("MaximumRetryAttempts", 1));
FunctionEventInvokeConfig cfg = service.getEventInvokeConfig(REGION, "test-fn", null);
assertEquals(1, cfg.getMaximumRetryAttempts());
}
@Test
void getThrows404WhenNotFound() {
AwsException ex = assertThrows(AwsException.class,
() -> service.getEventInvokeConfig(REGION, "test-fn", null));
assertEquals(404, ex.getHttpStatus());
}
@Test
void updateMergesPartialFields() {
service.putEventInvokeConfig(REGION, "test-fn", null, Map.of(
"MaximumRetryAttempts", 2,
"MaximumEventAgeInSeconds", 7200));
Map<String, Object> partial = new HashMap<>();
partial.put("MaximumRetryAttempts", 0);
service.updateEventInvokeConfig(REGION, "test-fn", null, partial);
FunctionEventInvokeConfig cfg = service.getEventInvokeConfig(REGION, "test-fn", null);
assertEquals(0, cfg.getMaximumRetryAttempts());
assertEquals(7200, cfg.getMaximumEventAgeInSeconds());
}
@Test
void updateThrows404WhenNotFound() {
AwsException ex = assertThrows(AwsException.class,
() -> service.updateEventInvokeConfig(REGION, "test-fn", null, Map.of()));
assertEquals(404, ex.getHttpStatus());
}
@Test
void deleteRemovesConfig() {
service.putEventInvokeConfig(REGION, "test-fn", null, Map.of("MaximumRetryAttempts", 1));
service.deleteEventInvokeConfig(REGION, "test-fn", null);
assertThrows(AwsException.class,
() -> service.getEventInvokeConfig(REGION, "test-fn", null));
}
@Test
void deleteThrows404WhenNotFound() {
AwsException ex = assertThrows(AwsException.class,
() -> service.deleteEventInvokeConfig(REGION, "test-fn", null));
assertEquals(404, ex.getHttpStatus());
}
@Test
void listReturnsAllConfigsForFunction() {
service.putEventInvokeConfig(REGION, "test-fn", null, Map.of("MaximumRetryAttempts", 1));
service.putEventInvokeConfig(REGION, "test-fn", "1", Map.of("MaximumRetryAttempts", 0));
List<FunctionEventInvokeConfig> configs = service.listEventInvokeConfigs(REGION, "test-fn");
assertEquals(2, configs.size());
}
@Test
void putWithDestinationConfig() {
Map<String, Object> req = new HashMap<>();
req.put("DestinationConfig", Map.of(
"OnSuccess", Map.of("Destination", "arn:aws:sqs:us-east-1:000000000000:my-queue"),
"OnFailure", Map.of("Destination", "arn:aws:sqs:us-east-1:000000000000:dlq")
));
FunctionEventInvokeConfig cfg = service.putEventInvokeConfig(REGION, "test-fn", null, req);
assertNotNull(cfg.getDestinationConfig());
assertEquals("arn:aws:sqs:us-east-1:000000000000:my-queue",
cfg.getDestinationConfig().getOnSuccess().getDestination());
assertEquals("arn:aws:sqs:us-east-1:000000000000:dlq",
cfg.getDestinationConfig().getOnFailure().getDestination());
}
}
@@ -181,11 +181,13 @@ class LambdaImageConfigTest {
when(config.docker()).thenReturn(docker);
when(docker.logMaxSize()).thenReturn("10m");
when(docker.logMaxFile()).thenReturn("3");
when(config.baseUrl()).thenReturn("http://localhost:4566");
lenient().when(config.hostname()).thenReturn(Optional.empty());
when(embeddedDnsServer.getServerIp()).thenReturn(Optional.empty());
ContainerBuilder containerBuilder = new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer);
launcher = new ContainerLauncher(containerBuilder, lifecycleManager, logStreamer, imageResolver,
runtimeApiServerFactory, dockerHostResolver, config, ecrRegistryManager);
runtimeApiServerFactory, dockerHostResolver, config, ecrRegistryManager, embeddedDnsServer);
when(runtimeApiServerFactory.create()).thenReturn(runtimeApiServer);
when(runtimeApiServer.getPort()).thenReturn(9000);
@@ -67,12 +67,14 @@ class ContainerLauncherTest {
when(config.docker()).thenReturn(docker);
when(docker.logMaxSize()).thenReturn("10m");
when(docker.logMaxFile()).thenReturn("3");
when(config.baseUrl()).thenReturn("http://localhost:4566");
lenient().when(config.hostname()).thenReturn(Optional.empty());
when(embeddedDnsServer.getServerIp()).thenReturn(Optional.empty());
ContainerBuilder containerBuilder = new ContainerBuilder(config, dockerHostResolver, embeddedDnsServer);
launcher = new ContainerLauncher(containerBuilder, lifecycleManager, logStreamer, imageResolver,
runtimeApiServerFactory, dockerHostResolver, config, ecrRegistryManager);
runtimeApiServerFactory, dockerHostResolver, config, ecrRegistryManager, embeddedDnsServer);
when(runtimeApiServerFactory.create()).thenReturn(runtimeApiServer);
when(runtimeApiServer.getPort()).thenReturn(9000);