Light, fluffy, and always free - The AWS Local Emulator alternative
feat(lambda): support EFS file system configs (#2053)
Alex Bannae1f389f61bd1
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaTest.javaindex 4e32bc5a94..5c488f67ba 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaTest.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaTest.java@@ -6,6 +6,8 @@ import software.amazon.awssdk.services.lambda.LambdaClient;import software.amazon.awssdk.services.lambda.model.*;import software.amazon.awssdk.services.lambda.model.Runtime;+import java.util.List;+import static org.assertj.core.api.Assertions.*;@DisplayName("Lambda")@@ -14,6 +16,7 @@ class LambdaTest {private static LambdaClient lambda;private static final String FUNCTION_NAME = "sdk-test-fn";+ private static final String FILE_SYSTEM_FUNCTION_NAME = "sdk-test-efs-fn";private static final String ROLE = "arn:aws:iam::000000000000:role/lambda-role";private static String functionArn;@@ -37,6 +40,12 @@ class LambdaTest {lambda.deleteFunction(DeleteFunctionRequest.builder().functionName("sdk-test-provided-fn").build());} catch (Exception ignored) {}+ try {+ lambda.deleteFunction(DeleteFunctionRequest.builder()+ .functionName(FILE_SYSTEM_FUNCTION_NAME).build());+ } catch (Exception e) {+ System.err.println("Best-effort Lambda file-system test cleanup failed: " + e.getMessage());+ }lambda.close();}}@@ -364,4 +373,54 @@ class LambdaTest {lambda.deleteFunction(DeleteFunctionRequest.builder().functionName(rubyFn).build());}++ @Test+ @Order(19)+ void fileSystemConfigsRoundTripAndMountWithAwsSdk() {+ FileSystemConfig fileSystem = FileSystemConfig.builder()+ .arn("arn:aws:elasticfilesystem:us-east-1:000000000000:"+ + "access-point/fsap-0123456789abcdef0")+ .localMountPath("/mnt/shared")+ .build();++ CreateFunctionResponse created = lambda.createFunction(CreateFunctionRequest.builder()+ .functionName(FILE_SYSTEM_FUNCTION_NAME)+ .runtime(Runtime.NODEJS20_X)+ .role(ROLE)+ .handler("index.handler")+ .vpcConfig(VpcConfig.builder()+ .subnetIds("subnet-0123456789abcdef0")+ .securityGroupIds("sg-0123456789abcdef0")+ .build())+ .fileSystemConfigs(fileSystem)+ .code(FunctionCode.builder()+ .zipFile(SdkBytes.fromByteArray(LambdaUtils.fileSystemZip()))+ .build())+ .build());++ assertThat(created.fileSystemConfigs()).containsExactly(fileSystem);++ GetFunctionConfigurationResponse configuration = lambda.getFunctionConfiguration(+ GetFunctionConfigurationRequest.builder()+ .functionName(FILE_SYSTEM_FUNCTION_NAME)+ .build());+ assertThat(configuration.fileSystemConfigs()).containsExactly(fileSystem);++ InvokeResponse invocation = lambda.invoke(InvokeRequest.builder()+ .functionName(FILE_SYSTEM_FUNCTION_NAME)+ .invocationType(InvocationType.REQUEST_RESPONSE)+ .payload(SdkBytes.fromUtf8String("{\"value\":\"from-sdk\"}"))+ .build());+ assertThat(invocation.functionError()).isNull();+ assertThat(invocation.payload().asUtf8String())+ .contains("\"mounted\":true")+ .contains("\"value\":\"from-sdk\"");++ UpdateFunctionConfigurationResponse updated = lambda.updateFunctionConfiguration(+ UpdateFunctionConfigurationRequest.builder()+ .functionName(FILE_SYSTEM_FUNCTION_NAME)+ .fileSystemConfigs(List.of())+ .build());+ assertThat(updated.fileSystemConfigs()).isEmpty();+ }}diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaUtils.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaUtils.javaindex 336e297f34..d559780c70 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaUtils.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaUtils.java@@ -95,6 +95,24 @@ public final class LambdaUtils {return createZip("index.js", code);}+ /**+ * ZIP containing a Node.js handler that writes and reads a configured Lambda file-system mount.+ */+ public static byte[] fileSystemZip() {+ String code = """+ const fs = require('fs');+ const file = '/mnt/shared/sdk-test.txt';+ exports.handler = async (event) => {+ fs.writeFileSync(file, event.value, 'utf8');+ return {+ mounted: fs.existsSync(file),+ value: fs.readFileSync(file, 'utf8')+ };+ };+ """;+ return createZip("index.js", code);+ }+/*** ZIP containing a Node.js handler that logs the first S3 event record.*/diff --git a/docs/services/lambda.md b/docs/services/lambda.mdindex 04f614ec63..98fe624c15 100644--- a/docs/services/lambda.md+++ b/docs/services/lambda.md@@ -255,6 +255,20 @@ No extra configuration or `cap_add` is needed — Docker containers have`CAP_NET_BIND_SERVICE` in their default capability set, so Floci (running as anon-root user) can bind UDP/53 without any changes to your Compose file.+### File system configs++`FileSystemConfigs` accepts one EFS access point and mounts it under the+requested `/mnt/...` path for local Lambda containers. As on AWS, the function+must include VPC subnet and security group configuration. The mounted path uses+the same shared-volume initialization settings as ECS EFS volumes under+`floci.storage.efs`.++This configuration is supported through the Lambda API, `AWS::Lambda::Function`+resources, and `AWS::Serverless::Function` resources.++S3 Files access points are not currently emulated and are rejected instead of+being mounted as an empty local volume.+!!! note "Resolving public hostnames from Lambda"A Lambda whose handler reaches a public host (`fetch()`/HTTPS to e.g.`business-api.tiktok.com`) resolves it through Floci's embedded DNS. As adiff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.javaindex 2bff8da334..ae8c741889 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java@@ -63,6 +63,7 @@ import io.github.hectorvent.floci.services.iam.model.IamRole;import io.github.hectorvent.floci.services.kms.KmsService;import io.github.hectorvent.floci.services.lambda.LambdaService;import io.github.hectorvent.floci.services.lambda.LambdaLayerService;+import io.github.hectorvent.floci.services.lambda.model.LambdaFileSystemConfig;import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;import io.github.hectorvent.floci.services.lambda.model.LambdaLayerVersion;import io.github.hectorvent.floci.services.pipes.PipesService;@@ -1584,6 +1585,8 @@ public class CloudFormationResourceProvisioner {configRequest.put("DeadLetterConfig", resolveMapOrDefault(props, "DeadLetterConfig", engine,mapWithNullValue("TargetArn")));configRequest.put("VpcConfig", resolveMapOrDefault(props, "VpcConfig", engine, Map.of()));+ configRequest.put("FileSystemConfigs",+ resolveObjectListOrEmpty(props, "FileSystemConfigs", engine));putResolvedMapIfPresent(configRequest, props, "ImageConfig", "ImageConfig", engine);createRequest.putAll(configRequest);@@ -1787,6 +1790,12 @@ public class CloudFormationResourceProvisioner {return true;}}+ case "FileSystemConfigs" -> {+ if (!Objects.equals(normalizeForCompare(fileSystemConfigs(fn)),+ normalizeForCompare(desired))) {+ return true;+ }+ }case "ImageConfig" -> {if (imageConfigurationChanged(fn, desired)) return true;}@@ -1816,6 +1825,22 @@ public class CloudFormationResourceProvisioner {&& !Objects.equals(fn.getImageConfigWorkingDirectory(), mapString(map, "WorkingDirectory"));}+ private static List<Map<String, String>> fileSystemConfigs(LambdaFunction fn) {+ if (fn.getFileSystemConfigs() == null) {+ return List.of();+ }+ return fn.getFileSystemConfigs().stream()+ .map(CloudFormationResourceProvisioner::fileSystemConfig)+ .toList();+ }++ private static Map<String, String> fileSystemConfig(LambdaFileSystemConfig config) {+ Map<String, String> value = new LinkedHashMap<>();+ value.put("Arn", config.getArn());+ value.put("LocalMountPath", config.getLocalMountPath());+ return value;+ }+private static String sha256Base64(String zipFileBase64) {byte[] zipBytes = Base64.getDecoder().decode(zipFileBase64);try {@@ -1915,6 +1940,23 @@ public class CloudFormationResourceProvisioner {return values;}+ private List<Object> resolveObjectListOrEmpty(JsonNode props, String source,+ CloudFormationTemplateEngine engine) {+ if (props == null || !props.has(source) || props.get(source).isNull()) {+ return List.of();+ }+ JsonNode resolved = engine.resolveNode(props.get(source));+ if (resolved == null) {+ return List.of();+ }+ if (!resolved.isArray()) {+ throw new AwsException("ValidationError", source + " must be a list", 400);+ }+ List<Object> values = new ArrayList<>();+ resolved.forEach(value -> values.add(jsonNodeToValue(value)));+ return values;+ }+private Map<String, Object> resolveMapOrDefault(JsonNode props, String source,CloudFormationTemplateEngine engine,Map<String, Object> defaultValue) {diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessor.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessor.javaindex ba2ccedf15..c7cd9caae5 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessor.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessor.java@@ -443,6 +443,8 @@ class SamTransformProcessor {copyIfPresent(properties, "Architectures", lambdaProps);copyIfPresent(properties, "ReservedConcurrentExecutions", lambdaProps);copyIfPresent(properties, "EphemeralStorage", lambdaProps);+ copyIfPresent(properties, "VpcConfig", lambdaProps);+ copyIfPresent(properties, "FileSystemConfigs", lambdaProps);JsonNode tracing = properties.path("Tracing");if (!tracing.isMissingNode()) {diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.javaindex 12ff1d1650..191d97d0a6 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaController.java@@ -626,6 +626,13 @@ public class LambdaController {node.put("KMSKeyArn", fn.getKmsKeyArn());}+ if (fn.getFileSystemConfigs() != null && !fn.getFileSystemConfigs().isEmpty()) {+ ArrayNode fileSystems = node.putArray("FileSystemConfigs");+ fn.getFileSystemConfigs().forEach(fileSystem -> fileSystems.addObject()+ .put("Arn", fileSystem.getArn())+ .put("LocalMountPath", fileSystem.getLocalMountPath()));+ }+// Environment — always present (SDK expects it even when empty)ObjectNode envNode = node.putObject("Environment");if (fn.getEnvironment() != null && !fn.getEnvironment().isEmpty()) {diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.javaindex f63830a54a..62d20b1bf1 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.lambda.model.FunctionEventInvokeConfiimport io.github.hectorvent.floci.services.lambda.model.InvocationType;import io.github.hectorvent.floci.services.lambda.model.InvokeResult;import io.github.hectorvent.floci.services.lambda.model.LambdaAlias;+import io.github.hectorvent.floci.services.lambda.model.LambdaFileSystemConfig;import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;import io.github.hectorvent.floci.services.lambda.model.LambdaUrlConfig;import io.github.hectorvent.floci.services.lambda.model.ScalingConfig;@@ -40,6 +41,7 @@ import java.util.Map;import java.util.Optional;import java.util.UUID;import java.util.concurrent.ConcurrentHashMap;+import java.util.regex.Pattern;/*** Business logic for Lambda function management and invocation.@@ -48,6 +50,11 @@ import java.util.concurrent.ConcurrentHashMap;public class LambdaService {private static final Logger LOG = Logger.getLogger(LambdaService.class);+ private static final Pattern EFS_ACCESS_POINT_ARN = Pattern.compile(+ "^arn:aws[a-zA-Z-]*:elasticfilesystem:(?:eusc-)?[a-z]{2}"+ + "(?:(?:-gov)|(?:-iso(?:b)?))?-[a-z]+-\\d:"+ + "\\d{12}:access-point/fsap-[a-f0-9]{17}$");+ private static final Pattern FILE_SYSTEM_LOCAL_MOUNT_PATH = Pattern.compile("^/mnt/[A-Za-z0-9._-]+$");private final LambdaFunctionStore functionStore;private final LambdaExecutorService executorService;@@ -327,6 +334,11 @@ public class LambdaService {fn.setVpcConfig(vpc);}+ List<LambdaFileSystemConfig> fileSystemConfigs =+ parseFileSystemConfigs(request.get("FileSystemConfigs"));+ validateFileSystemVpcConfig(fileSystemConfigs, fn.getVpcConfig());+ fn.setFileSystemConfigs(fileSystemConfigs);+// ImageConfig (PackageType=Image overrides)if (request.get("ImageConfig") instanceof Map<?, ?> ic) {@SuppressWarnings("unchecked")@@ -441,7 +453,7 @@ public class LambdaService {fn.setRevisionId(UUID.randomUUID().toString());// Drain warm containers — they have stale code mounted- warmPool.drainFunction(functionName);+ warmPool.drainEnvironment(fn);functionStore.save(region, fn);LOG.infov("Updated code for function: {0}", functionName);@@ -451,6 +463,20 @@ public class LambdaService {public LambdaFunction updateFunctionConfiguration(String region, String functionName, Map<String, Object> request) {LambdaFunction fn = getFunction(region, functionName);+ Map<String, Object> requestedVpcConfig = fn.getVpcConfig();+ if (request.get("VpcConfig") instanceof Map<?, ?>) {+ @SuppressWarnings("unchecked")+ Map<String, Object> vpc = (Map<String, Object>) request.get("VpcConfig");+ requestedVpcConfig = new java.util.HashMap<>(vpc);+ }+ List<LambdaFileSystemConfig> requestedFileSystemConfigs = fn.getFileSystemConfigs();+ if (request.containsKey("FileSystemConfigs")) {+ requestedFileSystemConfigs = parseFileSystemConfigs(request.get("FileSystemConfigs"));+ }+ if (request.containsKey("VpcConfig") || request.containsKey("FileSystemConfigs")) {+ validateFileSystemVpcConfig(requestedFileSystemConfigs, requestedVpcConfig);+ }+if (request.containsKey("Description")) {fn.setDescription((String) request.get("Description"));}@@ -531,12 +557,14 @@ public class LambdaService {if (request.containsKey("VpcConfig")) {if (request.get("VpcConfig") instanceof Map<?, ?>) {- @SuppressWarnings("unchecked")- Map<String, Object> vpc = (Map<String, Object>) request.get("VpcConfig");- fn.setVpcConfig(vpc);+ fn.setVpcConfig(requestedVpcConfig);}}+ if (request.containsKey("FileSystemConfigs")) {+ fn.setFileSystemConfigs(requestedFileSystemConfigs);+ }+if (request.containsKey("ImageConfig")) {if (request.get("ImageConfig") instanceof Map<?, ?> ic) {@SuppressWarnings("unchecked")@@ -562,7 +590,7 @@ public class LambdaService {fn.setRevisionId(UUID.randomUUID().toString());// Drain warm containers so the next invocation picks up the new configuration- warmPool.drainFunction(functionName);+ warmPool.drainEnvironment(fn);functionStore.save(region, fn);LOG.infov("Updated configuration for function: {0}", functionName);@@ -944,6 +972,10 @@ public class LambdaService {snapshot.setState(fn.getState());snapshot.setCodeSizeBytes(fn.getCodeSizeBytes());snapshot.setEnvironment(fn.getEnvironment());+ snapshot.setVpcConfig(fn.getVpcConfig() == null+ ? null+ : new java.util.HashMap<>(fn.getVpcConfig()));+ snapshot.setFileSystemConfigs(new ArrayList<>(fn.getFileSystemConfigs()));snapshot.setLastModified(System.currentTimeMillis());snapshot.setRevisionId(UUID.randomUUID().toString());@@ -952,6 +984,64 @@ public class LambdaService {return snapshot;}+ private static List<LambdaFileSystemConfig> parseFileSystemConfigs(Object value) {+ if (value == null) {+ return new ArrayList<>();+ }+ if (!(value instanceof List<?> configs)) {+ throw new AwsException("InvalidParameterValueException",+ "FileSystemConfigs must be a list", 400);+ }+ if (configs.size() > 1) {+ throw new AwsException("InvalidParameterValueException",+ "A Lambda function supports at most one file system configuration", 400);+ }++ List<LambdaFileSystemConfig> parsed = new ArrayList<>();+ for (Object rawConfig : configs) {+ if (!(rawConfig instanceof Map<?, ?> config)) {+ throw new AwsException("InvalidParameterValueException",+ "Each FileSystemConfigs entry must be an object", 400);+ }+ Object arnValue = config.get("Arn");+ Object mountPathValue = config.get("LocalMountPath");+ String arn = arnValue instanceof String string ? string : null;+ String localMountPath = mountPathValue instanceof String string ? string : null;+ if (arn == null || arn.isBlank() || arn.length() > 256) {+ throw new AwsException("InvalidParameterValueException",+ "File system Arn must be 1 to 256 characters", 400);+ }+ if (!EFS_ACCESS_POINT_ARN.matcher(arn).matches()) {+ throw new AwsException("InvalidParameterValueException",+ "File system Arn must identify an EFS access point", 400);+ }+ if (localMountPath == null || localMountPath.isBlank() || localMountPath.length() > 160+ || !FILE_SYSTEM_LOCAL_MOUNT_PATH.matcher(localMountPath).matches()) {+ throw new AwsException("InvalidParameterValueException",+ "LocalMountPath must be a directory directly under /mnt", 400);+ }+ parsed.add(new LambdaFileSystemConfig(arn, localMountPath));+ }+ return parsed;+ }++ private static void validateFileSystemVpcConfig(List<LambdaFileSystemConfig> fileSystemConfigs,+ Map<String, Object> vpcConfig) {+ if (fileSystemConfigs == null || fileSystemConfigs.isEmpty()) {+ return;+ }+ if (!hasValues(vpcConfig, "SubnetIds") || !hasValues(vpcConfig, "SecurityGroupIds")) {+ throw new AwsException("InvalidParameterValueException",+ "EFS file system access requires VpcConfig with subnets and security groups", 400);+ }+ }++ private static boolean hasValues(Map<String, Object> config, String key) {+ return config != null+ && config.get(key) instanceof List<?> values+ && !values.isEmpty();+ }+public List<LambdaFunction> listVersionsByFunction(String region, String functionName) {LambdaFunction fn = getFunction(region, functionName); // verify function existsreturn functionStore.listVersions(region, fn.getFunctionName());diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/WarmPool.java b/src/main/java/io/github/hectorvent/floci/services/lambda/WarmPool.javaindex b81829a83e..930fcc3190 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/WarmPool.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/WarmPool.java@@ -14,7 +14,9 @@ import org.jboss.logging.Logger;import java.util.ArrayDeque;import java.util.ArrayList;+import java.util.HashMap;import java.util.List;+import java.util.Map;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;@@ -41,10 +43,19 @@ public class WarmPool implements ContainerTeardown {private final ContainerLauncher containerLauncher;private final EmulatorConfig config;private final int maxPoolSizePerFunction;- private final ConcurrentHashMap<String, ArrayDeque<ContainerHandle>> pool = new ConcurrentHashMap<>();+ private final ConcurrentHashMap<String, PoolState> poolStates = new ConcurrentHashMap<>();+ private final ConcurrentHashMap<ContainerHandle, Lease> activeLeases = new ConcurrentHashMap<>();private final ScheduledExecutorService evictionScheduler = Executors.newSingleThreadScheduledExecutor(r -> { Thread t = new Thread(r, "warm-pool-evictor"); t.setDaemon(true); return t; });+ private static final class PoolState {+ private final Map<String, ArrayDeque<ContainerHandle>> idleByEnvironment = new HashMap<>();+ private final Map<String, Long> epochByEnvironment = new HashMap<>();+ }++ private record Lease(PoolState poolState, long epoch, String environmentKey) {+ }+@Injectpublic WarmPool(ContainerLauncher containerLauncher, EmulatorConfig config) {this.containerLauncher = containerLauncher;@@ -104,15 +115,22 @@ public class WarmPool implements ContainerTeardown {public ContainerHandle acquire(LambdaFunction fn) {boolean ephemeral = config != null && config.services().lambda().ephemeral();ContainerHandle handle = null;+ PoolState poolState = null;+ long leaseEpoch = 0;+ String environmentKey = executionEnvironmentKey(fn);if (!ephemeral) {- ArrayDeque<ContainerHandle> queue = pool.computeIfAbsent(fn.getFunctionName(), k -> new ArrayDeque<>());+ poolState = poolStates.computeIfAbsent(fn.getFunctionName(), ignored -> new PoolState());+ synchronized (poolState) {+ leaseEpoch = poolState.epochByEnvironment.computeIfAbsent(environmentKey, ignored -> 0L);+ }// Skip pooled handles whose container died out-of-band — otherwise the// caller would wait the full Lambda function timeout.while (true) {ContainerHandle candidate;- synchronized (queue) {- candidate = queue.pollFirst();+ synchronized (poolState) {+ ArrayDeque<ContainerHandle> idle = poolState.idleByEnvironment.get(environmentKey);+ candidate = idle == null ? null : idle.pollFirst();}if (candidate == null) {break;@@ -143,6 +161,9 @@ public class WarmPool implements ContainerTeardown {} else {LOG.debugv("Reusing warm container for function: {0}", fn.getFunctionName());}+ if (!ephemeral) {+ activeLeases.put(handle, new Lease(poolState, leaseEpoch, environmentKey));+ }handle.setState(ContainerState.BUSY);return handle;}@@ -153,6 +174,7 @@ public class WarmPool implements ContainerTeardown {* Otherwise it is returned to the warm pool.*/public void release(ContainerHandle handle) {+ Lease lease = activeLeases.remove(handle);boolean ephemeral = config != null && config.services().lambda().ephemeral();// An extension reporting an init/exit error is fatal to the execution environment in real// AWS. RuntimeApiServer already refuses new work at that point; the container is torn down@@ -171,17 +193,32 @@ public class WarmPool implements ContainerTeardown {return;}- handle.setState(ContainerState.WARM);- handle.touchLastUsed();- ArrayDeque<ContainerHandle> queue = pool.computeIfAbsent(handle.getFunctionName(), k -> new ArrayDeque<>());+ if (lease == null) {+ LOG.warnv("Container {0} for function {1} has no active warm-pool lease; stopping it",+ handle.getContainerId(), handle.getFunctionName());+ stopQuietly(handle);+ return;+ }++ boolean stale;boolean returned;- synchronized (queue) {- returned = queue.size() < maxPoolSizePerFunction;+ synchronized (lease.poolState()) {+ stale = lease.epoch() != lease.poolState().epochByEnvironment+ .getOrDefault(lease.environmentKey(), 0L);+ returned = !stale && idleSize(lease.poolState()) < maxPoolSizePerFunction;if (returned) {- queue.addFirst(handle);+ handle.setState(ContainerState.WARM);+ handle.touchLastUsed();+ lease.poolState().idleByEnvironment+ .computeIfAbsent(lease.environmentKey(), ignored -> new ArrayDeque<>())+ .addFirst(handle);}}- if (returned) {+ if (stale) {+ LOG.debugv("Pool was invalidated while container {0} was busy; stopping it",+ handle.getContainerId());+ stopQuietly(handle);+ } else if (returned) {LOG.debugv("Released container back to pool for function: {0}", handle.getFunctionName());} else {LOG.debugv("Pool full for function {0}, stopping excess container", handle.getFunctionName());@@ -196,7 +233,7 @@ public class WarmPool implements ContainerTeardown {public void pushCodeUpdate(LambdaFunction fn) {LOG.infov("Reactive S3 Sync: invalidating warm pool for function {0} to pick up new code",fn.getFunctionName());- drainFunction(fn.getFunctionName());+ drainEnvironment(fn);}/**@@ -205,31 +242,52 @@ public class WarmPool implements ContainerTeardown {* stop is needed — no pool bookkeeping required.*/public void destroyHandle(ContainerHandle handle) {+ activeLeases.remove(handle);LOG.debugv("Destroying timed-out container {0} for function {1}",handle.getContainerId(), handle.getFunctionName());stopQuietly(handle);}/**- * Stops and removes all warm containers for the given function.- * Called on function delete or code update.+ * Stops and removes warm containers for one immutable execution environment, such as+ * {@code $LATEST}. Published versions keep their independently keyed warm containers.*/- public void drainFunction(String functionName) {- ArrayDeque<ContainerHandle> queue = pool.remove(functionName);- if (queue == null) {- return;+ public void drainEnvironment(LambdaFunction fn) {+ String functionName = fn.getFunctionName();+ String environmentKey = executionEnvironmentKey(fn);+ PoolState poolState = poolStates.computeIfAbsent(functionName, ignored -> new PoolState());+ List<ContainerHandle> toStop;+ synchronized (poolState) {+ poolState.epochByEnvironment.merge(environmentKey, 1L, Long::sum);+ ArrayDeque<ContainerHandle> idle = poolState.idleByEnvironment.remove(environmentKey);+ toStop = idle == null ? List.of() : new ArrayList<>(idle);}+ LOG.infov("Draining {0} container(s) for Lambda environment: {1}",+ toStop.size(), environmentKey);+ stopInParallel(toStop);+ }++ /**+ * Stops and removes all warm containers for every environment of the given function.+ * Called on function deletion and emulator shutdown.+ */+ public void drainFunction(String functionName) {+ PoolState poolState = poolStates.computeIfAbsent(functionName, ignored -> new PoolState());List<ContainerHandle> toStop;- synchronized (queue) {- toStop = new ArrayList<>(queue);- queue.clear();+ synchronized (poolState) {+ poolState.epochByEnvironment.replaceAll((ignored, epoch) -> epoch + 1);+ toStop = new ArrayList<>();+ poolState.idleByEnvironment.values().forEach(toStop::addAll);+ poolState.idleByEnvironment.clear();}LOG.infov("Draining {0} container(s) for function: {1}", toStop.size(), functionName);stopInParallel(toStop);}private void stopInParallel(List<ContainerHandle> handles) {- if (handles.isEmpty()) return;+ if (handles.isEmpty()) {+ return;+ }int parallelism = Math.min(handles.size(), 16);ExecutorService pool = Executors.newFixedThreadPool(parallelism,r -> { Thread t = new Thread(r, "warm-pool-drainer"); t.setDaemon(true); return t; });@@ -251,34 +309,38 @@ public class WarmPool implements ContainerTeardown {}private void drainAll() {- for (String functionName : new ArrayList<>(pool.keySet())) {+ for (String functionName : new ArrayList<>(poolStates.keySet())) {drainFunction(functionName);}}private void evictIdleContainers() {- if (config == null) return;+ if (config == null) {+ return;+ }long idleTimeoutMs = config.services().lambda().containerIdleTimeoutSeconds() * 1000L;long now = System.currentTimeMillis();- for (var entry : pool.entrySet()) {+ for (var entry : poolStates.entrySet()) {String functionName = entry.getKey();- ArrayDeque<ContainerHandle> queue = entry.getValue();+ PoolState poolState = entry.getValue();List<ContainerHandle> toEvict = new ArrayList<>();- synchronized (queue) {- queue.removeIf(handle -> {- if (handle.getState() == ContainerState.WARM- && (now - handle.getLastUsedMs()) >= idleTimeoutMs) {- toEvict.add(handle);- return true;- }- return false;- });+ synchronized (poolState) {+ for (ArrayDeque<ContainerHandle> idle : poolState.idleByEnvironment.values()) {+ idle.removeIf(handle -> {+ if (handle.getState() == ContainerState.WARM+ && (now - handle.getLastUsedMs()) >= idleTimeoutMs) {+ toEvict.add(handle);+ return true;+ }+ return false;+ });+ }+ poolState.idleByEnvironment.values().removeIf(ArrayDeque::isEmpty);}- // Re-check that the queue is still registered to avoid double-stop with drainFunction.- if (!toEvict.isEmpty() && pool.get(functionName) == queue) {+ if (!toEvict.isEmpty()) {LOG.infov("Evicting {0} idle container(s) for function: {1}", toEvict.size(), functionName);for (ContainerHandle handle : toEvict) {stopQuietly(handle);@@ -287,6 +349,22 @@ public class WarmPool implements ContainerTeardown {}}+ private static String executionEnvironmentKey(LambdaFunction fn) {+ String functionArn = fn.getFunctionArn();+ if (functionArn != null && !functionArn.isBlank()) {+ return functionArn;+ }+ return fn.getFunctionName() + ":" + fn.getVersion();+ }++ private static int idleSize(PoolState poolState) {+ int size = 0;+ for (ArrayDeque<ContainerHandle> idle : poolState.idleByEnvironment.values()) {+ size += idle.size();+ }+ return size;+ }+private void stopQuietly(ContainerHandle handle) {try {containerLauncher.stop(handle);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 e228aa671e..94df0c6596 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@@ -32,13 +32,17 @@ import java.io.Closeable;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;+import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Path;+import java.security.MessageDigest;+import java.security.NoSuchAlgorithmException;import java.security.cert.CertificateFactory;import java.security.cert.X509Certificate;import java.time.LocalDate;import java.time.format.DateTimeFormatter;import java.util.ArrayList;+import java.util.HexFormat;import java.util.List;import java.util.Optional;import java.util.concurrent.ExecutionException;@@ -274,6 +278,25 @@ public class ContainerLauncher {// enough that a shared volume's helper round-trip would only add cold-start latency.}+ if (fn.getFileSystemConfigs() != null && !fn.getFileSystemConfigs().isEmpty()) {+ var efsCfg = config.storage().efs();+ fn.getFileSystemConfigs().forEach(fileSystem -> {+ String volumeName = efsVolumeName(fileSystem.getArn());+ lifecycleManager.ensureSharedVolume(volumeName,+ efsCfg.ownerUid(), efsCfg.ownerGid(), efsCfg.rootPermissions(),+ efsCfg.initImage());+ specBuilder.withNamedVolume(volumeName, fileSystem.getLocalMountPath(), false);+ });+ efsCfg.mountUser().ifPresent(user -> {+ if (!user.matches("^\\d+(:\\d+)?$")) {+ throw new IllegalArgumentException(+ "floci.storage.efs.mount-user must be \"uid\" or \"uid:gid\": " + user);+ }+ specBuilder.withUser(user);+ });+ efsCfg.mountGroupAdd().ifPresent(gid -> specBuilder.withGroupAdd(String.valueOf(gid)));+ }+// For Image package type use ImageConfig.Command/EntryPoint/WorkingDirectory if set, otherwise fall back to Handler (Zip-style)if ("Image".equals(fn.getPackageType())) {if (fn.getImageConfigEntryPoint() != null && !fn.getImageConfigEntryPoint().isEmpty()) {@@ -766,6 +789,25 @@ public class ContainerLauncher {return "floci-code-" + fname + "-" + h;}+ static String efsVolumeName(String accessPointArn) {+ int separator = Math.max(accessPointArn.lastIndexOf('/'), accessPointArn.lastIndexOf(':'));+ String resourceId = separator >= 0 ? accessPointArn.substring(separator + 1) : accessPointArn;+ if (resourceId.isBlank()) {+ throw new IllegalArgumentException("File system access point ARN must include a resource id");+ }+ return "floci-efs-" + resourceId + "-" + sha256Hex(accessPointArn);+ }++ private static String sha256Hex(String value) {+ try {+ byte[] digest = MessageDigest.getInstance("SHA-256")+ .digest(value.getBytes(StandardCharsets.UTF_8));+ return HexFormat.of().formatHex(digest);+ } catch (NoSuchAlgorithmException e) {+ throw new IllegalStateException("SHA-256 is required but not available", e);+ }+ }+/*** Buffer for the tar-streaming pipe. The default {@link java.io.PipedInputStream} buffer is* only 1KB, which forces a writer/reader thread hand-off (wait/notify) every 1KB. Streaming adiff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/model/LambdaFileSystemConfig.java b/src/main/java/io/github/hectorvent/floci/services/lambda/model/LambdaFileSystemConfig.javanew file mode 100644index 0000000000..00c688cc5c--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/model/LambdaFileSystemConfig.java@@ -0,0 +1,34 @@+package io.github.hectorvent.floci.services.lambda.model;++import io.quarkus.runtime.annotations.RegisterForReflection;++@RegisterForReflection+public class LambdaFileSystemConfig {++ private String arn;+ private String localMountPath;++ public LambdaFileSystemConfig() {+ }++ public LambdaFileSystemConfig(String arn, String localMountPath) {+ this.arn = arn;+ this.localMountPath = localMountPath;+ }++ public String getArn() {+ return arn;+ }++ public void setArn(String arn) {+ this.arn = arn;+ }++ public String getLocalMountPath() {+ return localMountPath;+ }++ public void setLocalMountPath(String localMountPath) {+ this.localMountPath = localMountPath;+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/model/LambdaFunction.java b/src/main/java/io/github/hectorvent/floci/services/lambda/model/LambdaFunction.javaindex 0ca6751343..2de33e447e 100644--- a/src/main/java/io/github/hectorvent/floci/services/lambda/model/LambdaFunction.java+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/model/LambdaFunction.java@@ -49,6 +49,7 @@ public class LambdaFunction {private List<String> layers = new ArrayList<>();private String kmsKeyArn;private Map<String, Object> vpcConfig;+ private List<LambdaFileSystemConfig> fileSystemConfigs = new ArrayList<>();private String codeSha256;/** Non-null only for hot-reload functions. Holds the Docker-host path bind-mounted into /var/task. */@@ -168,6 +169,11 @@ public class LambdaFunction {public Map<String, Object> getVpcConfig() { return vpcConfig; }public void setVpcConfig(Map<String, Object> vpcConfig) { this.vpcConfig = vpcConfig; }+ public List<LambdaFileSystemConfig> getFileSystemConfigs() { return fileSystemConfigs; }+ public void setFileSystemConfigs(List<LambdaFileSystemConfig> fileSystemConfigs) {+ this.fileSystemConfigs = fileSystemConfigs == null ? new ArrayList<>() : new ArrayList<>(fileSystemConfigs);+ }+public String getCodeSha256() { return codeSha256; }public void setCodeSha256(String codeSha256) { this.codeSha256 = codeSha256; }diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.javaindex a1f24c2c64..83193b9611 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationIntegrationTest.java@@ -882,6 +882,144 @@ class CloudFormationIntegrationTest {""".formatted(functionName);}+ @Test+ void updateStack_lambdaFileSystemConfigUpdatesInPlace() {+ String stackName = "cfn-lambda-efs-config-stack";+ String functionName = "cfn-lambda-efs-config-func";+ String accessPointArn =+ "arn:aws:elasticfilesystem:us-east-1:000000000000:access-point/fsap-0123456789abcdef0";++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateStack")+ .formParam("StackName", stackName)+ .formParam("TemplateBody", lambdaFileSystemTemplate(functionName, accessPointArn, true))+ .when()+ .post("/")+ .then()+ .statusCode(200);++ given()+ .when()+ .get("/2015-03-31/functions/" + functionName)+ .then()+ .statusCode(200)+ .body("Configuration.FileSystemConfigs[0].Arn", equalTo(accessPointArn))+ .body("Configuration.FileSystemConfigs[0].LocalMountPath", equalTo("/mnt/shared"));++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "UpdateStack")+ .formParam("StackName", stackName)+ .formParam("TemplateBody", lambdaFileSystemTemplate(functionName, accessPointArn, false))+ .when()+ .post("/")+ .then()+ .statusCode(200);++ String resourceXml = given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "DescribeStackResources")+ .formParam("StackName", stackName)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .extract().asString();+ assertThat(firstPhysicalResourceId(resourceXml), equalTo(functionName));++ given()+ .when()+ .get("/2015-03-31/functions/" + functionName)+ .then()+ .statusCode(200)+ .body("Configuration.FileSystemConfigs", nullValue());+ }++ @Test+ void createStack_lambdaFileSystemConfigsMustBeAList() {+ String stackName = "cfn-lambda-invalid-efs-config-stack";+ String template = """+ {+ "Resources": {+ "MyFunction": {+ "Type": "AWS::Lambda::Function",+ "Properties": {+ "FunctionName": "cfn-lambda-invalid-efs-config",+ "Runtime": "nodejs20.x",+ "Handler": "index.handler",+ "Role": "arn:aws:iam::000000000000:role/cfn-test-lambda-role",+ "Code": {+ "ZipFile": "exports.handler = async () => ({ statusCode: 200 });"+ },+ "VpcConfig": {+ "SubnetIds": ["subnet-12345678"],+ "SecurityGroupIds": ["sg-12345678"]+ },+ "FileSystemConfigs": {+ "Arn": "arn:aws:elasticfilesystem:us-east-1:000000000000:access-point/fsap-0123456789abcdef0",+ "LocalMountPath": "/mnt/shared"+ }+ }+ }+ }+ }+ """;++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "CreateStack")+ .formParam("StackName", stackName)+ .formParam("TemplateBody", template)+ .when()+ .post("/")+ .then()+ .statusCode(200);++ given()+ .contentType("application/x-www-form-urlencoded")+ .formParam("Action", "DescribeStackEvents")+ .formParam("StackName", stackName)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("ROLLBACK_COMPLETE"))+ .body(containsString("FileSystemConfigs must be a list"));+ }++ private static String lambdaFileSystemTemplate(String functionName, String accessPointArn,+ boolean includeFileSystem) {+ String fileSystemConfig = includeFileSystem+ ? """+ ,"FileSystemConfigs": [{+ "Arn": "%s",+ "LocalMountPath": "/mnt/shared"+ }]+ """.formatted(accessPointArn)+ : "";+ return """+ {+ "Resources": {+ "MyFunction": {+ "Type": "AWS::Lambda::Function",+ "Properties": {+ "FunctionName": "%s",+ "Runtime": "nodejs20.x",+ "Handler": "index.handler",+ "Role": "arn:aws:iam::000000000000:role/cfn-test-lambda-role",+ "VpcConfig": {+ "SubnetIds": ["subnet-0123456789abcdef0"],+ "SecurityGroupIds": ["sg-0123456789abcdef0"]+ }+ %s+ }+ }+ }+ }+ """.formatted(functionName, fileSystemConfig);+ }+@Testvoid createStack_kmsKeyWithOverrideTagUsesPinnedId() {String template = """diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.javaindex a7abb8e28f..ed70a141ca 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/SamTransformProcessorTest.java@@ -136,6 +136,46 @@ class SamTransformProcessorTest {assertTrue(lambdaProps.path("Runtime").isMissingNode());}+ @Test+ void expandSamTemplate_functionWithFileSystemConfig() throws Exception {+ JsonNode template = objectMapper.readTree("""+ {+ "Transform": "AWS::Serverless-2016-10-31",+ "Resources": {+ "MyFunc": {+ "Type": "AWS::Serverless::Function",+ "Properties": {+ "Handler": "index.handler",+ "Runtime": "nodejs20.x",+ "InlineCode": "exports.handler = async () => ({});",+ "VpcConfig": {+ "SubnetIds": ["subnet-0123456789abcdef0"],+ "SecurityGroupIds": ["sg-0123456789abcdef0"]+ },+ "FileSystemConfigs": [{+ "Arn": "arn:aws:elasticfilesystem:us-east-1:000000000000:access-point/fsap-0123456789abcdef0",+ "LocalMountPath": "/mnt/shared"+ }]+ }+ }+ }+ }+ """);++ JsonNode lambdaProps = processor.expandSamTemplate(template)+ .path("Resources").path("MyFunc").path("Properties");++ assertEquals("subnet-0123456789abcdef0",+ lambdaProps.path("VpcConfig").path("SubnetIds").get(0).asText());+ assertEquals("sg-0123456789abcdef0",+ lambdaProps.path("VpcConfig").path("SecurityGroupIds").get(0).asText());+ assertEquals("arn:aws:elasticfilesystem:us-east-1:000000000000:"+ + "access-point/fsap-0123456789abcdef0",+ lambdaProps.path("FileSystemConfigs").get(0).path("Arn").asText());+ assertEquals("/mnt/shared",+ lambdaProps.path("FileSystemConfigs").get(0).path("LocalMountPath").asText());+ }+@Testvoid expandSamTemplate_functionWithImageConfig() throws Exception {// ImageConfig (EntryPoint/Command/WorkingDirectory overrides for a container-imagediff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbProjectionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbProjectionIntegrationTest.javaindex ca450c477c..899e63d7ce 100644--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbProjectionIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbProjectionIntegrationTest.java@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.node.ObjectNode;import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;import io.quarkus.test.junit.QuarkusTest;+import io.restassured.RestAssured;import org.junit.jupiter.api.AfterAll;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.MethodOrderer;@@ -28,6 +29,7 @@ class DynamoDbProjectionIntegrationTest {private static final String CT = "application/x-amz-json-1.0";private static final String TABLE = "ProjectionBugTable";+ private static int testPort;@BeforeAllstatic void configureRestAssured() {@@ -37,6 +39,7 @@ class DynamoDbProjectionIntegrationTest {@Test@Order(1)void createTable() {+ testPort = RestAssured.port;given().header("X-Amz-Target", "DynamoDB_20120810.CreateTable").contentType(CT)@@ -169,8 +172,8 @@ class DynamoDbProjectionIntegrationTest {@AfterAllstatic void cleanup() {- try {- given()+ given()+ .port(testPort).header("X-Amz-Target", "DynamoDB_20120810.DeleteTable").contentType(CT).body("""@@ -180,7 +183,6 @@ class DynamoDbProjectionIntegrationTest {.post("/").then().statusCode(200);- } catch (Exception ignored) {}}/**diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.javaindex 9dfcb638a5..ed3ba357fd 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;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.LambdaFileSystemConfig;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;@@ -49,6 +50,12 @@ class LambdaServiceTest {));}+ private static Map<String, Object> vpcConfig() {+ return Map.of(+ "SubnetIds", List.of("subnet-0123456789abcdef0"),+ "SecurityGroupIds", List.of("sg-0123456789abcdef0"));+ }+@Testvoid createFunctionSucceeds() {LambdaFunction fn = service.createFunction(REGION, baseRequest("my-function"));@@ -64,6 +71,151 @@ class LambdaServiceTest {assertNotNull(fn.getRevisionId());}+ @Test+ void createAndUpdateFunctionFileSystemConfig() {+ Map<String, Object> request = baseRequest("efs-function");+ request.put("VpcConfig", vpcConfig());+ request.put("FileSystemConfigs", List.of(Map.of(+ "Arn", "arn:aws:elasticfilesystem:us-east-1:000000000000:access-point/fsap-0123456789abcdef0",+ "LocalMountPath", "/mnt/shared")));++ LambdaFunction created = service.createFunction(REGION, request);+ assertEquals(1, created.getFileSystemConfigs().size());+ assertEquals("arn:aws:elasticfilesystem:us-east-1:000000000000:access-point/fsap-0123456789abcdef0",+ created.getFileSystemConfigs().getFirst().getArn());+ assertEquals("/mnt/shared", created.getFileSystemConfigs().getFirst().getLocalMountPath());++ LambdaFunction updated = service.updateFunctionConfiguration(REGION, "efs-function",+ Map.of("FileSystemConfigs", List.of()));+ assertTrue(updated.getFileSystemConfigs().isEmpty());+ }++ @Test+ void publishedVersionKeepsFileSystemConfigSnapshot() {+ Map<String, Object> request = baseRequest("versioned-efs-function");+ request.put("VpcConfig", vpcConfig());+ request.put("FileSystemConfigs", List.of(Map.of(+ "Arn", "arn:aws:elasticfilesystem:us-east-1:000000000000:access-point/fsap-0123456789abcdef0",+ "LocalMountPath", "/mnt/shared")));++ service.createFunction(REGION, request);+ LambdaFunction version = service.publishVersion(REGION, "versioned-efs-function", null);+ service.updateFunctionConfiguration(REGION, "versioned-efs-function",+ Map.of(+ "FileSystemConfigs", List.of(),+ "VpcConfig", Map.of(+ "SubnetIds", List.of("subnet-updated"),+ "SecurityGroupIds", List.of("sg-updated"))));++ assertEquals(vpcConfig(), version.getVpcConfig());+ assertEquals(1, version.getFileSystemConfigs().size());+ LambdaFileSystemConfig fileSystem = version.getFileSystemConfigs().getFirst();+ assertEquals("arn:aws:elasticfilesystem:us-east-1:000000000000:access-point/fsap-0123456789abcdef0",+ fileSystem.getArn());+ assertEquals("/mnt/shared", fileSystem.getLocalMountPath());+ }++ @Test+ void createFunctionRejectsInvalidFileSystemMountPath() {+ Map<String, Object> request = baseRequest("invalid-efs-function");+ request.put("FileSystemConfigs", List.of(Map.of(+ "Arn", "arn:aws:elasticfilesystem:us-east-1:000000000000:access-point/fsap-0123456789abcdef0",+ "LocalMountPath", "/tmp/shared")));++ AwsException error = assertThrows(AwsException.class,+ () -> service.createFunction(REGION, request));+ assertEquals("InvalidParameterValueException", error.getErrorCode());+ }++ @Test+ void createFunctionRejectsInvalidFileSystemAccessPointArn() {+ Map<String, Object> request = baseRequest("invalid-efs-arn-function");+ request.put("FileSystemConfigs", List.of(Map.of(+ "Arn", "arn:aws:elasticfilesystem:us-east-1:000000000000:file-system/fs-0123456789abcdef0",+ "LocalMountPath", "/mnt/shared")));++ AwsException error = assertThrows(AwsException.class,+ () -> service.createFunction(REGION, request));+ assertEquals("InvalidParameterValueException", error.getErrorCode());+ }++ @Test+ void createFunctionRejectsMoreThanOneFileSystemConfig() {+ Map<String, Object> request = baseRequest("too-many-efs-function");+ request.put("FileSystemConfigs", List.of(+ Map.of(+ "Arn", "arn:aws:elasticfilesystem:us-east-1:000000000000:access-point/fsap-0123456789abcdef0",+ "LocalMountPath", "/mnt/shared"),+ Map.of(+ "Arn", "arn:aws:elasticfilesystem:us-east-1:000000000000:access-point/fsap-0123456789abcdef1",+ "LocalMountPath", "/mnt/other")));++ AwsException error = assertThrows(AwsException.class,+ () -> service.createFunction(REGION, request));+ assertEquals("InvalidParameterValueException", error.getErrorCode());+ }++ @Test+ void createFunctionRejectsUnsupportedS3FilesAccessPointArn() {+ Map<String, Object> request = baseRequest("s3-files-function");+ String arn = "arn:aws:s3files:us-east-1:000000000000:"+ + "file-system/fs-0123456789abcdef0/access-point/fsap-0123456789abcdef0";+ request.put("FileSystemConfigs", List.of(Map.of(+ "Arn", arn,+ "LocalMountPath", "/mnt/shared")));++ AwsException error = assertThrows(AwsException.class,+ () -> service.createFunction(REGION, request));+ assertEquals("InvalidParameterValueException", error.getErrorCode());+ }++ @Test+ void createFunctionRejectsFileSystemConfigWithoutVpcConfig() {+ Map<String, Object> request = baseRequest("efs-without-vpc-function");+ request.put("FileSystemConfigs", List.of(Map.of(+ "Arn", "arn:aws:elasticfilesystem:us-east-1:000000000000:access-point/fsap-0123456789abcdef0",+ "LocalMountPath", "/mnt/shared")));++ AwsException error = assertThrows(AwsException.class,+ () -> service.createFunction(REGION, request));+ assertEquals("InvalidParameterValueException", error.getErrorCode());+ }++ @Test+ void updateFunctionAcceptsVpcAndFileSystemConfigTogether() {+ service.createFunction(REGION, baseRequest("update-efs-function"));++ LambdaFunction updated = service.updateFunctionConfiguration(REGION, "update-efs-function",+ Map.of(+ "VpcConfig", vpcConfig(),+ "FileSystemConfigs", List.of(Map.of(+ "Arn", "arn:aws:elasticfilesystem:us-east-1:000000000000:"+ + "access-point/fsap-0123456789abcdef0",+ "LocalMountPath", "/mnt/shared"))));++ assertEquals(1, updated.getFileSystemConfigs().size());+ assertEquals(vpcConfig(), updated.getVpcConfig());+ }++ @Test+ void updateFunctionRejectsRemovingVpcWhileFileSystemConfigRemains() {+ Map<String, Object> request = baseRequest("update-efs-vpc-function");+ request.put("VpcConfig", vpcConfig());+ request.put("FileSystemConfigs", List.of(Map.of(+ "Arn", "arn:aws:elasticfilesystem:us-east-1:000000000000:access-point/fsap-0123456789abcdef0",+ "LocalMountPath", "/mnt/shared")));+ service.createFunction(REGION, request);++ AwsException error = assertThrows(AwsException.class,+ () -> service.updateFunctionConfiguration(REGION, "update-efs-vpc-function",+ Map.of("VpcConfig", Map.of(+ "SubnetIds", List.of(),+ "SecurityGroupIds", List.of()))));++ assertEquals("InvalidParameterValueException", error.getErrorCode());+ assertEquals(vpcConfig(), service.getFunction(REGION, "update-efs-vpc-function").getVpcConfig());+ }+@Testvoid createFunctionFailsWhenMissingFunctionName() {Map<String, Object> req = baseRequest("x");diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/WarmPoolTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/WarmPoolTest.javaindex 1315055ece..bf40d92f8e 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambda/WarmPoolTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/WarmPoolTest.java@@ -11,9 +11,16 @@ import org.junit.jupiter.api.extension.ExtendWith;import org.mockito.Mock;import org.mockito.junit.jupiter.MockitoExtension;+import java.util.concurrent.CountDownLatch;+import java.util.concurrent.ExecutorService;+import java.util.concurrent.Executors;+import java.util.concurrent.Future;+import java.util.concurrent.TimeUnit;+import static org.junit.jupiter.api.Assertions.assertEquals;-import static org.junit.jupiter.api.Assertions.assertSame;import static org.junit.jupiter.api.Assertions.assertNotSame;+import static org.junit.jupiter.api.Assertions.assertSame;+import static org.junit.jupiter.api.Assertions.assertTrue;import static org.mockito.ArgumentMatchers.any;import static org.mockito.Mockito.lenient;import static org.mockito.Mockito.mock;@@ -233,6 +240,82 @@ class WarmPoolTest {pool.shutdown();}+ @Test+ void versionsUseSeparateWarmPools() {+ WarmPool pool = buildPool();+ pool.init();++ LambdaFunction latest = mock(LambdaFunction.class);+ LambdaFunction version = mock(LambdaFunction.class);+ when(latest.getFunctionName()).thenReturn("versioned-fn");+ when(version.getFunctionName()).thenReturn("versioned-fn");+ when(latest.getFunctionArn())+ .thenReturn("arn:aws:lambda:us-east-1:000000000000:function:versioned-fn");+ when(version.getFunctionArn())+ .thenReturn("arn:aws:lambda:us-east-1:000000000000:function:versioned-fn:1");++ ContainerHandle latestHandle = new ContainerHandle(+ "cid-latest", "versioned-fn", null, ContainerState.WARM);+ ContainerHandle versionHandle = new ContainerHandle(+ "cid-version", "versioned-fn", null, ContainerState.WARM);+ when(containerLauncher.launch(any())).thenReturn(latestHandle, versionHandle);+ when(containerLauncher.isAlive(latestHandle)).thenReturn(true);+ when(containerLauncher.isAlive(versionHandle)).thenReturn(true);++ pool.release(pool.acquire(latest));+ ContainerHandle firstVersion = pool.acquire(version);+ assertSame(versionHandle, firstVersion);+ verify(containerLauncher, times(2)).launch(any());+ pool.release(firstVersion);++ ContainerHandle reacquiredLatest = pool.acquire(latest);+ ContainerHandle reacquiredVersion = pool.acquire(version);+ assertSame(latestHandle, reacquiredLatest);+ assertSame(versionHandle, reacquiredVersion);+ verify(containerLauncher, times(2)).launch(any());++ pool.release(reacquiredLatest);+ pool.release(reacquiredVersion);+ pool.shutdown();+ }++ @Test+ void drainingLatestPreservesPublishedVersionPool() {+ WarmPool pool = buildPool();+ pool.init();++ LambdaFunction latest = mock(LambdaFunction.class);+ LambdaFunction version = mock(LambdaFunction.class);+ when(latest.getFunctionName()).thenReturn("versioned-drain-fn");+ when(version.getFunctionName()).thenReturn("versioned-drain-fn");+ when(latest.getFunctionArn())+ .thenReturn("arn:aws:lambda:us-east-1:000000000000:function:versioned-drain-fn");+ when(version.getFunctionArn())+ .thenReturn("arn:aws:lambda:us-east-1:000000000000:function:versioned-drain-fn:1");++ ContainerHandle latestHandle = new ContainerHandle(+ "cid-drain-latest", "versioned-drain-fn", null, ContainerState.WARM);+ ContainerHandle versionHandle = new ContainerHandle(+ "cid-drain-version", "versioned-drain-fn", null, ContainerState.WARM);+ ContainerHandle refreshedLatest = new ContainerHandle(+ "cid-drain-latest-fresh", "versioned-drain-fn", null, ContainerState.WARM);+ when(containerLauncher.launch(any())).thenReturn(latestHandle, versionHandle, refreshedLatest);+ when(containerLauncher.isAlive(versionHandle)).thenReturn(true);++ pool.release(pool.acquire(latest));+ pool.release(pool.acquire(version));++ pool.drainEnvironment(latest);++ verify(containerLauncher).stop(latestHandle);+ verify(containerLauncher, never()).stop(versionHandle);+ assertSame(versionHandle, pool.acquire(version));+ assertSame(refreshedLatest, pool.acquire(latest));+ verify(containerLauncher, times(3)).launch(any());++ pool.shutdown();+ }+@Testvoid acquire_discardsDeadPooledHandleAndColdStarts() {WarmPool pool = buildPool();@@ -298,4 +381,74 @@ class WarmPoolTest {pool.shutdown();}++ @Test+ void releaseAfterDrainStopsStaleHandleAndColdStarts() {+ WarmPool pool = buildPool();+ pool.init();++ LambdaFunction fn = mock(LambdaFunction.class);+ when(fn.getFunctionName()).thenReturn("updated-fn");+ ContainerHandle stale = new ContainerHandle(+ "cid-stale", "updated-fn", null, ContainerState.WARM);+ ContainerHandle fresh = new ContainerHandle(+ "cid-fresh", "updated-fn", null, ContainerState.WARM);+ when(containerLauncher.launch(any())).thenReturn(stale, fresh);++ ContainerHandle acquired = pool.acquire(fn);+ pool.drainFunction("updated-fn");+ pool.release(acquired);++ verify(containerLauncher).stop(stale);+ assertSame(fresh, pool.acquire(fn));+ verify(containerLauncher, times(2)).launch(any());++ pool.shutdown();+ }++ @Test+ void launchCrossingDrainIsNeverReturnedToPool() throws Exception {+ WarmPool pool = buildPool();+ pool.init();++ LambdaFunction fn = mock(LambdaFunction.class);+ when(fn.getFunctionName()).thenReturn("racing-fn");+ ContainerHandle stale = new ContainerHandle(+ "cid-racing-stale", "racing-fn", null, ContainerState.WARM);+ ContainerHandle fresh = new ContainerHandle(+ "cid-racing-fresh", "racing-fn", null, ContainerState.WARM);+ CountDownLatch launchStarted = new CountDownLatch(1);+ CountDownLatch finishLaunch = new CountDownLatch(1);+ when(containerLauncher.launch(any()))+ .thenAnswer(invocation -> {+ launchStarted.countDown();+ assertTrue(finishLaunch.await(5, TimeUnit.SECONDS));+ return stale;+ })+ .thenReturn(fresh);++ ExecutorService executor = Executors.newSingleThreadExecutor();+ try {+ Future<ContainerHandle> acquisition = executor.submit(() -> pool.acquire(fn));+ assertTrue(launchStarted.await(5, TimeUnit.SECONDS));++ pool.drainFunction("racing-fn");+ finishLaunch.countDown();+ ContainerHandle acquired = acquisition.get(5, TimeUnit.SECONDS);+ pool.release(acquired);++ verify(containerLauncher).stop(stale);+ ContainerHandle postDrain = pool.acquire(fn);+ assertSame(fresh, postDrain);++ when(containerLauncher.isAlive(fresh)).thenReturn(true);+ pool.release(postDrain);+ assertSame(fresh, pool.acquire(fn));+ verify(containerLauncher, times(2)).launch(any());+ } finally {+ finishLaunch.countDown();+ executor.shutdownNow();+ pool.shutdown();+ }+ }}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 5e89c941f2..918b230a65 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@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;+import io.github.hectorvent.floci.services.lambda.model.LambdaFileSystemConfig;import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServer;import io.github.hectorvent.floci.services.lambda.runtime.RuntimeApiServerFactory;@@ -45,6 +46,7 @@ import java.nio.file.Path;import java.util.List;import java.util.Map;import java.util.Optional;+import java.util.OptionalInt;import java.util.concurrent.atomic.AtomicBoolean;import static org.junit.jupiter.api.Assertions.assertEquals;@@ -85,6 +87,8 @@ class ContainerLauncherTest {EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);EmulatorConfig.LambdaServiceConfig lambda = mock(EmulatorConfig.LambdaServiceConfig.class);EmulatorConfig.DockerConfig docker = mock(EmulatorConfig.DockerConfig.class);+ EmulatorConfig.StorageConfig storage = mock(EmulatorConfig.StorageConfig.class);+ EmulatorConfig.EfsSharingConfig efs = mock(EmulatorConfig.EfsSharingConfig.class);when(config.services()).thenReturn(services);when(services.lambda()).thenReturn(lambda);@@ -100,6 +104,14 @@ class ContainerLauncherTest {lenient().when(tls.enabled()).thenReturn(false);lenient().when(config.defaultRegion()).thenReturn("us-east-1");lenient().when(config.hostname()).thenReturn(Optional.empty());+ lenient().when(config.storage()).thenReturn(storage);+ lenient().when(storage.efs()).thenReturn(efs);+ lenient().when(efs.ownerUid()).thenReturn(OptionalInt.empty());+ lenient().when(efs.ownerGid()).thenReturn(OptionalInt.empty());+ lenient().when(efs.rootPermissions()).thenReturn(Optional.empty());+ lenient().when(efs.initImage()).thenReturn("busybox:stable");+ lenient().when(efs.mountUser()).thenReturn(Optional.empty());+ lenient().when(efs.mountGroupAdd()).thenReturn(OptionalInt.empty());when(embeddedDnsServer.getServerIp()).thenReturn(Optional.empty());@@ -209,6 +221,35 @@ class ContainerLauncherTest {"small code should be copied directly into /var/task");}+ @Test+ void launchFunction_mountsConfiguredFileSystemVolume() throws Exception {+ Path codePath = Files.createDirectory(tempDir.resolve("efs-code"));++ LambdaFunction fn = new LambdaFunction();+ fn.setFunctionName("efs-fn");+ fn.setFunctionArn("arn:aws:lambda:us-east-1:000000000000:function:efs-fn");+ fn.setRuntime("nodejs20.x");+ fn.setHandler("index.handler");+ fn.setCodeLocalPath(codePath.toString());+ fn.setFileSystemConfigs(List.of(new LambdaFileSystemConfig(+ "arn:aws:elasticfilesystem:us-east-1:000000000000:access-point/fsap-0123456789abcdef0",+ "/mnt/shared")));++ launcher.launch(fn);++ String expectedVolumeName = "floci-efs-fsap-0123456789abcdef0-"+ + "9d6eafd2aec94d4518a004f005725b4b3c673c1506436bb7368cfd5450fc0810";+ verify(lifecycleManager).ensureSharedVolume(expectedVolumeName,+ OptionalInt.empty(), OptionalInt.empty(), Optional.empty(), "busybox:stable");+ Mount mount = captureRealContainerSpec().mounts().stream()+ .filter(candidate -> "/mnt/shared".equals(candidate.getTarget()))+ .findFirst()+ .orElseThrow();+ assertEquals(MountType.VOLUME, mount.getType());+ assertEquals(expectedVolumeName, mount.getSource());+ assertTrue(!Boolean.TRUE.equals(mount.getReadOnly()));+ }+@Testvoid launchFunction_createsBeforeCopyAndStartsAfter() throws Exception {Path codePath = Files.createDirectory(tempDir.resolve("code"));diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherVolumeNamingTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherVolumeNamingTest.javaindex bb0c323447..377b45c6a7 100644--- a/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherVolumeNamingTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncherVolumeNamingTest.java@@ -100,6 +100,33 @@ class ContainerLauncherVolumeNamingTest {"a later lastModified must produce a different volume name");}+ @Test+ void efsVolumeNameIncludesStableHashOfCompleteArn() {+ String arn = "arn:aws:elasticfilesystem:us-east-1:000000000000:"+ + "access-point/fsap-0123456789abcdef0";++ String volumeName = ContainerLauncher.efsVolumeName(arn);++ assertEquals("floci-efs-fsap-0123456789abcdef0-"+ + "9d6eafd2aec94d4518a004f005725b4b3c673c1506436bb7368cfd5450fc0810",+ volumeName);+ assertEquals(volumeName, ContainerLauncher.efsVolumeName(arn));+ assertTrue(DOCKER_VOLUME_NAME.matcher(volumeName).matches(),+ "EFS volume name must be Docker-safe, was: " + volumeName);+ }++ @Test+ void efsVolumeNameDistinguishesSameResourceIdAcrossArns() {+ String eastArn = "arn:aws:elasticfilesystem:us-east-1:000000000000:"+ + "access-point/fsap-0123456789abcdef0";+ String westArn = "arn:aws:elasticfilesystem:us-west-2:111111111111:"+ + "access-point/fsap-0123456789abcdef0";++ assertNotEquals(ContainerLauncher.efsVolumeName(eastArn),+ ContainerLauncher.efsVolumeName(westArn),+ "the complete ARN must participate in volume identity");+ }+@TempDirPath tempDir;