feat(lambda): support ScalingConfig.MaximumConcurrency on SQS event source mappings (#490)

* feat(lambda): support ScalingConfig.MaximumConcurrency on SQS event source mappings

Wire-level support for ScalingConfig on CreateEventSourceMapping,
UpdateEventSourceMapping, GetEventSourceMapping, and
ListEventSourceMappings:

- New ScalingConfig model carrying MaximumConcurrency (Integer, nullable).
- Create and Update parse ScalingConfig from the request, validate range
  (2-1000), reject non-SQS event sources with InvalidParameterValueException.
- Response includes ScalingConfig only when a cap is set; omitted when
  null, matching AWS behavior.
- Update with an empty ScalingConfig clears the cap (AWS reset semantics).

The value is persisted on the EventSourceMapping but not yet enforced at
invocation time — the SQS poller today serializes to one concurrent
invocation per ESM regardless. Real parallel dispatch capped by
MaximumConcurrency is a follow-up (Scope B).

Tests:
- EsmIntegrationTest: 6 new cases covering round-trip, below/above range
  rejection, non-SQS rejection, omission when unset, add-then-clear via
  update.
- LambdaEsmScalingConfigTest (SDK compat): 5 cases verifying SDK typed
  ScalingConfig round-trips, InvalidParameterValueException on range
  violations, and add/clear lifecycle via update.

* fix(lambda): reject non-integer MaximumConcurrency and ScalingConfig on non-SQS sources

- parseScalingConfig now detects fractional values (e.g. 2.5) and
  returns InvalidParameterValueException instead of silently truncating.
- Empty ScalingConfig ({}) on a non-SQS event source is now rejected.
  Previously the non-SQS check only fired when MaximumConcurrency was
  non-null, so an empty ScalingConfig on Kinesis / DynamoDB Streams
  slipped through.
- Two new integration tests cover both cases.

* fix(lambda): tighten ScalingConfig input validation

- Reject non-object ScalingConfig (e.g. "foo", []) with 400 instead of
  silently treating it as absent. In update paths this previously cleared
  an existing cap unintentionally.
- Reject non-numeric MaximumConcurrency (e.g. string "7"). AWS expects a
  JSON number on the wire; the previous toString + parseInt path accepted
  strings that real AWS would reject.
- Use longValue() before range-checking to avoid intValue() overflow on
  large inputs (e.g. 5000000000) that would produce a negative value and
  a misleading error message.
- Two new integration tests: string MaximumConcurrency and non-object
  ScalingConfig.

* fix(lambda): add NaN/Infinity guard and expand ScalingConfig test coverage

- parseScalingConfig now explicitly rejects NaN and Infinity rather than
  letting them fall through to longValue() with a misleading error.
- The post-range non-SQS guard was flagged as dead code by the reviewer
  but is in fact reachable (non-SQS + valid numeric MaximumConcurrency).
  Retained and confirmed by the existing @Order(43) test.
- New tests: update range validation (below/above on PUT), and list
  with mixed ScalingConfig presence across ESMs.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-04-17 00:16:55 -05:00
1 parent ca2e5d4153
commit d8243b8803
7 files changed
+644 -1

No files matched your search

@@ -295,6 +295,14 @@ public class LambdaController {
if (esm.getFunctionResponseTypes() != null) {
esm.getFunctionResponseTypes().forEach(responseTypes::add);
}
// Only emit ScalingConfig when a cap is actually set — AWS omits the
// field entirely on mappings with no MaximumConcurrency rather than
// returning an empty object.
Integer maxConcurrency = esm.getMaximumConcurrency();
if (maxConcurrency != null) {
ObjectNode scaling = node.putObject("ScalingConfig");
scaling.put("MaximumConcurrency", maxConcurrency.intValue());
}
@SuppressWarnings("unchecked")
Map<String, Object> result = objectMapper.convertValue(node, Map.class);
return result;
@@ -5,11 +5,12 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.lambda.model.EventSourceMapping;
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
import io.github.hectorvent.floci.services.lambda.model.InvokeResult;
import io.github.hectorvent.floci.services.lambda.model.LambdaAlias;
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
import io.github.hectorvent.floci.services.lambda.model.LambdaUrlConfig;
import io.github.hectorvent.floci.services.lambda.model.ScalingConfig;
import io.github.hectorvent.floci.services.lambda.zip.CodeStore;
import io.github.hectorvent.floci.services.lambda.zip.ZipExtractor;
import io.github.hectorvent.floci.services.s3.S3Service;
@@ -442,6 +443,8 @@ public class LambdaService {
? (List<String>) request.get("FunctionResponseTypes")
: new ArrayList<>();
ScalingConfig scalingConfig = parseScalingConfig(request, eventSourceArn);
String queueUrl = eventSourceArn.contains(":sqs:") ? AwsArnUtils.arnToQueueUrl(eventSourceArn, config.effectiveBaseUrl()) : null;
EventSourceMapping esm = new EventSourceMapping();
@@ -454,6 +457,7 @@ public class LambdaService {
esm.setBatchSize(batchSize);
esm.setEnabled(enabled);
esm.setState(enabled ? "Enabled" : "Disabled");
esm.setScalingConfig(scalingConfig);
esm.setFunctionResponseTypes(functionResponseTypes);
esm.setLastModified(System.currentTimeMillis());
@@ -465,6 +469,53 @@ public class LambdaService {
return esm;
}
/**
* Parses {@code ScalingConfig} out of a create/update request and applies
* AWS-level validation: {@code MaximumConcurrency} must be in [2, 1000]
* and is only valid on SQS event sources. Returns {@code null} when no
* config was supplied or when the supplied config has no cap (AWS treats
* an empty ScalingConfig as "clear the cap").
*/
private ScalingConfig parseScalingConfig(Map<String, Object> request, String eventSourceArn) {
Object raw = request.get("ScalingConfig");
if (raw == null) {
return null;
}
if (!(raw instanceof Map<?, ?>)) {
throw new AwsException("InvalidParameterValueException",
"ScalingConfig must be a JSON object", 400);
}
boolean isSqs = eventSourceArn != null && eventSourceArn.contains(":sqs:");
Map<?, ?> map = (Map<?, ?>) raw;
Object mc = map.get("MaximumConcurrency");
if (mc == null) {
if (!isSqs) {
throw new AwsException("InvalidParameterValueException",
"ScalingConfig is only supported for Amazon SQS event source mappings", 400);
}
return null;
}
if (!(mc instanceof Number)) {
throw new AwsException("InvalidParameterValueException",
"ScalingConfig.MaximumConcurrency must be a numeric value", 400);
}
double d = ((Number) mc).doubleValue();
if (Double.isNaN(d) || Double.isInfinite(d) || d != Math.floor(d)) {
throw new AwsException("InvalidParameterValueException",
"ScalingConfig.MaximumConcurrency must be an integer", 400);
}
long longValue = ((Number) mc).longValue();
if (longValue < 2 || longValue > 1000) {
throw new AwsException("InvalidParameterValueException",
"ScalingConfig.MaximumConcurrency must be between 2 and 1000 (got " + longValue + ")", 400);
}
if (!isSqs) {
throw new AwsException("InvalidParameterValueException",
"ScalingConfig is only supported for Amazon SQS event source mappings", 400);
}
return new ScalingConfig((int) longValue);
}
private void startPollingHelper(EventSourceMapping esm) {
if (esm.getEventSourceArn().contains(":sqs:")) {
poller.startPolling(esm);
@@ -514,6 +565,11 @@ public class LambdaService {
esm.setEnabled(nowEnabled);
esm.setState(nowEnabled ? "Enabled" : "Disabled");
}
if (request.containsKey("ScalingConfig")) {
// AWS: passing ScalingConfig resets it. An empty object or one
// with MaximumConcurrency=null clears the cap.
esm.setScalingConfig(parseScalingConfig(request, esm.getEventSourceArn()));
}
esm.setLastModified(System.currentTimeMillis());
esmStore.save(esm);
@@ -24,6 +24,7 @@ public class EventSourceMapping {
private long lastModified;
private List<String> functionResponseTypes = new ArrayList<>();
private Map<String, String> shardSequenceNumbers = new HashMap<>();
private ScalingConfig scalingConfig;
public EventSourceMapping() {
}
@@ -71,4 +72,12 @@ public class EventSourceMapping {
public void setShardSequenceNumbers(Map<String, String> shardSequenceNumbers) {
this.shardSequenceNumbers = shardSequenceNumbers != null ? shardSequenceNumbers : new java.util.HashMap<>();
}
public ScalingConfig getScalingConfig() { return scalingConfig; }
public void setScalingConfig(ScalingConfig scalingConfig) { this.scalingConfig = scalingConfig; }
/** Convenience accessor: returns {@code null} when no cap is configured. */
public Integer getMaximumConcurrency() {
return scalingConfig != null ? scalingConfig.getMaximumConcurrency() : null;
}
}
@@ -0,0 +1,38 @@
package io.github.hectorvent.floci.services.lambda.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
/**
* AWS Lambda Event Source Mapping scaling configuration.
*
* <p>Currently carries only {@code MaximumConcurrency}, the SQS-only cap on
* how many Lambda invocations an ESM may run in parallel. The rest of the
* AWS schema (none today) can extend this class as needed.
*
* <p>Wire shape:
* <pre>{@code
* "ScalingConfig": { "MaximumConcurrency": 5 }
* }</pre>
*/
@RegisterForReflection
@JsonIgnoreProperties(ignoreUnknown = true)
public class ScalingConfig {
private Integer maximumConcurrency;
public ScalingConfig() {
}
public ScalingConfig(Integer maximumConcurrency) {
this.maximumConcurrency = maximumConcurrency;
}
public Integer getMaximumConcurrency() {
return maximumConcurrency;
}
public void setMaximumConcurrency(Integer maximumConcurrency) {
this.maximumConcurrency = maximumConcurrency;
}
}