Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(lambda): honor alias async destination configuration (#4348)

Aniket Shukla09276c75dcdc
diff --git a/docs/services/lambda.md b/docs/services/lambda.md
index 83dc78da79..f36baa3cc8 100644
--- a/docs/services/lambda.md
+++ b/docs/services/lambda.md
@@ -95,13 +95,11 @@ type `Lambda Function Invocation Result - Failure`, a `condition` of `RetriesExh
`functionError` member is present only on a failure record. Since retries are not applied, a
failure reaches its destination once and `approximateInvokeCount` is always 1.
-Three limits are worth knowing:
+An alias-specific destination configuration is used when the alias is invoked. If the alias has
+none, Floci checks the configuration on the version that the alias resolves to.
+
+Two limits are worth knowing:
-- **A destination configured on an alias does not fire.** The configuration is matched against the
- version the invocation actually ran, so one stored for `$LATEST` or for an explicit version is
- found, while one stored for an alias (`Qualifier: prod`) is not, and nothing is delivered. The
- CDK `onSuccess` and `onFailure` properties store theirs under `$LATEST`, so a destination
- declared that way is unaffected.
- **An S3 `OnFailure` destination is not delivered.** AWS added an S3 bucket as a fifth
destination kind, on failure only. Floci routes on the service in the destination ARN and has
no `s3` arm, so such a record is dropped with a warning rather than written to the bucket. The
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/AsyncInvokeDestinationRouter.java b/src/main/java/io/github/hectorvent/floci/services/lambda/AsyncInvokeDestinationRouter.java
index 64b9048b16..a49efc9e0f 100644
--- a/src/main/java/io/github/hectorvent/floci/services/lambda/AsyncInvokeDestinationRouter.java
+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/AsyncInvokeDestinationRouter.java
@@ -108,12 +108,17 @@ public class AsyncInvokeDestinationRouter {
* {@link LambdaInvocationChain} instead of running forever
*/
public void route(LambdaFunction fn, byte[] requestPayload, InvokeResult result, int chainDepth) {
+ route(fn, requestPayload, result, chainDepth, null);
+ }
+
+ public void route(LambdaFunction fn, byte[] requestPayload, InvokeResult result, int chainDepth,
+ String invokedQualifier) {
// A runtime that never started, timed out, or crashed is reported as a function error by
// the executor, so this one test covers a handler error and a failed runtime alike.
boolean failed = result.getFunctionError() != null;
FunctionEventInvokeConfig.Destination destination;
try {
- destination = destinationFor(fn, failed);
+ destination = destinationFor(fn, failed, invokedQualifier);
} catch (Exception e) {
LOG.warnv("Could not read the event invoke configuration of {0}: {1}",
fn.getFunctionArn(), e.getMessage());
@@ -133,8 +138,11 @@ public class AsyncInvokeDestinationRouter {
}
}
- private FunctionEventInvokeConfig.Destination destinationFor(LambdaFunction fn, boolean failed) {
- FunctionEventInvokeConfig config = lambdaService.get().findEventInvokeConfig(fn).orElse(null);
+ private FunctionEventInvokeConfig.Destination destinationFor(LambdaFunction fn, boolean failed,
+ String invokedQualifier) {
+ FunctionEventInvokeConfig config = (invokedQualifier == null
+ ? lambdaService.get().findEventInvokeConfig(fn)
+ : lambdaService.get().findEventInvokeConfig(fn, invokedQualifier)).orElse(null);
if (config == null || config.getDestinationConfig() == null) {
return null;
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorService.java
index 7d1716cf73..64d79bc2c8 100644
--- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorService.java
@@ -80,6 +80,11 @@ public class LambdaExecutorService {
* one a destination chain can re-enter through.
*/
InvokeResult invoke(LambdaFunction fn, byte[] payload, InvocationType type, int chainDepth) {
+ return invoke(fn, payload, type, chainDepth, null);
+ }
+
+ InvokeResult invoke(LambdaFunction fn, byte[] payload, InvocationType type, int chainDepth,
+ String invokedQualifier) {
String requestId = UUID.randomUUID().toString();
if (type == InvocationType.DryRun) {
@@ -105,7 +110,7 @@ public class LambdaExecutorService {
permit.close();
}
if (destinationRouter != null) {
- destinationRouter.route(fn, payload, asyncResult, chainDepth);
+ destinationRouter.route(fn, payload, asyncResult, chainDepth, invokedQualifier);
}
});
} catch (RuntimeException e) {
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.java
index 72bd8d5752..7085e3a41d 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
@@ -1047,7 +1047,7 @@ public class LambdaService implements ResourceProvider {
}
reportCustomResourceLiveness(payload);
InvokeResult result = executorService.invoke(fn, payload, type,
- LambdaInvocationChain.currentDepth());
+ LambdaInvocationChain.currentDepth(), qualifier);
result.setExecutedVersion(fn.getVersion());
return result;
}
@@ -1076,7 +1076,7 @@ public class LambdaService implements ResourceProvider {
LambdaArnUtils.ResolvedFunctionRef ref = LambdaArnUtils.resolve(functionArn);
LambdaFunction fn = targetResolver.resolveInvokeTargetForAccount(
arn.accountId(), arn.region(), ref.name(), ref.qualifier());
- InvokeResult result = executorService.invoke(fn, payload, type, chainDepth);
+ InvokeResult result = executorService.invoke(fn, payload, type, chainDepth, ref.qualifier());
result.setExecutedVersion(fn.getVersion());
return result;
}
@@ -3220,36 +3220,38 @@ public class LambdaService implements ResourceProvider {
return result;
}
+ public Optional<FunctionEventInvokeConfig> findEventInvokeConfig(LambdaFunction fn) {
+ return findEventInvokeConfig(fn, null);
+ }
+
/**
- * The event invoke configuration that applies to an invocation of {@code fn}, or empty when
- * the function has none. Unlike {@link #getEventInvokeConfig} this answers a background
- * worker rather than an API caller, so an absent configuration is a result and not a fault.
- *
- * <p>The lookup key is the unqualified function ARN plus the version the invocation actually
- * ran, which is where a function-level {@code PutFunctionEventInvokeConfig} stores its
- * settings: that call names {@code $LATEST}, and so does a resolved unpublished function.
- *
- * <p>The read runs as the function's owning account. {@code PutFunctionEventInvokeConfig}
- * stored the configuration in that account's partition of the account-aware backend, and the
- * background worker calling this carries no request context, so without re-establishing the
- * account the read would land in the default partition and a function in any other account
- * would look as though it had no configuration at all.
+ * Finds the asynchronous settings for the qualifier the caller invoked, falling back to the
+ * resolved version when an alias has no configuration of its own. The read runs in the
+ * function owner's account because destination delivery happens on a background thread.
*/
- public Optional<FunctionEventInvokeConfig> findEventInvokeConfig(LambdaFunction fn) {
+ public Optional<FunctionEventInvokeConfig> findEventInvokeConfig(LambdaFunction fn,
+ String invokedQualifier) {
if (fn == null || fn.getFunctionArn() == null) {
return Optional.empty();
}
- String qualifier = fn.getVersion() != null ? fn.getVersion() : "$LATEST";
+ String version = fn.getVersion() != null ? fn.getVersion() : "$LATEST";
String functionArn = fn.getFunctionArn();
- if (functionArn.endsWith(":" + qualifier)) {
- functionArn = functionArn.substring(0, functionArn.length() - qualifier.length() - 1);
+ if (functionArn.endsWith(":" + version)) {
+ functionArn = functionArn.substring(0, functionArn.length() - version.length() - 1);
}
String region = AwsArnUtils.regionOrDefault(functionArn, null);
- String key = eventInvokeKey(region, functionArn, qualifier);
+ String baseArn = functionArn;
String owner = fn.getAccountId() != null
? fn.getAccountId()
: AwsArnUtils.accountOrDefault(functionArn, null);
- return RequestScopes.callAs(owner, () -> Optional.ofNullable(eventInvokeConfigs.get(key)));
+ return RequestScopes.callAs(owner, () -> {
+ FunctionEventInvokeConfig config = invokedQualifier != null && !invokedQualifier.isBlank()
+ ? eventInvokeConfigs.get(eventInvokeKey(region, baseArn, invokedQualifier)) : null;
+ if (config == null) {
+ config = eventInvokeConfigs.get(eventInvokeKey(region, baseArn, version));
+ }
+ return Optional.ofNullable(config);
+ });
}
private String eventInvokeKey(String region, String functionArn, String qualifier) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/AsyncInvokeDestinationRouterTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/AsyncInvokeDestinationRouterTest.java
index 2e63e8c085..288aff03e5 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambda/AsyncInvokeDestinationRouterTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/AsyncInvokeDestinationRouterTest.java
@@ -1,13 +1,16 @@
package io.github.hectorvent.floci.services.lambda;
import com.fasterxml.jackson.databind.JsonNode;
-import java.time.Instant;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.config.EmulatorConfig;
+import io.github.hectorvent.floci.core.common.RegionResolver;
+import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
import io.github.hectorvent.floci.services.lambda.model.FunctionEventInvokeConfig;
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
+import io.github.hectorvent.floci.services.lambda.zip.CodeStore;
+import io.github.hectorvent.floci.services.lambda.zip.ZipExtractor;
import io.github.hectorvent.floci.services.sns.SnsService;
import io.github.hectorvent.floci.services.sqs.SqsService;
import jakarta.enterprise.inject.Instance;
@@ -20,6 +23,8 @@ import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
+import java.nio.file.Path;
+import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -339,6 +344,45 @@ class AsyncInvokeDestinationRouterTest {
assertEquals("3", detail.path("responseContext").path("executedVersion").asText());
}
+ @Test
+ void realAliasConfigDeliversToAliasBusAndFallsBackToVersionBus() {
+ String versionBusArn = "arn:aws:events:us-east-1:000000000000:event-bus/version-bus";
+ LambdaService realService = new LambdaService(
+ new LambdaFunctionStore(new InMemoryStorage<>()),
+ new WarmPool(),
+ new CodeStore(Path.of("target/test-data/lambda-code")),
+ new ZipExtractor(),
+ new RegionResolver("us-east-1", "000000000000"));
+ realService.createFunction("us-east-1", Map.of(
+ "FunctionName", "bank-pawnshop",
+ "PackageType", "Image",
+ "Role", "arn:aws:iam::000000000000:role/test-role",
+ "Code", Map.of("ImageUri", "public.ecr.aws/lambda/nodejs:20")));
+ LambdaFunction version = realService.publishVersion("us-east-1", "bank-pawnshop", null);
+ realService.putEventInvokeConfig("us-east-1", "bank-pawnshop", version.getVersion(), Map.of(
+ "DestinationConfig", Map.of("OnSuccess", Map.of("Destination", versionBusArn))));
+ realService.putEventInvokeConfig("us-east-1", "bank-pawnshop", "prod", Map.of(
+ "DestinationConfig", Map.of("OnSuccess", Map.of("Destination", BUS_ARN))));
+ AsyncInvokeDestinationRouter realRouter = new AsyncInvokeDestinationRouter(
+ instanceOf(realService), instanceOf(eventBridgeService), instanceOf(sqsService),
+ instanceOf(snsService), MAPPER, config);
+
+ realRouter.route(version, request(), success("{}"), 0, "prod");
+ realRouter.route(version, request(), success("{}"), 0, "other");
+
+ @SuppressWarnings("unchecked")
+ ArgumentCaptor<List<Map<String, Object>>> entries = ArgumentCaptor.forClass(List.class);
+ verify(eventBridgeService, times(2)).putEvents(entries.capture(), eq("us-east-1"), eq(null));
+ JsonNode aliasEntry = MAPPER.valueToTree(entries.getAllValues().get(0).get(0));
+ JsonNode versionEntry = MAPPER.valueToTree(entries.getAllValues().get(1).get(0));
+ assertEquals(BUS_ARN, aliasEntry.path("EventBusName").asText());
+ assertEquals(versionBusArn, versionEntry.path("EventBusName").asText());
+ assertEquals(version.getFunctionArn(), detailOf(aliasEntry)
+ .path("requestContext").path("functionArn").asText());
+ assertEquals(version.getVersion(), detailOf(aliasEntry)
+ .path("responseContext").path("executedVersion").asText());
+ }
+
private void configure(String onSuccess, String onFailure) {
FunctionEventInvokeConfig config = new FunctionEventInvokeConfig();
FunctionEventInvokeConfig.DestinationConfig destinations =
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaArnInvocationAccountTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaArnInvocationAccountTest.java
index 0ae08b667a..2f4c2522ae 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaArnInvocationAccountTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaArnInvocationAccountTest.java
@@ -20,6 +20,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.AdditionalMatchers.aryEq;
import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -48,7 +50,7 @@ class LambdaArnInvocationAccountTest {
LambdaExecutorService executor = mock(LambdaExecutorService.class);
InvokeResult executorResult = new InvokeResult();
- when(executor.invoke(eq(function), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0)))
+ when(executor.invoke(eq(function), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), isNull()))
.thenReturn(executorResult);
LambdaService service = new LambdaService(
store,
@@ -76,7 +78,7 @@ class LambdaArnInvocationAccountTest {
InvokeResult result = service.invokeArn(functionArn, "{}".getBytes(), InvocationType.Event);
assertEquals("$LATEST", result.getExecutedVersion());
- verify(executor).invoke(eq(function), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0));
+ verify(executor).invoke(eq(function), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), isNull());
}
@Test
@@ -111,7 +113,7 @@ class LambdaArnInvocationAccountTest {
"alias::" + region + "::" + functionName + "::live", alias);
LambdaExecutorService executor = mock(LambdaExecutorService.class);
- when(executor.invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0)))
+ when(executor.invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), nullable(String.class)))
.thenAnswer(ignored -> new InvokeResult());
LambdaService service = new LambdaService(
functionStore,
@@ -143,8 +145,8 @@ class LambdaArnInvocationAccountTest {
assertEquals("7", versionResult.getExecutedVersion());
assertEquals("7", aliasResult.getExecutedVersion());
- verify(executor, org.mockito.Mockito.times(2))
- .invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0));
+ verify(executor).invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), eq("7"));
+ verify(executor).invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), eq("live"));
}
@Test
@@ -178,9 +180,9 @@ class LambdaArnInvocationAccountTest {
new AccountAwareStorageBackend<>(rawAliases, null, defaultAccount));
LambdaExecutorService executor = mock(LambdaExecutorService.class);
- when(executor.invoke(eq(latest), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0)))
+ when(executor.invoke(eq(latest), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), isNull()))
.thenAnswer(ignored -> new InvokeResult());
- when(executor.invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0)))
+ when(executor.invoke(eq(version), aryEq("{}".getBytes()), eq(InvocationType.Event), eq(0), nullable(String.class)))
.thenAnswer(ignored -> new InvokeResult());
LambdaService service = service(functionStore, aliasStore, executor, region, defaultAccount);
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaEventInvokeConfigTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaEventInvokeConfigTest.java
index e6fe3c383c..0445af8a5c 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaEventInvokeConfigTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaEventInvokeConfigTest.java
@@ -145,4 +145,26 @@ class LambdaEventInvokeConfigTest {
assertEquals("arn:aws:sqs:us-east-1:000000000000:dlq",
cfg.getDestinationConfig().getOnFailure().getDestination());
}
+
+ @Test
+ void invokedAliasConfigTakesPrecedenceAndFallsBackToExecutedVersion() {
+ String versionDestination = "arn:aws:sqs:us-east-1:000000000000:version-queue";
+ String aliasDestination = "arn:aws:sqs:us-east-1:000000000000:alias-queue";
+ service.putEventInvokeConfig(REGION, "test-fn", "1", Map.of(
+ "DestinationConfig", Map.of("OnSuccess", Map.of("Destination", versionDestination))));
+ service.putEventInvokeConfig(REGION, "test-fn", "prod", Map.of(
+ "DestinationConfig", Map.of("OnSuccess", Map.of("Destination", aliasDestination))));
+
+ LambdaFunction executedVersion = new LambdaFunction();
+ executedVersion.setFunctionArn("arn:aws:lambda:us-east-1:000000000000:function:test-fn:1");
+ executedVersion.setAccountId("000000000000");
+ executedVersion.setVersion("1");
+
+ assertEquals(aliasDestination, service.findEventInvokeConfig(executedVersion, "prod")
+ .orElseThrow().getDestinationConfig().getOnSuccess().getDestination());
+ assertEquals(versionDestination, service.findEventInvokeConfig(executedVersion, "other")
+ .orElseThrow().getDestinationConfig().getOnSuccess().getDestination());
+ assertEquals(versionDestination, service.findEventInvokeConfig(executedVersion)
+ .orElseThrow().getDestinationConfig().getOnSuccess().getDestination());
+ }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorServiceTest.java
index e9f09f2ca0..d4bf6b76d5 100644
--- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaExecutorServiceTest.java
@@ -27,6 +27,8 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -76,6 +78,27 @@ class LambdaExecutorServiceTest {
verify(warmPool, timeout(5000)).acquire(fn);
}
+ @Test
+ void asyncInvocationPassesInvokedAliasToDestinationRouter() {
+ AsyncInvokeDestinationRouter router = mock(AsyncInvokeDestinationRouter.class);
+ executor.shutdown();
+ executor = new LambdaExecutorService(warmPool, new ObjectMapper(), concurrencyLimiter, router);
+ RuntimeApiServer rtas = mock(RuntimeApiServer.class);
+ ContainerHandle handle = new ContainerHandle("cid-alias", "test-fn", rtas, ContainerState.WARM);
+ when(warmPool.acquire(fn)).thenReturn(handle);
+ doAnswer(inv -> {
+ PendingInvocation invocation = inv.getArgument(0);
+ invocation.getResultFuture().complete(
+ new InvokeResult(200, null, "{}".getBytes(), null, invocation.getRequestId()));
+ return invocation.getResultFuture();
+ }).when(rtas).enqueue(any(PendingInvocation.class));
+
+ byte[] payload = "{}".getBytes();
+ executor.invoke(fn, payload, InvocationType.Event, 0, "prod");
+
+ verify(router, timeout(5000)).route(eq(fn), eq(payload), any(InvokeResult.class), eq(0), eq("prod"));
+ }
+
@Test
void syncInvocationPastTheChainBound_isNotAffected() {
// Only the asynchronous path can be re-entered by a destination chain, and refusing a
@@ -234,14 +257,14 @@ class LambdaExecutorServiceTest {
doAnswer(inv -> {
routed.countDown();
return null;
- }).when(router).route(any(), any(), any(), anyInt());
+ }).when(router).route(any(), any(), any(), anyInt(), isNull());
byte[] payload = "{}".getBytes();
InvokeResult result = routingExecutor.invoke(fn, payload, InvocationType.Event);
assertEquals(202, result.getStatusCode());
assertTrue(routed.await(5, TimeUnit.SECONDS), "destination routing never ran");
- verify(router).route(fn, payload, expected, 0);
+ verify(router).route(fn, payload, expected, 0, null);
}
@Test