From d1b30b79db9697f6c337828cc7d1b5dbe487ab78 Mon Sep 17 00:00:00 2001 From: Niels Kootstra <545768+nkootstra@users.noreply.github.com> Date: Thu, 3 Dec 2026 12:30:43 +0100 Subject: [PATCH] fix(lambda): delete a function's asynchronous invoke configs with the function (#10009) DeleteFunction left the function's event invoke configs in the store, so a function created later under the same name inherited the deleted one's retry, event age and destination settings. Deleting the whole function now removes its configs for every qualifier, leaving other functions alone, including ones whose names share a prefix. Put and Update of a config take the same per-function lock as the delete, so a write racing a delete gets ResourceNotFoundException instead of bringing the settings back. --- docs/services/lambda.md | 3 +- .../aws/services/lambda/LambdaService.java | 70 ++++++++++++------- .../lambda/LambdaEventInvokeConfigTest.java | 33 +++++++-- 3 files changed, 73 insertions(+), 33 deletions(-) diff --git a/docs/services/lambda.md b/docs/services/lambda.md index abe2d0c383..880786643c 100644 --- a/docs/services/lambda.md +++ b/docs/services/lambda.md @@ -109,7 +109,8 @@ provisions it from a stack. A SAM function's `EventInvokeConfig` expands into th `sam deploy` does: on `$LATEST`, or on the `AutoPublishAlias` alias, with the inline role policy for each destination and the SQS queue or SNS topic SAM creates when a destination gives only its `Type`. Asynchronous invocations apply its retry, event age, destination settings, and dead-letter queue -configurations (`DeadLetterConfig`). +configurations (`DeadLetterConfig`). `DeleteFunction` on the whole function removes its asynchronous +invocation settings for every qualifier, so a function recreated under the same name starts without them. ### Deployment package storage diff --git a/src/main/java/io/floci/aws/services/lambda/LambdaService.java b/src/main/java/io/floci/aws/services/lambda/LambdaService.java index 23cbc97b79..26fd403c00 100644 --- a/src/main/java/io/floci/aws/services/lambda/LambdaService.java +++ b/src/main/java/io/floci/aws/services/lambda/LambdaService.java @@ -1363,6 +1363,7 @@ public class LambdaService implements ResourceProvider, VpcDependencyProvider { versionCounters.remove(versionCounterKey(region, fn)); versionCounters.remove(legacyVersionCounterKey(region, functionName)); failedReactiveSyncs.remove(arn); + eventInvokeEntries(region, arn).forEach(entry -> eventInvokeConfigs.remove(entry.getKey())); if (aliasStore != null) { for (LambdaAlias alias : aliasStore.list(region, functionName)) { aliasStore.delete(region, functionName, alias.getName()); @@ -4670,29 +4671,37 @@ public class LambdaService implements ResourceProvider, VpcDependencyProvider { public FunctionEventInvokeConfig putEventInvokeConfig(String region, String functionName, String qualifier, Map request) { LambdaFunction fn = getFunction(region, functionName); - String key = eventInvokeKey(region, fn.getFunctionArn(), qualifier); - FunctionEventInvokeConfig cfg = new FunctionEventInvokeConfig(); - cfg.setFunctionArn(qualifiedArn(fn.getFunctionArn(), qualifier)); - cfg.setLastModified(System.currentTimeMillis()); - applyEventInvokeRequest(cfg, request, true); - eventInvokeConfigs.put(key, cfg); - return cfg; + // Under the lock DeleteFunction holds, and re-checked there: a write that read the function + // before a delete must not store settings the delete has already swept. + synchronized (lockForConcurrencyOp(fn.getFunctionArn())) { + fn = getFunction(region, functionName); + String key = eventInvokeKey(region, fn.getFunctionArn(), qualifier); + FunctionEventInvokeConfig cfg = new FunctionEventInvokeConfig(); + cfg.setFunctionArn(qualifiedArn(fn.getFunctionArn(), qualifier)); + cfg.setLastModified(System.currentTimeMillis()); + applyEventInvokeRequest(cfg, request, true); + eventInvokeConfigs.put(key, cfg); + return cfg; + } } public FunctionEventInvokeConfig updateEventInvokeConfig(String region, String functionName, String qualifier, Map request) { LambdaFunction fn = getFunction(region, functionName); - String key = eventInvokeKey(region, fn.getFunctionArn(), qualifier); - FunctionEventInvokeConfig existing = eventInvokeConfigs.get(key); - if (existing == null) { - throw new AwsException("ResourceNotFoundException", - "The function " + fn.getFunctionArn() + " doesn't have an EventInvokeConfig", 404); + synchronized (lockForConcurrencyOp(fn.getFunctionArn())) { + fn = getFunction(region, functionName); + String key = eventInvokeKey(region, fn.getFunctionArn(), qualifier); + FunctionEventInvokeConfig existing = eventInvokeConfigs.get(key); + if (existing == null) { + throw new AwsException("ResourceNotFoundException", + "The function " + fn.getFunctionArn() + " doesn't have an EventInvokeConfig", 404); + } + applyEventInvokeRequest(existing, request, false); + existing.setLastModified(System.currentTimeMillis()); + // Re-put so StorageBackedMap routes the mutation through the backend + eventInvokeConfigs.put(key, existing); + return existing; } - applyEventInvokeRequest(existing, request, false); - existing.setLastModified(System.currentTimeMillis()); - // Re-put so StorageBackedMap routes the mutation through the backend - eventInvokeConfigs.put(key, existing); - return existing; } public FunctionEventInvokeConfig getEventInvokeConfig(String region, String functionName, String qualifier) { @@ -4717,14 +4726,7 @@ public class LambdaService implements ResourceProvider, VpcDependencyProvider { public List listEventInvokeConfigs(String region, String functionName) { LambdaFunction fn = getFunction(region, functionName); - String prefix = region + ":" + fn.getFunctionArn() + ":"; - List result = new ArrayList<>(); - for (Map.Entry entry : eventInvokeConfigs.entrySet()) { - if (entry.getKey().startsWith(prefix)) { - result.add(entry.getValue()); - } - } - return result; + return eventInvokeEntries(region, fn.getFunctionArn()).stream().map(Map.Entry::getValue).toList(); } public Optional findEventInvokeConfig(LambdaFunction fn) { @@ -4762,7 +4764,23 @@ public class LambdaService implements ResourceProvider, VpcDependencyProvider { } private String eventInvokeKey(String region, String functionArn, String qualifier) { - return region + ":" + functionArn + ":" + (qualifier != null ? qualifier : "$LATEST"); + return eventInvokePrefix(region, functionArn) + (qualifier != null ? qualifier : "$LATEST"); + } + + private String eventInvokePrefix(String region, String functionArn) { + return region + ":" + functionArn + ":"; + } + + /** Every stored event invoke config of one function, whatever its qualifier, as a snapshot safe to remove from. */ + private List> eventInvokeEntries(String region, String functionArn) { + String prefix = eventInvokePrefix(region, functionArn); + List> entries = new ArrayList<>(); + for (Map.Entry entry : eventInvokeConfigs.entrySet()) { + if (entry.getKey().startsWith(prefix)) { + entries.add(Map.entry(entry.getKey(), entry.getValue())); + } + } + return entries; } private String qualifiedArn(String functionArn, String qualifier) { diff --git a/src/test/java/io/floci/aws/services/lambda/LambdaEventInvokeConfigTest.java b/src/test/java/io/floci/aws/services/lambda/LambdaEventInvokeConfigTest.java index 1b702c8918..3b7d6aabd9 100644 --- a/src/test/java/io/floci/aws/services/lambda/LambdaEventInvokeConfigTest.java +++ b/src/test/java/io/floci/aws/services/lambda/LambdaEventInvokeConfigTest.java @@ -31,12 +31,7 @@ class LambdaEventInvokeConfigTest { 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") - )); + createImageFunction("test-fn"); } @Test @@ -167,4 +162,30 @@ class LambdaEventInvokeConfigTest { assertEquals(versionDestination, service.findEventInvokeConfig(executedVersion) .orElseThrow().getDestinationConfig().getOnSuccess().getDestination()); } + + @Test + void deletingTheFunctionRemovesItsEventInvokeConfigsForEveryQualifier() { + service.putEventInvokeConfig(REGION, "test-fn", null, Map.of("MaximumRetryAttempts", 0)); + service.putEventInvokeConfig(REGION, "test-fn", "prod", Map.of("MaximumRetryAttempts", 1)); + createImageFunction("test-fn-sibling"); + service.putEventInvokeConfig(REGION, "test-fn-sibling", null, Map.of("MaximumRetryAttempts", 2)); + + service.deleteFunction(REGION, "test-fn"); + createImageFunction("test-fn"); + + assertTrue(service.listEventInvokeConfigs(REGION, "test-fn").isEmpty()); + AwsException ex = assertThrows(AwsException.class, + () -> service.getEventInvokeConfig(REGION, "test-fn", null)); + assertEquals("ResourceNotFoundException", ex.getErrorCode()); + assertEquals(2, service.getEventInvokeConfig(REGION, "test-fn-sibling", null).getMaximumRetryAttempts()); + } + + private void createImageFunction(String name) { + service.createFunction(REGION, Map.of( + "FunctionName", name, + "PackageType", "Image", + "Role", "arn:aws:iam::000000000000:role/test-role", + "Code", Map.of("ImageUri", "public.ecr.aws/lambda/nodejs:20") + )); + } }