Skip to content

fix(ec2): TerminateInstances honours termination protection (#489) - #504

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/489-termination-protection
Aug 2, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/489-termination-protection

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

What

TerminateInstances now honours termination protection, and does so per Availability Zone.

Closes #489

Why it mattered

#473 made disableApiTermination observable 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:

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.

For the reference's own four-instance example — A and B unprotected in us-east-1a, C protected and D unprotected in us-east-1b, all named in one request:

Instance Zone Protected Outcome
A us-east-1a no terminated
B us-east-1a no terminated
C us-east-1b yes still running
D us-east-1b no still running — shares C's zone

The request reports OperationNotPermitted naming C, after the us-east-1a terminations are persisted. That ordering is the behaviour, not an implementation detail.

The zone had to be modelled first

EC2Instance carried no AvailabilityZone at 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's Overrides.N.AvailabilityZone now reaches the launch, so a fleet spread across zones no longer looks single-zone. RunInstances and DescribeInstances report <placement><availabilityZone>, and DescribeInstances accepts an availability-zone filter — that is the reference's spelling, not placement.availability-zone. AvailabilityZoneId is 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-instances synthesizes a TerminateInstances 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

Split, and labelled as such in the code. The code is documented — EC2's client-error table lists OperationNotPermitted and 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 per docs/fidelity.md it 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 48 terminated immediately, where real EC2 reports code 32 shutting-down first. 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:

  • The AZ table — fails under both behaviours Termination protection is recorded and reported but TerminateInstances does not honour it #489 guessed at.
  • A protected instance stays running; clearing protection then terminating succeeds.
  • A protected fleet instance survives DeleteFleets --terminate-instances; the unprotected sibling in another zone does not.
  • A bad ID terminates nothing; an already-terminated instance still succeeds (the operation is idempotent and the protection check must not change that).
  • Zone from a subnet, so the rule is zone-scoped for callers who never pass Placement.AvailabilityZone — without this it would collapse to whole-request refusal for the common case.
  • Every instance carries a zone, whatever the launch named; an empty zone would let a whole-request refusal masquerade as the zone rule.

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, DeleteFleets swallowing the refusal, and json:"-" 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:

$ aws ec2 terminate-instances --instance-ids $A $B      # A protected in 1b, B in 1a
An error occurred (OperationNotPermitted) ...: The instance 'i-f41e...' may not be
terminated. Modify its 'disableApiTermination' instance attribute and try again.

$ aws ec2 describe-instances --instance-ids $A $B
i-2c92c2fd60987400   us-east-1a   terminated
i-f41e4d3745ed8a40   us-east-1b   running

$ aws ec2 describe-instances --filters Name=availability-zone,Values=us-east-1b
i-f41e4d3745ed8a40

$ aws ec2 modify-instance-attribute --instance-id $A --no-disable-api-termination
$ aws ec2 terminate-instances --instance-ids $A
i-f41e4d3745ed8a40   terminated

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 the shutting-down divergence. Operations table rows updated for RunInstances, DescribeInstances, TerminateInstances and DeleteFleets.

#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

codecov Bot commented Aug 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.65079% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/ec2_plugin.go 91.11% 2 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit b502c5f into main Aug 2, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the feat/489-termination-protection branch August 2, 2026 21:58
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.

Termination protection is recorded and reported but TerminateInstances does not honour it

1 participant