Skip to content

fix(ec2): a block device mapping AWS refuses is refused, not materialized (#671) - #680

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/671-invalid-block-device-mapping
Aug 20, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/671-invalid-block-device-mapping

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #671.

RunInstances accepted every block device mapping it could parse, so IaC carrying a mapping real EC2 rejects got a green test here and a failure on AWS — the same class of defect an empty ImageId used to have, and the worst kind for a tool that exists to validate infrastructure code before it reaches AWS. Six refusals now answer InvalidBlockDeviceMapping with a 400, before anything is written.

Refused Provenance
An Ebs structure naming neither Ebs.VolumeSize nor Ebs.SnapshotId Documented verbatim on EbsBlockDevice.VolumeSize: "You must specify either a snapshot ID or a volume size."
Ebs.Throughput on an explicitly named type other than gp3 Documented verbatim: "This parameter is valid only for gp3 volumes."
Ebs.Iops on an explicitly named standard, st1 or sc1 The sibling launch-template shape — substrate's reading
An unparseable Ebs.VolumeSize, Ebs.Iops or Ebs.Throughput Substrate's own
Two mappings naming one DeviceName Substrate's own — AWS documents no rule
A VirtualName beside any Ebs.* member Substrate's own — AWS documents no rule

Scope: only what the API model states

Both type-scoped rules key off the explicitly named VolumeType, never the resolved one. Substrate resolves an absent type to gp2, but on real EC2 it comes from the AMI's own mapping — commonly gp3 — so refusing Throughput on a mapping that named no type would refuse a launch real EC2 accepts. For the same reason the per-type numeric size and IOPS ranges the reference lists are not encoded: they change, and a stale range is a false deny a caller cannot work around. And a VolumeSize is not compared against the named snapshot's, because EC2Snapshot.VolumeSize is a constant at its single producer and there is no CreateSnapshot — the comparison would test the constant. Follow-up filed for that one.

A mapping carrying no Ebs structure at all — a bare DeviceName — is still accepted and still takes the 8 GiB default: the size-or-snapshot requirement is documented on a member of the Ebs structure, so with no structure it has no subject. NoDevice and a VirtualName-only instance store mapping are untouched.

Ebs.Iops uses a short deny list rather than AWS's io1 | io2 | gp3 allow list. The "supported for io1, io2, and gp3 volumes only" sentence is on LaunchTemplateEbsBlockDeviceRequest.Iops, not on the EbsBlockDevice shape RunInstances accepts, and that same member's own paragraph — on both shapes — explains what Iops means "for gp2 volumes". AWS contradicts itself inside one member, so substrate refuses only the three types that appear in neither list.

Placement

The validator sits between the ImageId check and the ensureDefaultVPC branch, and both sides of that position are load-bearing:

  • After the launch template merge, so a mapping that reaches a launch through a template is refused at RunInstances time rather than materialized — and one validator covers requests, templates, template versions and fleet launches alike, since a fleet reaches mappings only through a template.
  • Before ensureDefaultVPC, which commits a VPC, subnet, security group, internet gateway, route table and four index mutations. A refusal past that point leaves state the next request in the same test sees, which is worse than no validation at all.

CreateLaunchTemplate deliberately does not refuse: its response carries a documented warning member of type ValidationWarning that exists precisely for "parameters or parameter combinations that are not valid", and its Errors section lists none — so a 400 there would be substrate's invention. Rendering that element is a follow-up.

Recording the raw values

EC2BlockDeviceMapping gains VolumeSizeRaw, IOPSRaw and ThroughputRaw. The parser discarded them, so by validation time Ebs.VolumeSize=60GB was indistinguishable from an absent size — and a literal 0 is not the same thing as absent, which is exactly what the size-or-snapshot rule is about. They are persisted rather than derived because a launch template carries its mappings into state and the original parameters are gone by the time a RunInstances names it. Adding fields to a persisted struct is replay-safe: an older event log unmarshals them empty, which reads as "nothing unparseable" — the behavior those logs already had.

Provenance

InvalidBlockDeviceMapping is documented in EC2's client-error table ("A block device mapping parameter is not valid. The returned message indicates the incorrect value."). The HTTP status is a class-level inference — the table says only that client errors are "accompanied by a 400-series HTTP response code" — and every per-case message is substrate's own, since AWS publishes no wording. Each message names the offending device, because a request can carry many mappings and the code alone does not say which was refused.

Fail-before

All 8 refusal subtests plus both ordering tests fail on the pre-fix tree:

--- FAIL: TestEC2_BlockDeviceMapping_Invalid/an_unparseable_size
    expected: 400 / actual: 200
    expected: "InvalidBlockDeviceMapping" / actual: ""
    expected: "Invalid block device mapping for /dev/sdf: Ebs.VolumeSize value \"60GB\" is not an integer" / actual: ""
--- FAIL: TestEC2_BlockDeviceMapping_RefusalWritesNothing
    expected: "InvalidBlockDeviceMapping" / actual: ""

Tests

  • TestEC2_BlockDeviceMapping_Invalid — 8 rows, one per refusal, asserting 400 + code + exact message.
  • TestEC2_BlockDeviceMapping_ValidIsAccepted — 7 rows pinning the non-refusals, so the scope decision is a test rather than a comment: a bare device name; a snapshot with no size; Iops/Throughput with no VolumeType; Iops on gp2; NoDevice beside an instance store mapping; a suppressed device sharing a name with a volume; a size outside the documented range.
  • TestEC2_BlockDeviceMapping_RefusalWritesNothing — no volume, VPC, subnet or instance after a refusal.
  • TestEC2_BlockDeviceMapping_TemplateMappingRefusedAtLaunch — CreateLaunchTemplate returns 200; the RunInstances naming it is refused.
  • TestJourney_EC2InvalidBlockDeviceMappingRefused — SDK tier, smithy.APIError with the code, then zero VPCs and zero volumes.

Two existing parser rows are re-labelled as API-unreachable rather than deleted: the unparseable-size row still pins the parser's half of the contract, and the virtualName-beside-Ebs row still pins the materializer's totality over replayed or pre-existing state.

Compatibility

A RunInstances or fleet launch that v0.104.0 and earlier accepted may now fail with InvalidBlockDeviceMapping. The likeliest case by far is a mapping naming a VolumeType and no size, which previously produced an 8 GiB volume of that type. No CloudFormation, CDK or Terraform fixture in the repo carries a block device mapping.

Verification

gofmt -l ., go build ./..., go vet ./..., golangci-lint run ./... (0 issues), make test (race, all packages), go run ./cmd/gen-service-reference -check, bash scripts/check-doc-versions.sh, go test -C test/e2e ./..., npm --prefix docs run build — all clean.

Every mapping substrate could parse was accepted and materialized, so IaC
carrying an invalid mapping got a green test here and a failure on real AWS.
Six refusals now answer InvalidBlockDeviceMapping with a 400, before anything
is written.

Two are documented verbatim (an Ebs structure naming neither VolumeSize nor
SnapshotId; Throughput on a non-gp3 type). Iops on standard/st1/sc1 rests on
the sibling launch-template shape. An unparseable numeric value, a duplicate
DeviceName and a VirtualName beside an Ebs member are substrate's own reading.

Both type-scoped rules key off the explicitly named VolumeType, never the
resolved one: substrate defaults absent to gp2 while real EC2 inherits from
the AMI, so keying off the resolved type would be a false deny. Per-type
numeric ranges are deliberately not encoded for the same reason.

The validator runs after the template merge — so a template-carried mapping is
refused at RunInstances time, and one validator covers requests, templates,
template versions and fleet launches — and before ensureDefaultVPC, so a
refusal leaves no VPC, subnet, security group or volume behind.

EC2BlockDeviceMapping gains VolumeSizeRaw/IOPSRaw/ThroughputRaw because the
parser discarded them, leaving an unparseable value indistinguishable from an
absent one; adding fields is replay-safe.
@codecov

codecov Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit e111039 into main Aug 20, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the fix/671-invalid-block-device-mapping branch August 20, 2026 22:16
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: an invalid block device mapping is accepted instead of refused with InvalidBlockDeviceMapping

1 participant