fix(lambda): retain event source qualifiers at invocation (#6288)

Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-22 20:55:44 -04:00
1 parent 2ae6d16936
commit 504e0dfde7
7 files changed
+29 -18

No files matched your search

@@ -350,7 +350,10 @@ public class DynamoDbStreamsEventSourcePoller implements Resettable {
String eventJson = buildDynamoDbEvent(matched, esm);
InvokeResult invokeResult;
try {
invokeResult = executorService.invoke(fn, eventJson.getBytes(), InvocationType.RequestResponse);
String qualifier = LambdaTargetResolver.mappingQualifier(esm);
invokeResult = qualifier == null
? executorService.invoke(fn, eventJson.getBytes(), InvocationType.RequestResponse)
: executorService.invoke(fn, eventJson.getBytes(), InvocationType.RequestResponse, 0, qualifier);
} catch (AwsException e) {
if ("TooManyRequestsException".equals(e.getErrorCode())) {
LOG.infov("DynamoDB Streams ESM {0}: function {1} throttled, shard iterator not advanced",
@@ -173,7 +173,10 @@ public class KinesisEventSourcePoller implements Resettable {
String eventJson = buildKinesisEvent(matched, esm, shard.getShardId());
InvokeResult invokeResult;
try {
invokeResult = executorService.invoke(fn, eventJson.getBytes(), InvocationType.RequestResponse);
String qualifier = LambdaTargetResolver.mappingQualifier(esm);
invokeResult = qualifier == null
? executorService.invoke(fn, eventJson.getBytes(), InvocationType.RequestResponse)
: executorService.invoke(fn, eventJson.getBytes(), InvocationType.RequestResponse, 0, qualifier);
} catch (AwsException e) {
if ("TooManyRequestsException".equals(e.getErrorCode())) {
LOG.infov("Kinesis ESM {0}: function {1} throttled, shard iterator not advanced",
@@ -52,8 +52,7 @@ public class LambdaTargetResolver {
/** The function a mapping invokes, or empty when it or its version or alias is gone. */
public Optional<LambdaFunction> resolveMappingTarget(EventSourceMapping esm) {
String qualifier = esm.getFunctionArn() == null
? null : LambdaArnUtils.resolve(esm.getFunctionArn()).qualifier();
String qualifier = mappingQualifier(esm);
try {
return Optional.of(resolveInvokeTargetForAccount(
esm.getAccountId(), esm.getRegion(), esm.getFunctionName(), qualifier));
@@ -65,6 +64,10 @@ public class LambdaTargetResolver {
}
}
static String mappingQualifier(EventSourceMapping esm) {
return esm.getFunctionArn() == null ? null : LambdaArnUtils.resolve(esm.getFunctionArn()).qualifier();
}
private LambdaFunction resolveTarget(String region, String name, String qualifier,
Function<LambdaAlias, String> aliasVersion) {
if (qualifier == null || qualifier.equals("$LATEST")) {
@@ -266,8 +266,10 @@ public class SqsEventSourcePoller implements Resettable {
LOG.infov("ESM {0}: invoking function {1}", esm.getUuid(), fn.getFunctionName());
InvokeResult result;
try {
result = executorService.invoke(
fn, eventJson.getBytes(), InvocationType.RequestResponse);
String qualifier = LambdaTargetResolver.mappingQualifier(esm);
result = qualifier == null
? executorService.invoke(fn, eventJson.getBytes(), InvocationType.RequestResponse)
: executorService.invoke(fn, eventJson.getBytes(), InvocationType.RequestResponse, 0, qualifier);
} catch (AwsException e) {
if ("TooManyRequestsException".equals(e.getErrorCode())) {
LOG.infov("ESM {0}: function {1} throttled, messages will return to queue after visibility timeout",
@@ -409,14 +409,14 @@ class DynamoDbStreamsEventSourcePollerTest {
void versionQualifiedMappingInvokesThatVersion() {
stubTrimHorizon(List.of(ddbRecord("s1", "INSERT", "{\"status\":{\"S\":\"active\"}}")));
LambdaFunction version1 = stubVersion("1");
when(executorService.invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse)))
when(executorService.invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse), eq(0), eq("1")))
.thenReturn(new InvokeResult());
EventSourceMapping esm = filterEsm();
esm.setFunctionArn("arn:aws:lambda:us-east-1:" + ACCOUNT_ID + ":function:fn:1");
pollerWith(mock(EsmStore.class)).pollAndInvoke(esm);
verify(executorService, timeout(2000)).invoke(eq(version1), any(byte[].class), eq(InvocationType.RequestResponse));
verify(executorService, timeout(2000)).invoke(eq(version1), any(byte[].class), eq(InvocationType.RequestResponse), eq(0), eq("1"));
}
@Test
@@ -428,14 +428,14 @@ class DynamoDbStreamsEventSourcePollerTest {
alias.setFunctionName("fn");
alias.setFunctionVersion("2");
when(aliasStore.getForAccount(ACCOUNT_ID, "us-east-1", "fn", "live")).thenReturn(Optional.of(alias));
when(executorService.invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse)))
when(executorService.invoke(any(), any(byte[].class), eq(InvocationType.RequestResponse), eq(0), eq("live")))
.thenReturn(new InvokeResult());
EventSourceMapping esm = filterEsm();
esm.setFunctionArn("arn:aws:lambda:us-east-1:" + ACCOUNT_ID + ":function:fn:live");
pollerWith(mock(EsmStore.class)).pollAndInvoke(esm);
verify(executorService, timeout(2000)).invoke(eq(version2), any(byte[].class), eq(InvocationType.RequestResponse));
verify(executorService, timeout(2000)).invoke(eq(version2), any(byte[].class), eq(InvocationType.RequestResponse), eq(0), eq("live"));
}
private LambdaFunction stubVersion(String version) {
@@ -168,13 +168,13 @@ class KinesisEventSourcePollerTest {
stubFunction();
LambdaFunction version1 = stubVersion("1");
stubStreamWith(List.of(record("s1", "p1", "{}")));
when(executorService.invoke(any(), any(), eq(InvocationType.RequestResponse))).thenReturn(new InvokeResult());
when(executorService.invoke(any(), any(), eq(InvocationType.RequestResponse), eq(0), eq("1"))).thenReturn(new InvokeResult());
EventSourceMapping esm = esm();
esm.setFunctionArn("arn:aws:lambda:" + REGION + ":" + ACCOUNT + ":function:fn:1");
poller.pollAndInvoke(esm);
verify(executorService, timeout(2000)).invoke(eq(version1), any(), eq(InvocationType.RequestResponse));
verify(executorService, timeout(2000)).invoke(eq(version1), any(), eq(InvocationType.RequestResponse), eq(0), eq("1"));
}
@Test
@@ -187,13 +187,13 @@ class KinesisEventSourcePollerTest {
alias.setFunctionVersion("2");
when(aliasStore.getForAccount(ACCOUNT, REGION, "fn", "live")).thenReturn(Optional.of(alias));
stubStreamWith(List.of(record("s1", "p1", "{}")));
when(executorService.invoke(any(), any(), eq(InvocationType.RequestResponse))).thenReturn(new InvokeResult());
when(executorService.invoke(any(), any(), eq(InvocationType.RequestResponse), eq(0), eq("live"))).thenReturn(new InvokeResult());
EventSourceMapping esm = esm();
esm.setFunctionArn("arn:aws:lambda:" + REGION + ":" + ACCOUNT + ":function:fn:live");
poller.pollAndInvoke(esm);
verify(executorService, timeout(2000)).invoke(eq(version2), any(), eq(InvocationType.RequestResponse));
verify(executorService, timeout(2000)).invoke(eq(version2), any(), eq(InvocationType.RequestResponse), eq(0), eq("live"));
}
private LambdaFunction stubVersion(String version) {
@@ -344,12 +344,12 @@ class SqsEventSourcePollerTest {
when(functionStore.getForAccount("000000000000", "us-east-1", "throwfn", "1"))
.thenReturn(Optional.of(version1));
stubReceive(esm, List.of(message("m1")));
when(executorService.invoke(any(LambdaFunction.class), any(byte[].class), eq(InvocationType.RequestResponse)))
when(executorService.invoke(any(LambdaFunction.class), any(byte[].class), eq(InvocationType.RequestResponse), eq(0), eq("1")))
.thenReturn(new InvokeResult());
poller.pollAndInvoke(esm);
verify(executorService, timeout(2000)).invoke(eq(version1), any(byte[].class), eq(InvocationType.RequestResponse));
verify(executorService, timeout(2000)).invoke(eq(version1), any(byte[].class), eq(InvocationType.RequestResponse), eq(0), eq("1"));
}
@Test
@@ -370,12 +370,12 @@ class SqsEventSourcePollerTest {
when(functionStore.getForAccount("000000000000", "us-east-1", "throwfn", "2"))
.thenReturn(Optional.of(version2));
stubReceive(esm, List.of(message("m1")));
when(executorService.invoke(any(LambdaFunction.class), any(byte[].class), eq(InvocationType.RequestResponse)))
when(executorService.invoke(any(LambdaFunction.class), any(byte[].class), eq(InvocationType.RequestResponse), eq(0), eq("live")))
.thenReturn(new InvokeResult());
poller.pollAndInvoke(esm);
verify(executorService, timeout(2000)).invoke(eq(version2), any(byte[].class), eq(InvocationType.RequestResponse));
verify(executorService, timeout(2000)).invoke(eq(version2), any(byte[].class), eq(InvocationType.RequestResponse), eq(0), eq("live"));
}
@Test