fix: Implement S3 Lambda notifications (#278)

* feat: Implement S3 Lambda notifications

Add support for S3 event notifications to AWS Lambda functions.
- Introduce `LambdaNotification` model to represent Lambda configurations.
- Update S3 controller to parse and serialize `CloudFunctionConfiguration` in notification XML.
- Implement logic in `S3Service` to extract Lambda ARN details and invoke functions upon matching S3 events.
- Add unit and integration tests for Lambda notification configuration persistence and event triggering.

* test(s3-notifications): Add S3 bucket notification compatibility tests

Introduces `S3NotificationsTest` to verify S3 event notifications for SQS, SNS, and Lambda targets.
Includes tests for notification configuration round-trip and Lambda invocation with object filters.
Adds `s3NotificationLoggerZip` to `LambdaUtils` for creating a test Lambda function.

Co-authored-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Fred PeñaandHector Ventura authored and GitHub committed 2026-04-08 23:11:20 -05:00
1 parent ae49bfb378
commit 04cbeb542f
9 files changed
+585 -10

No files matched your search

@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.s3.model.Bucket;
import io.github.hectorvent.floci.services.s3.model.GetObjectAttributesParts;
import io.github.hectorvent.floci.services.s3.model.GetObjectAttributesResult;
import io.github.hectorvent.floci.services.s3.model.LambdaNotification;
import io.github.hectorvent.floci.services.s3.model.MultipartUpload;
import io.github.hectorvent.floci.services.s3.model.FilterRule;
import io.github.hectorvent.floci.services.s3.model.NotificationConfiguration;
@@ -974,6 +975,16 @@ public class S3Controller {
appendFilterRules(xml, tn.filterRules());
xml.end("TopicConfiguration");
}
for (LambdaNotification ln : config.getLambdaFunctionConfigurations()) {
xml.start("CloudFunctionConfiguration")
.elem("Id", ln.id())
.elem("CloudFunction", ln.functionArn());
for (String event : ln.events()) {
xml.elem("Event", event);
}
appendFilterRules(xml, ln.filterRules());
xml.end("CloudFunctionConfiguration");
}
xml.end("NotificationConfiguration");
return Response.ok(xml.build()).type(MediaType.APPLICATION_XML).build();
} catch (AwsException e) {
@@ -994,6 +1005,14 @@ public class S3Controller {
config.getTopicConfigurations().add(
new TopicNotification(parsed.id, parsed.arn, parsed.events, parsed.filterRules));
}
for (var parsed : parseNotificationGroups(xml, "LambdaFunctionConfiguration", "LambdaFunctionArn")) {
config.getLambdaFunctionConfigurations().add(
new LambdaNotification(parsed.id, parsed.arn, parsed.events, parsed.filterRules));
}
for (var parsed : parseNotificationGroups(xml, "CloudFunctionConfiguration", "CloudFunction")) {
config.getLambdaFunctionConfigurations().add(
new LambdaNotification(parsed.id, parsed.arn, parsed.events, parsed.filterRules));
}
config.setEventBridgeEnabled(xml.contains("<EventBridgeConfiguration"));
@@ -8,6 +8,8 @@ import io.github.hectorvent.floci.core.common.XmlBuilder;
import io.github.hectorvent.floci.core.common.XmlParser;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.lambda.LambdaService;
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
import io.github.hectorvent.floci.services.s3.model.*;
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
import io.github.hectorvent.floci.services.sns.SnsService;
@@ -16,12 +18,14 @@ 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.inject.Instance;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
@@ -34,6 +38,11 @@ import java.util.concurrent.ConcurrentHashMap;
@ApplicationScoped
public class S3Service {
@FunctionalInterface
interface LambdaInvoker {
void invoke(String region, String functionName, byte[] payload, InvocationType type);
}
private static final Logger LOG = Logger.getLogger(S3Service.class);
private final StorageBackend<String, Bucket> bucketStore;
@@ -46,6 +55,9 @@ public class S3Service {
private final SqsService sqsService;
private final SnsService snsService;
private final LambdaService lambdaService;
private final Instance<LambdaService> lambdaServiceProvider;
private final LambdaInvoker lambdaInvoker;
private final EventBridgeService eventBridgeService;
private final RegionResolver regionResolver;
private final String baseUrl;
@@ -54,6 +66,7 @@ public class S3Service {
@Inject
public S3Service(StorageFactory storageFactory, EmulatorConfig config,
SqsService sqsService, SnsService snsService,
Instance<LambdaService> lambdaServiceProvider,
EventBridgeService eventBridgeService,
RegionResolver regionResolver,
ObjectMapper objectMapper) {
@@ -66,7 +79,10 @@ public class S3Service {
}),
Path.of(config.storage().persistentPath()).resolve("s3"),
"memory".equals(config.storage().services().s3().mode().orElse(config.storage().mode())),
sqsService, snsService, eventBridgeService, regionResolver, config.effectiveBaseUrl(), objectMapper
sqsService, snsService, null, lambdaServiceProvider, null,
eventBridgeService,
regionResolver,
config.effectiveBaseUrl(), objectMapper
);
}
@@ -76,13 +92,34 @@ public class S3Service {
S3Service(StorageBackend<String, Bucket> bucketStore,
StorageBackend<String, S3Object> objectStore,
Path dataRoot, boolean inMemory) {
this(bucketStore, objectStore, dataRoot, inMemory, null, null, null, null, "http://localhost:4566",
new ObjectMapper());
this(bucketStore, objectStore, dataRoot, inMemory, null, null, null, null, null, null, null,
"http://localhost:4566", new ObjectMapper());
}
S3Service(StorageBackend<String, Bucket> bucketStore,
StorageBackend<String, S3Object> objectStore,
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());
}
S3Service(StorageBackend<String, Bucket> bucketStore,
StorageBackend<String, S3Object> objectStore,
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());
}
private S3Service(StorageBackend<String, Bucket> bucketStore,
StorageBackend<String, S3Object> objectStore,
Path dataRoot, boolean inMemory, SqsService sqsService, SnsService snsService,
LambdaService lambdaService,
Instance<LambdaService> lambdaServiceProvider,
LambdaInvoker lambdaInvoker,
EventBridgeService eventBridgeService,
RegionResolver regionResolver, String baseUrl, ObjectMapper objectMapper) {
this.bucketStore = bucketStore;
@@ -91,6 +128,9 @@ public class S3Service {
this.inMemory = inMemory;
this.sqsService = sqsService;
this.snsService = snsService;
this.lambdaService = lambdaService;
this.lambdaServiceProvider = lambdaServiceProvider;
this.lambdaInvoker = lambdaInvoker;
this.eventBridgeService = eventBridgeService;
this.regionResolver = regionResolver;
this.baseUrl = baseUrl;
@@ -1298,7 +1338,8 @@ public class S3Service {
}
private void fireNotifications(String bucketName, String key, String eventName, S3Object obj) {
if (sqsService == null && snsService == null && eventBridgeService == null) {
if (sqsService == null && snsService == null && lambdaService == null
&& lambdaServiceProvider == null && lambdaInvoker == null && eventBridgeService == null) {
return;
}
Bucket bucket = bucketStore.get(bucketName).orElse(null);
@@ -1335,6 +1376,25 @@ public class S3Service {
}
}
if (lambdaInvoker != null || resolveLambdaService() != null) {
for (LambdaNotification ln : config.getLambdaFunctionConfigurations()) {
if (ln.events().stream().anyMatch(p -> matchesEvent(p, eventName)) && ln.matchesKey(key)) {
try {
String lambdaRegion = extractRegionFromArn(ln.functionArn());
String functionName = extractLambdaFunctionName(ln.functionArn());
if (lambdaRegion == null || functionName == null) {
throw new AwsException("InvalidParameterValueException",
"Invalid Lambda function ARN: " + ln.functionArn(), 400);
}
invokeLambda(lambdaRegion, functionName, eventJson.getBytes(StandardCharsets.UTF_8));
LOG.debugv("Fired S3 event {0} to Lambda {1}", eventName, ln.functionArn());
} catch (Exception e) {
LOG.warnv("Failed to deliver S3 event to Lambda {0}: {1}", ln.functionArn(), e.getMessage());
}
}
}
}
if (config.isEventBridgeEnabled() && eventBridgeService != null) {
try {
String detailType = eventName.startsWith("ObjectCreated") ? "Object Created" : "Object Deleted";
@@ -1386,6 +1446,48 @@ public class S3Service {
return AwsArnUtils.arnToQueueUrl(arn, baseUrl);
}
private static String extractRegionFromArn(String arn) {
if (arn == null || !arn.startsWith("arn:aws:")) {
return null;
}
String[] parts = arn.split(":");
return parts.length >= 4 ? parts[3] : null;
}
private static String extractLambdaFunctionName(String functionArn) {
if (functionArn == null) {
return null;
}
int functionMarker = functionArn.indexOf(":function:");
if (functionMarker < 0) {
return null;
}
String suffix = functionArn.substring(functionMarker + ":function:".length());
int qualifierSeparator = suffix.indexOf(':');
return qualifierSeparator >= 0 ? suffix.substring(0, qualifierSeparator) : suffix;
}
private LambdaService resolveLambdaService() {
if (lambdaService != null) {
return lambdaService;
}
if (lambdaServiceProvider != null && lambdaServiceProvider.isResolvable()) {
return lambdaServiceProvider.get();
}
return null;
}
private void invokeLambda(String region, String functionName, byte[] payload) {
if (lambdaInvoker != null) {
lambdaInvoker.invoke(region, functionName, payload, InvocationType.Event);
return;
}
LambdaService service = resolveLambdaService();
if (service != null) {
service.invoke(region, functionName, payload, InvocationType.Event);
}
}
private String buildS3EventJson(String bucketName, String key, String eventName,
S3Object obj, String region, boolean isVersionEnabled) {
try {
@@ -0,0 +1,17 @@
package io.github.hectorvent.floci.services.s3.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.List;
@RegisterForReflection
public record LambdaNotification(String id, String functionArn, List<String> events, List<FilterRule> filterRules) {
public LambdaNotification(String id, String functionArn, List<String> events) {
this(id, functionArn, events, List.of());
}
public boolean matchesKey(String key) {
return filterRules == null || filterRules.isEmpty() || filterRules.stream().allMatch(r -> r.matches(key));
}
}
@@ -12,24 +12,49 @@ public class NotificationConfiguration {
private List<QueueNotification> queueConfigurations = new ArrayList<>();
private List<TopicNotification> topicConfigurations = new ArrayList<>();
private List<LambdaNotification> lambdaFunctionConfigurations = new ArrayList<>();
private boolean eventBridgeEnabled;
public NotificationConfiguration() {}
public NotificationConfiguration() {
}
public List<QueueNotification> getQueueConfigurations() {
return queueConfigurations;
}
public List<QueueNotification> getQueueConfigurations() { return queueConfigurations; }
public void setQueueConfigurations(List<QueueNotification> queueConfigurations) {
this.queueConfigurations = queueConfigurations != null ? queueConfigurations : new ArrayList<>();
}
public List<TopicNotification> getTopicConfigurations() { return topicConfigurations; }
public List<TopicNotification> getTopicConfigurations() {
return topicConfigurations;
}
public void setTopicConfigurations(List<TopicNotification> topicConfigurations) {
this.topicConfigurations = topicConfigurations != null ? topicConfigurations : new ArrayList<>();
}
public boolean isEventBridgeEnabled() { return eventBridgeEnabled; }
public void setEventBridgeEnabled(boolean eventBridgeEnabled) { this.eventBridgeEnabled = eventBridgeEnabled; }
public List<LambdaNotification> getLambdaFunctionConfigurations() {
return lambdaFunctionConfigurations;
}
public void setLambdaFunctionConfigurations(List<LambdaNotification> lambdaFunctionConfigurations) {
this.lambdaFunctionConfigurations = lambdaFunctionConfigurations != null
? lambdaFunctionConfigurations : new ArrayList<>();
}
public boolean isEventBridgeEnabled() {
return eventBridgeEnabled;
}
public void setEventBridgeEnabled(boolean eventBridgeEnabled) {
this.eventBridgeEnabled = eventBridgeEnabled;
}
public boolean isEmpty() {
return queueConfigurations.isEmpty() && topicConfigurations.isEmpty() && !eventBridgeEnabled;
return queueConfigurations.isEmpty()
&& topicConfigurations.isEmpty()
&& lambdaFunctionConfigurations.isEmpty()
&& !eventBridgeEnabled;
}
}