fix(lambda): preserve CloudFormation event source mapping options (#4316)

AWS::Lambda::EventSourceMapping now forwards MaximumBatchingWindowInSeconds,
MaximumRetryAttempts, MaximumRecordAgeInSeconds, BisectBatchOnFunctionError,
FilterCriteria, DestinationConfig and ScalingConfig to LambdaService, so a
stack-created mapping matches an API-created one. On UpdateStack each of
these that the template omits is sent as null, which clears it.

ParallelizationFactor and TumblingWindowInSeconds are not modeled and remain
ignored; the resource-type table notes this.

Fixes #4313
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-09-24 21:05:00 -04:00
1 parent eb2872f578
commit 4abba5a218
5 files changed
+547 -44

No files matched your search

@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
@@ -17,6 +18,7 @@ import java.util.Map;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -32,6 +34,10 @@ import static org.mockito.Mockito.when;
class LambdaEventSourceMappingCfnProvisionerTest {
private static final List<String> CLEARABLE_OPTIONS = List.of(
"MaximumBatchingWindowInSeconds", "MaximumRetryAttempts", "MaximumRecordAgeInSeconds",
"BisectBatchOnFunctionError", "FilterCriteria", "DestinationConfig", "ScalingConfig");
private final LambdaService lambdaService = mock(LambdaService.class);
private final LambdaEventSourceMappingCfnProvisioner provisioner =
new LambdaEventSourceMappingCfnProvisioner(lambdaService);
@@ -51,6 +57,10 @@ class LambdaEventSourceMappingCfnProvisionerTest {
if (node.isObject() && node.has("Ref")) {
return "resolved-" + node.get("Ref").asText();
}
if (node.isObject() && node.has("Fn::GetAtt")) {
JsonNode getAtt = node.get("Fn::GetAtt");
return "resolved-" + getAtt.get(0).asText() + "." + getAtt.get(1).asText();
}
return node.asText();
});
when(engine.resolveNode(any())).thenAnswer(inv -> resolveMockNode((JsonNode) inv.getArgument(0), engine));
@@ -89,7 +99,7 @@ class LambdaEventSourceMappingCfnProvisionerTest {
return node;
}
if (node.isObject()) {
if (node.has("Ref")) {
if (node.has("Ref") || node.has("Fn::GetAtt")) {
return mapper.getNodeFactory().textNode(engine.resolve(node));
}
ObjectNode resolved = mapper.createObjectNode();
@@ -281,6 +291,269 @@ class LambdaEventSourceMappingCfnProvisionerTest {
assertEquals("Value of property BatchSize must be an integer.", ex.getMessage());
}
private Map<String, Object> provisionAndCaptureCreateRequest(ObjectNode props) {
StackResource r = new StackResource();
r.setResourceType("AWS::Lambda::EventSourceMapping");
EventSourceMapping esm = new EventSourceMapping();
esm.setUuid("options-esm-uuid");
when(lambdaService.createEventSourceMapping(eq("us-east-1"), any())).thenReturn(esm);
provisioner.provision(r, props, ctx());
@SuppressWarnings("unchecked")
ArgumentCaptor<Map<String, Object>> captor = ArgumentCaptor.forClass(Map.class);
verify(lambdaService).createEventSourceMapping(eq("us-east-1"), captor.capture());
return captor.getValue();
}
@Test
void provisionForwardsBatchingRetryAndStructureOptionsOnCreate() {
String pattern = "{\"body\":{\"kind\":[\"order\"]}}";
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
props.put("EventSourceArn", "arn:aws:sqs:us-east-1:000000000000:my-queue");
props.put("MaximumBatchingWindowInSeconds", 10);
props.put("MaximumRetryAttempts", 3);
props.put("MaximumRecordAgeInSeconds", 120);
props.put("BisectBatchOnFunctionError", true);
props.putObject("FilterCriteria").putArray("Filters").addObject().put("Pattern", pattern);
props.putObject("DestinationConfig").putObject("OnFailure")
.put("Destination", "arn:aws:sqs:us-east-1:000000000000:my-dlq");
props.putObject("ScalingConfig").put("MaximumConcurrency", 5);
Map<String, Object> req = provisionAndCaptureCreateRequest(props);
assertEquals(Integer.valueOf(10), req.get("MaximumBatchingWindowInSeconds"));
assertEquals(Integer.valueOf(3), req.get("MaximumRetryAttempts"));
assertEquals(Integer.valueOf(120), req.get("MaximumRecordAgeInSeconds"));
assertEquals(Boolean.TRUE, req.get("BisectBatchOnFunctionError"));
assertEquals(Map.of("Filters", List.of(Map.of("Pattern", pattern))), req.get("FilterCriteria"));
assertEquals(Map.of("OnFailure", Map.of("Destination", "arn:aws:sqs:us-east-1:000000000000:my-dlq")),
req.get("DestinationConfig"));
assertEquals(Map.of("MaximumConcurrency", 5), req.get("ScalingConfig"));
}
@Test
void provisionResolvesGetAttInDestinationConfig() {
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
ObjectNode getAtt = mapper.createObjectNode();
getAtt.set("Fn::GetAtt", mapper.createArrayNode().add("MyDlq").add("Arn"));
props.putObject("DestinationConfig").putObject("OnFailure").set("Destination", getAtt);
Map<String, Object> req = provisionAndCaptureCreateRequest(props);
assertEquals(Map.of("OnFailure", Map.of("Destination", "resolved-MyDlq.Arn")), req.get("DestinationConfig"));
}
@Test
void provisionOmitsAbsentOptionsOnCreate() {
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
props.put("EventSourceArn", "arn:aws:sqs:us-east-1:000000000000:my-queue");
Map<String, Object> req = provisionAndCaptureCreateRequest(props);
for (String option : CLEARABLE_OPTIONS) {
assertFalse(req.containsKey(option), option + " must not be sent when absent");
}
}
@Test
void provisionTreatsNoValueStructureAsAbsentOnCreate() {
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
props.put("DestinationConfig", "");
Map<String, Object> req = provisionAndCaptureCreateRequest(props);
assertFalse(req.containsKey("DestinationConfig"));
}
@Test
void provisionClearsNoValueStructureOnUpdate() {
StackResource r = new StackResource();
r.setResourceType("AWS::Lambda::EventSourceMapping");
r.setPhysicalId("existing-esm-uuid");
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
props.put("DestinationConfig", "");
provisioner.provision(r, props, ctx(true, "existing-esm-uuid"));
@SuppressWarnings("unchecked")
ArgumentCaptor<Map<String, Object>> captor = ArgumentCaptor.forClass(Map.class);
verify(lambdaService).updateEventSourceMapping(eq("existing-esm-uuid"), captor.capture());
assertTrue(captor.getValue().containsKey("DestinationConfig"));
assertNull(captor.getValue().get("DestinationConfig"));
}
@Test
void provisionTreatsNoValueScalarsAsAbsentOnCreate() {
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
props.put("MaximumRetryAttempts", "");
props.put("BisectBatchOnFunctionError", "");
Map<String, Object> req = provisionAndCaptureCreateRequest(props);
assertFalse(req.containsKey("MaximumRetryAttempts"));
assertFalse(req.containsKey("BisectBatchOnFunctionError"));
}
@Test
void provisionRejectsWhitespaceBatchSize() {
StackResource r = new StackResource();
r.setResourceType("AWS::Lambda::EventSourceMapping");
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
props.put("BatchSize", " ");
AwsException ex = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx()));
assertEquals("ValidationError", ex.getErrorCode());
assertEquals("Value of property BatchSize must be an integer.", ex.getMessage());
}
@Test
void provisionRejectsNonBooleanBisectBatchOnFunctionError() {
StackResource r = new StackResource();
r.setResourceType("AWS::Lambda::EventSourceMapping");
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
props.put("BisectBatchOnFunctionError", "yes");
AwsException ex = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx()));
assertEquals("ValidationError", ex.getErrorCode());
assertEquals("BisectBatchOnFunctionError must be a boolean", ex.getMessage());
}
@Test
void provisionRejectsNonObjectDestinationConfig() {
StackResource r = new StackResource();
r.setResourceType("AWS::Lambda::EventSourceMapping");
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
props.put("DestinationConfig", "arn:aws:sqs:us-east-1:000000000000:my-dlq");
AwsException ex = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx()));
assertEquals("ValidationError", ex.getErrorCode());
assertEquals("DestinationConfig must be an object", ex.getMessage());
}
@Test
void provisionKeepsWhitespaceMemberForLambdaValidation() {
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
props.putObject("DestinationConfig").put("OnFailure", " ");
Map<String, Object> req = provisionAndCaptureCreateRequest(props);
assertEquals(Map.of("OnFailure", " "), req.get("DestinationConfig"));
}
@Test
void provisionDropsNestedNoValueFromDestinationConfig() {
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
props.putObject("DestinationConfig").put("OnFailure", "");
Map<String, Object> req = provisionAndCaptureCreateRequest(props);
// LambdaService treats a DestinationConfig without OnFailure as no destination.
assertEquals(Map.of(), req.get("DestinationConfig"));
}
@Test
void provisionDropsNoValueFilterElement() {
String pattern = "{\"body\":{\"kind\":[\"order\"]}}";
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
ArrayNode filters = props.putObject("FilterCriteria").putArray("Filters");
filters.addObject().put("Pattern", pattern);
filters.add("");
Map<String, Object> req = provisionAndCaptureCreateRequest(props);
assertEquals(Map.of("Filters", List.of(Map.of("Pattern", pattern))), req.get("FilterCriteria"));
}
@Test
void provisionDropsNestedNoValueMaximumConcurrency() {
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
props.put("EventSourceArn", "arn:aws:sqs:us-east-1:000000000000:my-queue");
props.putObject("ScalingConfig").put("MaximumConcurrency", "");
Map<String, Object> req = provisionAndCaptureCreateRequest(props);
// LambdaService treats an empty ScalingConfig on an SQS mapping as clearing the cap.
assertEquals(Map.of(), req.get("ScalingConfig"));
}
@Test
void provisionCoercesTextMaximumConcurrencyToInteger() {
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
props.putObject("ScalingConfig").put("MaximumConcurrency", "5");
Map<String, Object> req = provisionAndCaptureCreateRequest(props);
assertEquals(Map.of("MaximumConcurrency", 5), req.get("ScalingConfig"));
}
@Test
void provisionRejectsInvalidMaximumConcurrency() {
StackResource r = new StackResource();
r.setResourceType("AWS::Lambda::EventSourceMapping");
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
props.putObject("ScalingConfig").put("MaximumConcurrency", "many");
AwsException ex = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx()));
assertEquals("ValidationError", ex.getErrorCode());
assertEquals("Value of property ScalingConfig.MaximumConcurrency must be an integer.", ex.getMessage());
}
@Test
void provisionRejectsInvalidMaximumRetryAttempts() {
StackResource r = new StackResource();
r.setResourceType("AWS::Lambda::EventSourceMapping");
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
props.put("MaximumRetryAttempts", "not-a-number");
AwsException ex = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx()));
assertEquals("ValidationError", ex.getErrorCode());
assertEquals("Value of property MaximumRetryAttempts must be an integer.", ex.getMessage());
}
@Test
void provisionUpdateClearsRemovedBatchingRetryAndStructureOptions() {
StackResource r = new StackResource();
r.setResourceType("AWS::Lambda::EventSourceMapping");
r.setPhysicalId("existing-esm-uuid");
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
provisioner.provision(r, props, ctx(true, "existing-esm-uuid"));
@SuppressWarnings("unchecked")
ArgumentCaptor<Map<String, Object>> captor = ArgumentCaptor.forClass(Map.class);
verify(lambdaService).updateEventSourceMapping(eq("existing-esm-uuid"), captor.capture());
Map<String, Object> req = captor.getValue();
for (String option : CLEARABLE_OPTIONS) {
assertTrue(req.containsKey(option), option + " must be sent so the update clears it");
assertNull(req.get(option), option + " must be cleared");
}
}
@Test
void provisionUpdatesClearsSourceAccessConfigurationsWhenRemoved() {
StackResource r = new StackResource();