fix(lambda): persist ParallelizationFactor on stream event source mappings (#7299)

* fix(lambda): persist ParallelizationFactor on stream event source mappings

CreateEventSourceMapping and UpdateEventSourceMapping dropped ParallelizationFactor, and
the responses never returned it. A Terraform aws_lambda_event_source_mapping with
parallelization_factor set therefore showed a change on every plan.

The field is now validated (an integer from 1 to 10, Kinesis and DynamoDB Streams sources
only), stored, and returned on every stream mapping, 1 when none was set, as AWS does.
SQS mappings still omit it. The pollers still process one batch per shard at a time; the
docs say so.

* fix(lambda): keep ParallelizationFactor on a null update and reject values beyond long

An UpdateEventSourceMapping with "ParallelizationFactor": null cleared the stored factor,
so a mapping set to 10 read back as 1. A JSON null is an absent member, as
FunctionResponseTypes already treats it, and now leaves the factor unchanged.

The range check ran after narrowing to long, so 18446744073709551617 wrapped to 1 and was
accepted. The value is now range-checked before it is narrowed.

* fix(lambda): share one range check across the event source mapping integers

MaximumBatchingWindowInSeconds, MaximumRetryAttempts, MaximumRecordAgeInSeconds,
ScalingConfig.MaximumConcurrency and ParallelizationFactor each repeated the same numeric,
whole-number and range check. One boundedInteger helper now serves all five, with the
error messages unchanged. It checks the range before narrowing, so a value beyond long
such as 18446744073709551617 no longer wraps to 1 and passes in the other four.
This commit is contained in:
Ovidiu (Ovi) authored and GitHub committed 2026-11-02 09:36:25 -04:00
1 parent 7abba35051
commit 33e12b3cf7
5 files changed
+288 -64

No files matched your search

@@ -264,6 +264,187 @@ class EsmIntegrationTest {
.statusCode(202);
}
@Test
void parallelizationFactorPersistsAcrossCreateGetListAndUpdate() {
String streamArn = "arn:aws:dynamodb:us-east-1:000000000000:table/esm-parallel/stream/2026-01-01T00:00:00.000";
String uuid = given()
.contentType("application/json")
.body("""
{
"FunctionName": "%s",
"EventSourceArn": "%s",
"StartingPosition": "TRIM_HORIZON",
"ParallelizationFactor": 10,
"Enabled": false
}
""".formatted(FUNCTION_NAME, streamArn))
.when()
.post(LAMBDA_BASE + "/event-source-mappings/")
.then()
.statusCode(202)
.body("ParallelizationFactor", equalTo(10))
.extract()
.path("UUID");
try {
given()
.when()
.get(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then()
.statusCode(200)
.body("ParallelizationFactor", equalTo(10));
given()
.when()
.get(LAMBDA_BASE + "/event-source-mappings?EventSourceArn=" + streamArn)
.then()
.statusCode(200)
.body("EventSourceMappings.find { it.UUID == '%s' }.ParallelizationFactor".formatted(uuid),
equalTo(10));
given()
.contentType("application/json")
.body("""
{ "ParallelizationFactor": 4 }
""")
.when()
.put(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then()
.statusCode(202)
.body("ParallelizationFactor", equalTo(4));
given()
.when()
.get(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then()
.statusCode(200)
.body("ParallelizationFactor", equalTo(4));
given()
.contentType("application/json")
.body("""
{ "ParallelizationFactor": null, "BatchSize": 50 }
""")
.when()
.put(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then()
.statusCode(202)
.body("BatchSize", equalTo(50))
.body("ParallelizationFactor", equalTo(4));
} finally {
given().delete(LAMBDA_BASE + "/event-source-mappings/" + uuid).then().statusCode(202);
}
}
@Test
void parallelizationFactorDefaultsToOneOnStreamMappingsOnly() {
String kinesisUuid = given()
.contentType("application/json")
.body("""
{
"FunctionName": "%s",
"EventSourceArn": "%s",
"StartingPosition": "LATEST",
"Enabled": false
}
""".formatted(FUNCTION_NAME, STREAM_ARN))
.when()
.post(LAMBDA_BASE + "/event-source-mappings/")
.then()
.statusCode(202)
.body("ParallelizationFactor", equalTo(1))
.extract()
.path("UUID");
given().delete(LAMBDA_BASE + "/event-source-mappings/" + kinesisUuid).then().statusCode(202);
String sqsUuid = given()
.contentType("application/json")
.body("""
{
"FunctionName": "%s",
"EventSourceArn": "%s",
"Enabled": false
}
""".formatted(FUNCTION_NAME, QUEUE_ARN))
.when()
.post(LAMBDA_BASE + "/event-source-mappings/")
.then()
.statusCode(202)
.body("$", not(hasKey("ParallelizationFactor")))
.extract()
.path("UUID");
given().delete(LAMBDA_BASE + "/event-source-mappings/" + sqsUuid).then().statusCode(202);
}
@Test
void createEventSourceMappingRejectsParallelizationFactorOutOfRange() {
for (String factor : List.of("0", "11", "2.5", "18446744073709551617")) {
given()
.contentType("application/json")
.body("""
{
"FunctionName": "%s",
"EventSourceArn": "%s",
"StartingPosition": "LATEST",
"ParallelizationFactor": %s,
"Enabled": false
}
""".formatted(FUNCTION_NAME, STREAM_ARN, factor))
.when()
.post(LAMBDA_BASE + "/event-source-mappings")
.then()
.statusCode(400)
.body("__type", equalTo("InvalidParameterValueException"));
}
}
/** 2^64 + 1 is a whole number that wraps to 1 when narrowed to long, so each range check must see it first. */
@Test
void createEventSourceMappingRejectsIntegerOptionsBeyondLong() {
String beyondLong = "18446744073709551617";
List<String[]> cases = List.of(
new String[] {QUEUE_ARN, "\"MaximumBatchingWindowInSeconds\": " + beyondLong},
new String[] {QUEUE_ARN, "\"ScalingConfig\": {\"MaximumConcurrency\": " + beyondLong + "}"},
new String[] {STREAM_ARN, "\"StartingPosition\": \"LATEST\", \"MaximumRetryAttempts\": " + beyondLong},
new String[] {STREAM_ARN, "\"StartingPosition\": \"LATEST\", \"MaximumRecordAgeInSeconds\": " + beyondLong});
for (String[] c : cases) {
given()
.contentType("application/json")
.body("""
{
"FunctionName": "%s",
"EventSourceArn": "%s",
"Enabled": false,
%s
}
""".formatted(FUNCTION_NAME, c[0], c[1]))
.when()
.post(LAMBDA_BASE + "/event-source-mappings")
.then()
.statusCode(400)
.body("message", containsString("(got " + beyondLong + ")"));
}
}
@Test
void createEventSourceMappingRejectsParallelizationFactorOnSqsSource() {
given()
.contentType("application/json")
.body("""
{
"FunctionName": "%s",
"EventSourceArn": "%s",
"ParallelizationFactor": 2
}
""".formatted(FUNCTION_NAME, QUEUE_ARN))
.when()
.post(LAMBDA_BASE + "/event-source-mappings")
.then()
.statusCode(400)
.body("message", containsString("only supported for Amazon Kinesis and Amazon DynamoDB Streams"));
}
@Test
void createLatestDynamoDbMappingStartsAfterTheNewestRecord() {
String tableName = "esm-latest-" + UUID.randomUUID();