feat(lambda): implement reactive S3-to-Lambda sync for hot-reloading (#509)

* feat(lambda): implement reactive S3-to-Lambda sync for hot-reloading
* fix: clean unused hotreload properties
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-04-17 19:04:47 -05:00
1 parent 9b5f364e47
commit 114fade129
9 files changed
+318 -7

No files matched your search

@@ -15,9 +15,11 @@ import io.github.hectorvent.floci.services.lambda.zip.CodeStore;
import io.github.hectorvent.floci.services.lambda.zip.ZipExtractor;
import io.github.hectorvent.floci.services.s3.S3Service;
import io.github.hectorvent.floci.services.s3.model.S3Object;
import io.github.hectorvent.floci.services.s3.model.S3ObjectUpdatedEvent;
import io.github.hectorvent.floci.services.sqs.SqsService;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
@@ -240,6 +242,8 @@ public class LambdaService {
}
String zipFileBase64 = (String) code.get("ZipFile");
if (zipFileBase64 != null) {
fn.setS3Bucket(null);
fn.setS3Key(null);
extractZipCode(fn, zipFileBase64);
}
String s3Bucket = (String) code.get("S3Bucket");
@@ -304,6 +308,8 @@ public class LambdaService {
String s3Key = (String) request.get("S3Key");
if (zipFileBase64 != null) {
fn.setS3Bucket(null);
fn.setS3Key(null);
extractZipCode(fn, zipFileBase64);
}
if (imageUri != null) {
@@ -951,6 +957,9 @@ public class LambdaService {
if (s3Service == null) {
throw new AwsException("ServiceUnavailableException", "S3 service not available", 503);
}
fn.setS3Bucket(s3Bucket);
fn.setS3Key(s3Key);
S3Object obj;
try {
obj = s3Service.getObject(s3Bucket, s3Key);
@@ -1087,4 +1096,34 @@ public class LambdaService {
return defaultValue;
}
}
/**
* Observes S3 object updates and triggers reactive sync for any Lambda
* functions linked to the updated object.
*/
public void onS3ObjectUpdated(@Observes S3ObjectUpdatedEvent event) {
LOG.debugv("Observing S3 update: {0}/{1}", event.bucketName(), event.key());
// For simplicity, we scan all functions in the default region
// Most local dev setups use a single region
String region = regionResolver.getDefaultRegion();
List<LambdaFunction> functions = functionStore.list(region);
for (LambdaFunction fn : functions) {
if (event.bucketName().equals(fn.getS3Bucket()) && event.key().equals(fn.getS3Key())) {
LOG.infov("Reactive S3 Sync: updating function {0} from s3://{1}/{2}",
fn.getFunctionName(), event.bucketName(), event.key());
try {
S3Object obj = s3Service.getObject(event.bucketName(), event.key());
extractZipCode(fn, Base64.getEncoder().encodeToString(obj.getData()));
fn.setLastModified(Instant.now().toEpochMilli());
fn.setRevisionId(UUID.randomUUID().toString());
functionStore.save(region, fn);
// Push to warm workers
warmPool.pushCodeUpdate(fn);
} catch (Exception e) {
LOG.warnv("Failed reactive sync for function {0}: {1}", fn.getFunctionName(), e.getMessage());
}
}
}
}
}
@@ -150,6 +150,16 @@ public class WarmPool {
}
}
/**
* Pushes a code update to all warm containers in the pool for the given function.
* In this implementation, we drain the containers to force a fresh start with new code.
*/
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());
}
/**
* Stops and removes all warm containers for the given function.
* Called on function delete or code update.
@@ -150,7 +150,6 @@ public class ContainerLauncher {
String shortId = java.util.UUID.randomUUID().toString().replace("-", "").substring(0, 8);
String containerName = "floci-" + fn.getFunctionName() + "-" + shortId;
// Build container spec
ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(image)
.withName(containerName)
.withEnv(env)
@@ -28,6 +28,8 @@ public class LambdaFunction {
private String packageType = "Zip";
private String imageUri;
private String codeLocalPath;
private String s3Bucket;
private String s3Key;
private Map<String, String> environment = new HashMap<>();
private Map<String, String> tags = new HashMap<>();
private List<Map<String, Object>> policies = new ArrayList<>();
@@ -88,6 +90,12 @@ public class LambdaFunction {
public String getCodeLocalPath() { return codeLocalPath; }
public void setCodeLocalPath(String codeLocalPath) { this.codeLocalPath = codeLocalPath; }
public String getS3Bucket() { return s3Bucket; }
public void setS3Bucket(String s3Bucket) { this.s3Bucket = s3Bucket; }
public String getS3Key() { return s3Key; }
public void setS3Key(String s3Key) { this.s3Key = s3Key; }
public Map<String, String> getEnvironment() { return environment; }
public void setEnvironment(Map<String, String> environment) { this.environment = environment; }
@@ -18,6 +18,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Event;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
@@ -63,6 +64,7 @@ public class S3Service {
private final Instance<LambdaService> lambdaServiceProvider;
private final LambdaInvoker lambdaInvoker;
private final EventBridgeService eventBridgeService;
private final Event<S3ObjectUpdatedEvent> s3UpdatedEvent;
private final RegionResolver regionResolver;
private final String baseUrl;
private final ObjectMapper objectMapper;
@@ -72,6 +74,7 @@ public class S3Service {
SqsService sqsService, SnsService snsService,
Instance<LambdaService> lambdaServiceProvider,
EventBridgeService eventBridgeService,
Event<S3ObjectUpdatedEvent> s3UpdatedEvent,
RegionResolver regionResolver,
ObjectMapper objectMapper) {
this(
@@ -84,7 +87,7 @@ public class S3Service {
Path.of(config.storage().persistentPath()).resolve("s3"),
"memory".equals(config.storage().services().s3().mode().orElse(config.storage().mode())),
sqsService, snsService, null, lambdaServiceProvider, null,
eventBridgeService,
eventBridgeService, s3UpdatedEvent,
regionResolver,
config.effectiveBaseUrl(), objectMapper
);
@@ -97,7 +100,7 @@ public class S3Service {
StorageBackend<String, S3Object> objectStore,
Path dataRoot, boolean inMemory) {
this(bucketStore, objectStore, dataRoot, inMemory, null, null, null, null, null, null, null,
"http://localhost:4566", new ObjectMapper());
null, "http://localhost:4566", new ObjectMapper());
}
S3Service(StorageBackend<String, Bucket> bucketStore,
@@ -105,8 +108,8 @@ public class S3Service {
Path dataRoot, boolean inMemory,
LambdaService lambdaService,
RegionResolver regionResolver) {
this(bucketStore, objectStore, dataRoot, inMemory, null, null, lambdaService, null, null, null, regionResolver,
"http://localhost:4566", new ObjectMapper());
this(bucketStore, objectStore, dataRoot, inMemory, null, null, lambdaService, null, null, null, null,
regionResolver, "http://localhost:4566", new ObjectMapper());
}
S3Service(StorageBackend<String, Bucket> bucketStore,
@@ -114,8 +117,8 @@ public class S3Service {
Path dataRoot, boolean inMemory,
LambdaInvoker lambdaInvoker,
RegionResolver regionResolver) {
this(bucketStore, objectStore, dataRoot, inMemory, null, null, null, null, lambdaInvoker, null, regionResolver,
"http://localhost:4566", new ObjectMapper());
this(bucketStore, objectStore, dataRoot, inMemory, null, null, null, null, lambdaInvoker, null, null,
regionResolver, "http://localhost:4566", new ObjectMapper());
}
private S3Service(StorageBackend<String, Bucket> bucketStore,
@@ -125,6 +128,7 @@ public class S3Service {
Instance<LambdaService> lambdaServiceProvider,
LambdaInvoker lambdaInvoker,
EventBridgeService eventBridgeService,
Event<S3ObjectUpdatedEvent> s3UpdatedEvent,
RegionResolver regionResolver, String baseUrl, ObjectMapper objectMapper) {
this.bucketStore = bucketStore;
this.objectStore = objectStore;
@@ -136,6 +140,7 @@ public class S3Service {
this.lambdaServiceProvider = lambdaServiceProvider;
this.lambdaInvoker = lambdaInvoker;
this.eventBridgeService = eventBridgeService;
this.s3UpdatedEvent = s3UpdatedEvent;
this.regionResolver = regionResolver;
this.baseUrl = baseUrl;
this.objectMapper = objectMapper;
@@ -1462,6 +1467,9 @@ public class S3Service {
}
private void fireNotifications(String bucketName, String key, String eventName, S3Object obj) {
if (s3UpdatedEvent != null && eventName.startsWith("ObjectCreated")) {
s3UpdatedEvent.fire(new S3ObjectUpdatedEvent(bucketName, key));
}
if (sqsService == null && snsService == null && lambdaService == null
&& lambdaServiceProvider == null && lambdaInvoker == null && eventBridgeService == null) {
return;
@@ -0,0 +1,11 @@
package io.github.hectorvent.floci.services.s3.model;
/**
* Internal event fired when an S3 object is created or updated.
* Observed by other services (like Lambda) to trigger reactive behaviors.
*
* @param bucketName the name of the bucket
* @param key the object key
*/
public record S3ObjectUpdatedEvent(String bucketName, String key) {
}