Skip to content

Let a Spot replacement terminate itself instead of being left stopped (#1080) - #1081

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/1080-spot-replacement-self-terminate
Oct 17, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/1080-spot-replacement-self-terminate

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Closes #1080

Problem (observed live on v0.131.0). spored on a replacement EC2 launched from a leaked persistent Spot request (#1008) cancelled the request (good), then shut the OS down. spawn never sets InstanceInitiatedShutdownBehavior, so EC2's default stop applied, and the replacement was left stopped, untagged, its request cancelled:

Options considered:

  • InstanceInitiatedShutdownBehavior=terminate: rejected. It applies to the original instance too. A user's sudo poweroff would then terminate it with its persistent request still open, and EC2 would launch a replacement. That creates the leak.
  • Tag itself, then terminate: impossible. The instance role's CreateTags is scoped to spawn:managed=true, which the replacement lacks.
  • Let an instance terminate only itself: chosen. It's exactly the property the tag scope approximates, with no dependence on tags.

Fix

  • IAM: every spored policy (static shared role, dynamic baseline, inline --iam-policy) gains one statement, TerminateOnlySelf: ec2:TerminateInstances, Resource *, Condition StringEquals aws:ARN = ${ec2:SourceInstanceARN}. The tag-scoped grant is unchanged.
  • spotrequest.RetireReplacement: cancel → terminate self via the API → only if that's refused, the OS shutdown. It returns RetiredTerminated or RetiredShutdown; the latter's error says "STOPPED and billing its EBS volume". Cancel-first is preserved: a failed cancel, or an empty instance ID, does nothing at all.
  • spored: it logs which outcome happened. On the stopped fallback it says so and prints aws ec2 terminate-instances --region R --instance-ids I, rather than implying the instance retired. Notification text updated.

Proven on real AWS before and after writing it (#1069 is why a simulator pass isn't evidence):

  1. A throwaway role carrying only the statement: PutRolePolicy accepted and stored the policy variable. From an untagged instance, dry-runs showed terminating another instance → UnauthorizedOperation, itself → DryRunOperation, itself + another → UnauthorizedOperation. A real self-terminate succeeded and the other instance was untouched. Probe role, instance profile and instances deleted.
  2. This branch's CLI launched once against the real dev account, so spored-instance-role/spored-policy now stores TerminateOnlySelf exactly. Then two untagged raw instances with spored-instance-profile (a replacement's exact situation) repeated all four results. Leak check afterwards: 0 instances, volumes, open/active Spot requests, DNS records.

Not yet proven end to end: the new spored retiring a real replacement. spored ships only via a release, so that's the post-release check: rerun the orphan test and expect the replacement terminated, not stopped.

Gates. 9 reverts, each kept compiling; every one fails:

TestTheSelfTerminateGrantReachesIAM reads back what each writer stored. go vet, staticcheck (GOOS=linux), the hermetic suite, nested modules, pkg/reaperiam and semgrep all pass. pkg/spotrequest isn't public API; the API snapshot is unchanged.

@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 68.62745% with 16 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/spored/main.go 0.00% 6 Missing ⚠️
cmd/spored/spot_replacement.go 71.42% 4 Missing ⚠️
pkg/aws/iam.go 88.23% 1 Missing and 1 partial ⚠️
pkg/provider/ec2.go 0.00% 2 Missing ⚠️
pkg/spotrequest/replacement.go 83.33% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 90bba56 into main Oct 17, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/1080-spot-replacement-self-terminate branch October 17, 2026 17:17
@scttfrdmn scttfrdmn mentioned this pull request Oct 17, 2026
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.

Spot replacement self-retirement (#1008) STOPS the replacement instead of ending it: untagged, request cancelled, EBS bills forever

1 participant