Skip to content

fix(ec2): RunInstances must reject invalid MinCount/MaxCount (#431) - #438

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/431-run-instances-counts
Aug 1, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/431-run-instances-counts

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #431

The defect

RunInstances parsed both counts with strconv.Atoi's error discarded and clamped anything <= 0 up to 1:

minCount, _ := strconv.Atoi(req.Params["MinCount"])
if minCount <= 0 { minCount = 1 }

So MinCount=0 launched an instance where real EC2 fails the request, and MinCount=abc launched one too. That is worse than a missing error — it is a confidently wrong answer. A consumer asserting on the launched count got a green run for a request AWS rejects.

What changed

New emulator/ec2_runinstances_counts.go. A count that is present but invalid now fails; a count that is absent still defaults.

Request Before After
MinCount=0 1 instance 400 InvalidParameterValue
MaxCount=0 1 instance 400
MinCount=-1 1 instance 400
MinCount=abc 1 instance 400
MinCount=3&MaxCount=1 1 instance 400
Neither given 1 instance 1 instance (unchanged)
MinCount=2 alone 1 instance 2 instances
MinCount=1&MaxCount=3 3 instances 3 instances (unchanged)

The MinCount=2 row is the one behavior change beyond the errors: an absent MaxCount defaults to MinCount rather than to 1, so a request asking for three no longer silently launches one.

Why presence is not required

The declined half of #412 asked for a "quick pass for other required params". AWS marks both counts Required: Yes, but in every typed SDK they are required members that fail client-side — MaxCount == nil is smithy.NewErrParamRequired("MaxCount") in aws-sdk-go-v2's validateOpRunInstancesInput, and botocore rejects the missing kwarg before serializing. A consumer bug there cannot reach the wire, so validating presence would catch an unreachable bug class while breaking every hand-built form-encoded request — which substrate's own tests and betty_cfn.go's CFN path are.

Values are the opposite. Verified empirically: botocore's ParamValidator accepts MinCount=0, MinCount=3/MaxCount=1 and MinCount=-1 with zero validation errors, and aws-sdk-go-v2 checks only for nil. Both reach the wire from ordinary typed code. That split is the whole justification for validating one and not the other.

Error codes: verified, not guessed

Per CLAUDE.md, checked against the AWS reference — and the answer came out different from the issue's own guess:

  • The RunInstances reference documents no action-specific error for these and defers to the common errors. The EC2 errors overview lists nothing for instance counts either.
  • InvalidParameterValue is documented as "A value that you provided for a parameter isn't valid" (HTTP 400) — exactly an out-of-range or unparseable count, given the documented constraint "Between 1 and the quota for the specified instance type".
  • MinCount > MaxCount uses the same code rather than the plausible-looking InvalidParameterCombination, which is defined as "Parameters that must not be used together were used together". That cannot describe two parameters AWS documents as used together; the defect is the value of maxCount relative to minCount.

#413 is why a plausible guess about an AWS error code is not good enough to ship — SDK callers match the literal string.

The upper bound is a per-account, per-instance-type quota substrate does not model, so it is not enforced.

Only MaxCount reaches the launch loop

resolveInstanceCounts returns both, but runInstances discards minCount. AWS "launches the largest possible number of instances above the specified minimum count", and substrate models no capacity ceiling, so the largest possible number is always MaxCount and MinCount can only ever be satisfied. It is still validated, and still sets MaxCount's default — which is all it can affect. The discard is commented as a decision rather than left as a silent drop.

Tests

TestEC2_RunInstances_InstanceCounts, beside the #412 test, in two groups:

  • rejected — 8 rows, each asserting status, code and message, plus that DescribeInstances reports nothing (a rejected launch must create nothing, or a consumer's cleanup path is wrong).
  • accepted — 6 rows asserting the launched count on both the RunInstances response and DescribeInstances, including the two "absence still defaults" regression guards the issue asked for.

Verified by mutation, not just by passing:

  • Restoring the old clamp → all 8 rejection rows fail, accepted rows still pass.
  • Defaulting MaxCount to 1 instead of MinCount → the row pinning that fails.

Blast radius

Zero. All 101 existing RunInstances test references use MinCount=1/MaxCount=1 or omit both. betty_cfn.go:2054-2055 passes "1"/"1". ec2_fleet.go:437-438 passes count for both but is guarded by if gotPerPool[i] > 0, so no zero can reach runInstances from the fleet path.

Verification

gofmt · go build ./... · go vet ./... · golangci-lint run ./... (0 issues) · make test (race) · test/e2e · make docs-reference-check docs-versions — all green.

Docs: new ### MinCount and MaxCount section in docs/services.md with the behavior table and the code rationale; the RunInstances ops-table row cross-references it.

Both counts were parsed with strconv.Atoi's error discarded and anything
<= 0 clamped up to 1, so MinCount=0 launched an instance where real EC2
fails the request, and MinCount=abc launched one too. That is worse than
a missing error: it is a confidently wrong answer, so a consumer
asserting on the launched count got a green run for a request AWS
rejects.

A count that is present but unparseable or below 1, or a MinCount above
MaxCount, is now InvalidParameterValue / HTTP 400. Absence still
defaults, and an absent MaxCount defaults to MinCount rather than to 1,
so a request asking for three no longer risks launching one.

Presence is deliberately not required. Both are required members that
fail client-side in every typed SDK, so a consumer bug there cannot
reach the wire, while requiring presence here would break hand-built
form-encoded requests — substrate's own tests and betty_cfn.go's CFN
path. Values are different: the query protocol carries them as strings,
botocore's ParamValidator accepts MinCount=0 and MinCount=3/MaxCount=1
unchanged, and aws-sdk-go-v2's validateOpRunInstancesInput checks only
for nil. That split is why one is validated and the other is not.

The codes were verified against the RunInstances reference rather than
guessed, which changed the answer: the reference documents no
action-specific error for these and defers to the common errors, and
InvalidParameterCombination is defined as "Parameters that must not be
used together were used together" — which cannot describe two
parameters AWS documents as used together. All three cases use
InvalidParameterValue. #413 is why a plausible guess about an AWS error
code is not good enough to ship.

Only MaxCount reaches the launch loop: AWS launches "the largest
possible number of instances above the specified minimum count" and
substrate models no capacity ceiling, so that is always MaxCount and
MinCount can only ever be satisfied. It is still validated, and still
sets MaxCount's default.

Both directions of the fix were verified by mutation: restoring the
clamp fails all eight rejection rows, and defaulting MaxCount to 1
instead of MinCount fails the row that pins it.

Closes #431
@codecov

codecov Bot commented Aug 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EC2: RunInstances accepts MinCount/MaxCount that are present but invalid

1 participant