Repository navigation
fix(ec2): TerminateInstances honours termination protection (#489) - #504
Merged
Merged
Conversation
#473 made disableApiTermination observable but nothing acted on it, which is the sharpest case of this tracker's recurring shape: a consumer's test asserting "my teardown does not destroy the protected instance" passed against a code path that ignored protection entirely, so the assertion could not fail. A protected instance is now refused with OperationNotPermitted, HTTP 400, and stays running. A request naming both protected and unprotected instances fails per Availability Zone, not per request and not per instance — neither of the two answers #489 guessed at. Quoting TerminateInstances: "The specified instances that are in the same Availability Zone as the protected instance are not terminated. The specified instances that are in different Availability Zones, where no other specified instances are protected, are successfully terminated." So an unprotected instance sharing a zone with a protected one survives, while an unprotected instance in another zone does not, and the request reports the error after those terminations are persisted. The zone was not modelled at all, so the rule was neither implementable nor observable. EC2Instance now carries an AvailabilityZone, resolved at launch from Placement.AvailabilityZone, else the named subnet's zone, else the region's first zone; a fleet pool's Overrides.N.AvailabilityZone reaches the launch; RunInstances and DescribeInstances report <placement>; and DescribeInstances accepts an availability-zone filter. DeleteFleets --terminate-instances routes through the same handler, so a protected fleet instance survives its fleet's deletion while an unprotected sibling in another zone does not. DeleteFleets propagates the error rather than folding it into unsuccessfulFleetDeletionSet: DeleteFleetError's documented codes are exactly fleetIdDoesNotExist, fleetIdMalformed, fleetNotInDeletableState and unexpectedError, so folding it in would mean answering unexpectedError and losing the code a caller acts on. Provenance is split. The code is documented — EC2's client-error table names this case first among OperationNotPermitted's examples. The message text is substrate's own: #489's wording is from memory and no capture corroborates it, so per docs/fidelity.md it is not dressed up as observed. Closes #489
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Closed
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
TerminateInstancesnow honours termination protection, and does so per Availability Zone.Closes #489
Why it mattered
#473 made
disableApiTerminationobservable but nothing acted on it. That is the sharpest case of this tracker's recurring shape: a consumer's test asserting "my teardown does not destroy the protected instance" passed against a code path that ignored protection entirely — so the assertion could not fail. The test was green and verified nothing.The AZ rule — neither of #489's two guesses
#489 asked whether a mixed request refuses whole or terminates the unprotected instances, and noted that getting it backwards is the same defect in the other direction. The reference's answer is a third thing:
For the reference's own four-instance example — A and B unprotected in
us-east-1a, C protected and D unprotected inus-east-1b, all named in one request:us-east-1aus-east-1aus-east-1brunningus-east-1brunning— shares C's zoneThe request reports
OperationNotPermittednaming C, after theus-east-1aterminations are persisted. That ordering is the behaviour, not an implementation detail.The zone had to be modelled first
EC2Instancecarried noAvailabilityZoneat all, so the rule was neither implementable nor observable. Added and resolved at launch:Placement.AvailabilityZone→ the named subnet's zone → the region's first zone (<region>a, matching "EC2 automatically selects an Availability Zone for you"). A fleet pool'sOverrides.N.AvailabilityZonenow reaches the launch, so a fleet spread across zones no longer looks single-zone.RunInstancesandDescribeInstancesreport<placement><availabilityZone>, andDescribeInstancesaccepts anavailability-zonefilter — that is the reference's spelling, notplacement.availability-zone.AvailabilityZoneIdis not modelled.An instance replayed from an event log predating the field reads back with an empty zone, grouping all such instances together — the conservative reading, being what a single-zone account looks like.
The fleet path
DeleteFleets --terminate-instancessynthesizes aTerminateInstancesthrough the same handler, so a protected fleet instance survives its fleet's deletion while an unprotected sibling in another zone does not.DeleteFleetspropagates the error rather than folding it intounsuccessfulFleetDeletionSet:DeleteFleetError's documented codes are exactlyfleetIdDoesNotExist,fleetIdMalformed,fleetNotInDeletableStateandunexpectedError, so folding it in would mean answeringunexpectedErrorand losing the code a caller acts on.Provenance
Split, and labelled as such in the code. The code is documented — EC2's client-error table lists
OperationNotPermittedand names this case first among its examples: "you might be trying to terminate an instance that has termination protection enabled". The message text is substrate's own: #489 supplied a remembered console wording that no capture corroborates, so perdocs/fidelity.mdit is not dressed up as observed. It interpolates the instance ID and names the attribute to clear, which is what a caller acts on.Out of scope, deliberately
A terminated instance still reports code
48terminatedimmediately, where real EC2 reports code32shutting-downfirst. Pre-existing, unrelated to protection, and widening this into a lifecycle change would have put #489's gate at risk. Filed separately.Tests
Eleven new tests. The gates:
running; clearing protection then terminating succeeds.DeleteFleets --terminate-instances; the unprotected sibling in another zone does not.Placement.AvailabilityZone— without this it would collapse to whole-request refusal for the common case.Mutation-tested: 25 mutations, 25 caught, 0 survivors. Including both of #489's wrong answers (refuse the whole request; terminate per-instance), ignoring protection entirely, terminating the blocked zone while still reporting the error, dropping the zone from either response builder, losing a fleet pool's zone override,
DeleteFleetsswallowing the refusal, andjson:"-"on the new field.Verification
From the repo root:
gofmt -l .,go build ./...,go vet ./...,golangci-lint run ./...(0 issues),make test(race),make docs-reference docs-reference-check docs-versions,npm --prefix docs run build,go test -C test/e2e ./...— all clean.End-to-end through the AWS CLI against
substrate server:Docs
docs/services.md— the "recorded and reported but not acted on" caveat is gone, replaced by a new §"Termination protection is honoured, one Availability Zone at a time" carrying the quote, the four-instance table, the zone-resolution order, the filter name, the fleet behaviour, the event-log note and theshutting-downdivergence. Operations table rows updated forRunInstances,DescribeInstances,TerminateInstancesandDeleteFleets.