Repository navigation
feat(aws): build instances shut themselves down when their lease runs out - #34
Merged
Merged
Conversation
… out Each build instance runs a watchdog, started at boot from its user data, that shuts it down once the time in its LeaseUntil tag has passed, read from the instance metadata service. Whoever launched the instance renews the tag every minute while it is alive, up to the run's deadline (AWS_SHUTDOWN_TIME), so an instance outlives its launcher by at most five minutes however the launcher ended. Instances terminate rather than stop when they shut down. This replaces the shutdown timer sent over SSM once the instance was up, which an instance whose launcher died before then never got, and which kept an instance whose launcher died later running until the full AWS_SHUTDOWN_TIME. It also replaces terminating instances launched from a build instance through a ParentInstance tag: their launcher is on that instance, so their lease lapses with it.
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.
Every build instance now has a lease. It shuts itself down, and so terminates, once the lease runs out, and whoever launched it renews the lease while it runs. So an instance outlives its launcher by at most 5 minutes, however the launcher ended: cancelled, SIGKILLed, or on a runner that crashed or lost its connection.
How it works
On the instance (
internal/source_instance_lease)LeaseUntiltag.Restart=always, and its script is in/run, so it is never baked into an image.shutdown -h now. The launch template now setsInstanceInitiatedShutdownBehavior: terminate, so that terminates the instance and its volume./run, so a restarted watchdog doesn't extend it.On the launcher (
internal/aws_instance_lease)bootstrap_ec2starts it in the background before requesting the instance.LeaseUntilto 5 minutes from now, every minute.AWS_SHUTDOWN_TIMEfrom the start. A run that goes on too long therefore ends the same way as one whose launcher died.bootstrap_ec2exiting. Cleanup and the spot-eviction retry also stop it directly.aws/ami_update.shrenews its instance's lease the same way. It keeps shutdown as stop (AWS_SHUTDOWN_TERMINATES=0), ascreate-imagereboots the instance.What it replaces
shutdown -h +AWS_SHUTDOWN_TIMEsent over SSM once the instance was up:ParentInstancetag, which terminated instances launched from a build instance (the benchmark box) along with it. Their launcher isbootstrap_ec2on that instance, so their lease now lapses when it goes.This also supersedes #28's boot timer and claim. Its terminate-on-shutdown is included here. Its
CI3LaunchIdlookup, which finds an instance whose id was lost mid-request, is a separate change.Rollout
aztec-labs-eng/iac#83 must be applied first. It lets the launcher roles set the
LeaseUntiltag. Without it every renewal is refused, and every build instance shuts down 10 minutes after boot. The README now lists this under what ci3 expects from a consuming repository.Testing
New
tests/instance_lease_test:aws_request_instancewith a stub aws:AWS_SHUTDOWN_TERMINATES=0keeps the watchdog and leaves shutdown as stop.NO_TERMINATE=1gets none of them.I broke the watchdog's restart handling and the renewer's deadline cap on purpose, and the test caught both.
tests/cancel_testnow also checks that the renewer is stopped. All tests pass locally on bash 5.2 and 5.3.Still to do before merge: a live check on real AWS.
LeaseUntilmoving every minute.bootstrap_ec2mid-build; the instance should terminate itself within about 5 minutes.