fix(pipes): share SQS records with Lambda pollers (#8604)

EventBridge Pipes and Lambda event source mappings now build their SQS records through one SqsEventRecordBuilder, the record Lambda already sent. Pipes gains the FIFO attributes SequenceNumber, MessageGroupId and MessageDeduplicationId, md5OfMessageAttributes when present, and a default dataType for message attributes.

A message without a first-receive timestamp takes the Pipes poller's clock instead of wall time, without writing it back to the message. Lambda's record is unchanged.

Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-11-16 12:12:09 -04:00
1 parent bb6b308fdb
commit c12db764e6
4 files changed
+157 -77

No files matched your search

@@ -35,10 +35,12 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
@@ -183,6 +185,85 @@ class PipesPollerTest {
verify(sqsService).receiveMessage(anyString(), eq(10), anyInt(), anyInt(), eq("us-east-1"));
}
@Test
void sqsDeliveryIncludesFifoSystemAttributes() throws Exception {
Message message = new Message("fifo-body");
message.setSequenceNumber(27);
message.setMessageGroupId("orders");
message.setMessageDeduplicationId("order-42");
JsonNode record = deliveredSqsRecord(message);
assertEquals("27", record.at("/attributes/SequenceNumber").asText());
assertEquals("orders", record.at("/attributes/MessageGroupId").asText());
assertEquals("order-42", record.at("/attributes/MessageDeduplicationId").asText());
assertEquals("fifo-body", record.path("body").asText());
assertEquals("aws:sqs", record.path("eventSource").asText());
assertEquals("us-east-1", record.path("awsRegion").asText());
}
@Test
void sqsDeliveryOmitsAbsentFifoSystemAttributes() throws Exception {
JsonNode record = deliveredSqsRecord(new Message("standard-body"));
assertFalse(record.path("attributes").has("SequenceNumber"));
assertFalse(record.path("attributes").has("MessageGroupId"));
assertFalse(record.path("attributes").has("MessageDeduplicationId"));
}
@Test
void missingSqsReceiveTimestampUsesThePollerClockWithoutMutatingTheMessage() throws Exception {
AtomicLong clock = new AtomicLong(1000);
poller.setClockForTest(clock::get);
Message message = new Message("body");
message.setFirstReceiveTimestamp(null);
Pipe pipe = sqsDeliveryPipe(message);
poller.pollSqs(pipe, "us-east-1");
clock.addAndGet(10);
poller.pollSqs(pipe, "us-east-1");
ArgumentCaptor<String> payload = ArgumentCaptor.forClass(String.class);
verify(targetInvoker, times(2)).invoke(eq(pipe), payload.capture(), eq("us-east-1"));
assertEquals("1000", MAPPER.readTree(payload.getAllValues().get(0))
.at("/Records/0/attributes/ApproximateFirstReceiveTimestamp").asText());
assertEquals("1010", MAPPER.readTree(payload.getAllValues().get(1))
.at("/Records/0/attributes/ApproximateFirstReceiveTimestamp").asText());
assertNull(message.getFirstReceiveTimestamp());
}
@Test
void storedSqsReceiveTimestampDoesNotReadTheFallbackClock() throws Exception {
poller.setClockForTest(() -> {
throw new AssertionError("A stored timestamp must not read the fallback clock");
});
Message message = new Message("body");
message.setFirstReceiveTimestamp(Instant.ofEpochMilli(7));
JsonNode record = deliveredSqsRecord(message);
assertEquals("7", record.at("/attributes/ApproximateFirstReceiveTimestamp").asText());
assertEquals(Instant.ofEpochMilli(7), message.getFirstReceiveTimestamp());
}
private Pipe sqsDeliveryPipe(Message message) {
Pipe pipe = new Pipe();
pipe.setName("sqs-record");
pipe.setSource("arn:aws:sqs:us-east-1:000000000000:orders.fifo");
pipe.setTarget("arn:aws:lambda:us-east-1:000000000000:function:target");
when(sqsService.receiveMessage(anyString(), anyInt(), anyInt(), anyInt(), eq("us-east-1")))
.thenReturn(List.of(message));
return pipe;
}
private JsonNode deliveredSqsRecord(Message message) throws Exception {
Pipe pipe = sqsDeliveryPipe(message);
poller.pollSqs(pipe, "us-east-1");
ArgumentCaptor<String> payload = ArgumentCaptor.forClass(String.class);
verify(targetInvoker).invoke(eq(pipe), payload.capture(), eq("us-east-1"));
return MAPPER.readTree(payload.getValue()).path("Records").get(0);
}
@Test
void pollSqs_enrichmentToNonLambdaTargetForwardsRawResponse() throws Exception {
// A non-Lambda target (here Step Functions) must receive the raw enrichment response, not a