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,8 @@ 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.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
@@ -24,6 +26,11 @@ public class LambdaEventSourceMappingCfnProvisioner implements CfnResourceProvis
private static final ObjectMapper MAPPER = new ObjectMapper();
// Options that update in place: omitting one from an UpdateStack template clears it.
private static final List<String> CLEARABLE_OPTIONS = List.of(
"MaximumBatchingWindowInSeconds", "MaximumRetryAttempts", "MaximumRecordAgeInSeconds",
"BisectBatchOnFunctionError", "FilterCriteria", "DestinationConfig", "ScalingConfig");
private final LambdaService lambdaService;
@Inject
@@ -61,14 +68,41 @@ public class LambdaEventSourceMappingCfnProvisioner implements CfnResourceProvis
req.put("Enabled", Boolean.parseBoolean(enabledStr));
}
String batchSize = ctx.resolveOptional(props, "BatchSize");
if (batchSize != null) {
for (String name : List.of("BatchSize", "MaximumBatchingWindowInSeconds",
"MaximumRetryAttempts", "MaximumRecordAgeInSeconds")) {
putInteger(req, props, ctx, name);
}
String bisect = ctx.resolveOptional(props, "BisectBatchOnFunctionError");
if (bisect != null && !bisect.isEmpty()) {
if (!"true".equalsIgnoreCase(bisect) && !"false".equalsIgnoreCase(bisect)) {
throw new AwsException("ValidationError", "BisectBatchOnFunctionError must be a boolean", 400);
}
req.put("BisectBatchOnFunctionError", Boolean.parseBoolean(bisect));
}
for (String name : List.of("FilterCriteria", "DestinationConfig", "ScalingConfig")) {
if (props != null && props.has(name)) {
JsonNode resolved = ctx.engine().resolveNode(props.get(name));
if (resolved == null || resolved.isNull() || (resolved.isTextual() && resolved.textValue().isEmpty())) {
continue;
}
if (!resolved.isObject()) {
throw new AwsException("ValidationError", name + " must be an object", 400);
}
req.put(name, MAPPER.convertValue(withoutNoValue(resolved), Map.class));
}
}
if (req.get("ScalingConfig") instanceof Map<?, ?> scaling
&& scaling.get("MaximumConcurrency") instanceof String concurrency) {
Map<Object, Object> coerced = new HashMap<>(scaling);
try {
req.put("BatchSize", Integer.parseInt(batchSize));
coerced.put("MaximumConcurrency", Integer.parseInt(concurrency));
} catch (NumberFormatException e) {
throw new AwsException("ValidationError",
"Value of property BatchSize must be an integer.", 400);
"Value of property ScalingConfig.MaximumConcurrency must be an integer.", 400);
}
req.put("ScalingConfig", coerced);
}
String startingPosition = ctx.resolveOptional(props, "StartingPosition");
@@ -116,6 +150,12 @@ public class LambdaEventSourceMappingCfnProvisioner implements CfnResourceProvis
req.put("SourceAccessConfigurations", null);
}
if (ctx.isUpdate()) {
for (String name : CLEARABLE_OPTIONS) {
req.putIfAbsent(name, null);
}
}
if (ctx.isUpdate() && ctx.priorPhysicalId() != null) {
String uuid = ctx.priorPhysicalId();
EventSourceMapping existing = findExisting(uuid);
@@ -151,6 +191,50 @@ public class LambdaEventSourceMappingCfnProvisioner implements CfnResourceProvis
}
}
private static void putInteger(Map<String, Object> req, JsonNode props, ProvisionContext ctx, String name) {
String value = ctx.resolveOptional(props, name);
if (value == null || value.isEmpty()) {
return;
}
try {
req.put(name, Integer.parseInt(value));
} catch (NumberFormatException e) {
throw new AwsException("ValidationError",
"Value of property " + name + " must be an integer.", 400);
}
}
/**
* Drops the empty text a nested {@code AWS::NoValue} resolves to, as CloudFormation removes such
* members. Returns null for an empty text node.
*/
private static JsonNode withoutNoValue(JsonNode node) {
if (node.isTextual() && node.textValue().isEmpty()) {
return null;
}
if (node.isObject()) {
ObjectNode out = MAPPER.createObjectNode();
node.fields().forEachRemaining(e -> {
JsonNode value = withoutNoValue(e.getValue());
if (value != null) {
out.set(e.getKey(), value);
}
});
return out;
}
if (node.isArray()) {
ArrayNode out = MAPPER.createArrayNode();
for (JsonNode item : node) {
JsonNode value = withoutNoValue(item);
if (value != null) {
out.add(value);
}
}
return out;
}
return node;
}
private EventSourceMapping findExisting(String uuid) {
try {
return lambdaService.getEventSourceMapping(uuid);