Skip to content

fix(ec2): a block device mapping can only name a completed snapshot (#732) - #743

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/732-mapping-snapshot-state
Aug 22, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/732-mapping-snapshot-state

Conversation

@scttfrdmn

@scttfrdmn scttfrdmn commented Aug 22, 2026 •

Copy link
Copy Markdown
Owner

Closes #732

CreateVolume has refused a snapshot that is not usable yet since #715. A launch declaring the
same snapshot in a BlockDeviceMapping materialized a volume from it and reported success.
So one rule had two doors that disagreed:

$ aws ec2 create-volume --snapshot-id snap-… --availability-zone us-east-1a
IncorrectState: … a volume can only be created from a completed snapshot
$ aws ec2 run-instances --block-device-mappings 'DeviceName=/dev/sdf,Ebs={SnapshotId=snap-…}'
i-41f8cd8460402cbf

A consumer restoring through a launch — which is what CDK's and Terraform's snapshot-backed
volumes do — never reached the error branch it had written. RegisterImage, whose own second
AWS example registers an AMI from a root-device snapshot, had the same hole, and an AMI recorded
against a snapshot that never completes is one every later launch fails on.

The change

The rule goes into ec2CheckMappingSnapshot, so the launch path (and CreateFleet through it)
and RegisterImage inherit it from one place: IncorrectState / 400, naming the snapshot and
the state it is in so a caller can decide to wait. Ordering inside the validator is existence →
state → size, which is CreateVolume's order.

Everything except completed is refused, not just pending and error. AWS's
snapshot-states table covers all four in its own words: a pending snapshot "can't be used
while it is in the pending state", an error one "can't be used", a recoverable one "must
first [be recovered] from the Recycle Bin", and a recovering one becomes "ready for use" only
after it reaches completed.

The state comes from an observation, not the record. EC2Snapshot.State is "completed" for
every snapshot substrate writes, so if snap.State != "completed" would be dead code — the
observable state lives only behind the seed accessors.

And it peeks rather than observes. The countdown advances in exactly one place, so an
observing read would spend one observation per mapping per request: pendingObservations: 2
would mean two polls in a test that only describes and fewer in one that also attempts a
three-mapping launch, and a consumer's wait loop — which alternates a launch attempt and a
describe — would race its own budget. A test pins this by asserting a seeded one-observation
budget survives two three-mapping launch attempts; swapping peek for observe reddens exactly
that test and nothing else.

ec2CheckMappingSnapshot is deliberately pure — its doc comment records that it takes a
resolver and no StateManager. That is preserved by injecting a second
func(EC2Snapshot) (ec2SnapshotObservation, error) rather than promoting the function to a
method. A nil observer skips the state rule, which is how the exemption below is expressed.

The launch-template exemption

CreateLaunchTemplate and CreateLaunchTemplateVersion pass nil, with the reason recorded at
both call sites: a template records a mapping rather than consuming one, both report mapping
problems through AWS's documented warning member and refuse nothing (#693), and a snapshot
pending when a template is written may legitimately be completed by the time the template is
used — so refusing at write time would forbid an ordering AWS permits. A launch from such a
template is refused, so the rule is not escapable by routing a mapping through one; a test
asserts both halves.

Provenance

Substrate's reading. API_RegisterImage.html's Errors section contains only the pointer to the
errors common to all actions — it is empty — and the page says nothing about a snapshot's
required state. The rule is AWS's snapshot-states rule, quoted above; the code is
substrate's choice from EC2's client-error table, the one CreateVolume already answers with,
which is the same provenance shape that carries.

DeleteSnapshot is not analogous, and the issue's premise that #715 settled it is inverted:
#715 records the opposite, that AWS permits deleting a snapshot still in progress. It still
refuses nothing on state grounds — the live run below shows a seeded pending snapshot refused
only with InvalidSnapshot.InUse, because an AMI referenced it, a rule that has nothing to do
with the state.

A dedicated test covers the unreadable-state branch with a state double that fails Gets only in
the control-plane namespace, which is what keeps the refusal from collapsing into
InvalidSnapshot.NotFound.

An unreadable seed answers InternalError / 500 rather than treating the snapshot as usable,
which is deliberately the opposite of ec2SnapshotResolver's absent-on-error rule and is
documented as such: silently permitting a mapping because the seed could not be read would make
the rule's absence indistinguishable from its satisfaction.

Verification

Full gate green: gofmt, go build, go vet, golangci-lint (0 issues), make test (race),
make coverage (emulator 83.3%, total 82.6%), make docs-reference{,-check}, make docs-versions, npm --prefix docs run build with no dangling anchors, and go vet/go test
in test/e2e.

Fail-before: nulling the injected observer at both call sites reddens all seven new tests.
Mutation: swapping peekSnapshotStatus for observeSnapshotStatus reddens only the poll-budget
test.

Live SDK run against a built binary, which is how the issue was found, all twelve parts
verified — the AMI resolved through the SSM parameter generated IaC uses, the unseeded launch
and RegisterImage succeeding, both refused once seeded with the identical message,
CreateVolume agreeing on the code, three mappings plus a RegisterImage plus a CreateVolume
leaving the seeded three-poll budget exactly intact (pending 0% → 33% → 66% → completed 100%),
both template operations accepting the pending mapping while a launch from the template is
refused, and the same template launching both volumes once the seed is cleared.

Compatibility

A launch or a RegisterImage whose mapping names a snapshot under a seeded progression now
fails until that snapshot reports completed. Only a seeded snapshot is affected: every snapshot
substrate writes is born completed, so an unseeded mapping behaves exactly as before.

…732)

CreateVolume refused a snapshot that was not usable yet from #715 onward, but a
launch declaring the same snapshot in a BlockDeviceMapping materialized a volume
from it and reported success. One rule, two doors, disagreeing: a consumer
restoring through a launch — which is what CDK's and Terraform's snapshot-backed
volumes do — never reached the error branch it had written. RegisterImage, whose
own second AWS example registers an AMI from a root-device snapshot, had the same
hole.

The rule now lives in ec2CheckMappingSnapshot, so the launch path (and CreateFleet
through it) and RegisterImage inherit it from one place: IncorrectState/400 naming
the snapshot and its state, for everything except completed.

The state has to come from an observation rather than the record: EC2Snapshot.State
is "completed" for every snapshot substrate writes, so a record-reading check would
be dead code. It also has to peek rather than observe — the countdown advances in
exactly one place, so an observing read would spend one observation per mapping per
request. The observer is injected as a second function parameter, keeping the
validator free of a StateManager as its doc comment promises, and a nil observer
skips the state rule for the two CreateLaunchTemplate paths.
@codecov

codecov Bot commented Aug 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…an permitting it

Covers the InternalError branch of the state rule, which is the one path a
seeded progression cannot reach: it needs the control-plane namespace to fail a
Get while the snapshot record itself still resolves, so the double fails only
that namespace and only once armed. A store failing every Get would stop at the
record and answer InvalidSnapshot.NotFound — the collapse the assertion rules
out.
@scttfrdmn
scttfrdmn merged commit 7802dab into main Aug 22, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the fix/732-mapping-snapshot-state branch August 22, 2026 19:13
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: a pending snapshot in a block device mapping is accepted by RegisterImage and by a launch

1 participant