Skip to content

feat(aws): build instances shut themselves down when their lease runs out - #34

Merged
charlielye merged 1 commit into
mainfrom
cl/instance-lease
Nov 25, 2026
Merged

charlielye merged 1 commit into
mainfrom
cl/instance-lease

Conversation

@charlielye

Copy link
Copy Markdown
Contributor

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)

  • The lease is the time in the instance's LeaseUntil tag.
  • User data, run once by cloud-init at boot, starts a watchdog. It runs as a transient systemd service with Restart=always, and its script is in /run, so it is never baked into an image.
  • Every 30 seconds it reads the tag through the instance metadata service, which needs no credentials. The launch template enables tags in instance metadata.
  • Once the lease has passed, it runs shutdown -h now. The launch template now sets InstanceInitiatedShutdownBehavior: terminate, so that terminates the instance and its volume.
  • Missing data means shut down:
    • It starts with a lease of 10 minutes from boot, enough for the first renewal to land.
    • After that it keeps the latest lease it has read. If it can't read the tag, the last lease it saw still runs out.
    • It keeps the lease in /run, so a restarted watchdog doesn't extend it.

On the launcher (internal/aws_instance_lease)

  • bootstrap_ec2 starts it in the background before requesting the instance.
  • Once the instance id appears, it sets LeaseUntil to 5 minutes from now, every minute.
  • It never sets it past the run's deadline, AWS_SHUTDOWN_TIME from the start. A run that goes on too long therefore ends the same way as one whose launcher died.
  • It exits within a minute of bootstrap_ec2 exiting. Cleanup and the spot-eviction retry also stop it directly.
  • aws/ami_update.sh renews its instance's lease the same way. It keeps shutdown as stop (AWS_SHUTDOWN_TERMINATES=0), as create-image reboots the instance.

What it replaces

  • The shutdown -h +AWS_SHUTDOWN_TIME sent over SSM once the instance was up:
    • An instance whose launcher died before then never got it.
    • One whose launcher died later ran until the full 75 to 180 minutes.
    • A shut-down instance was stopped, not terminated, so its volume went on costing money.
  • The ParentInstance tag, which terminated instances launched from a build instance (the benchmark box) along with it. Their launcher is bootstrap_ec2 on 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 CI3LaunchId lookup, 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 LeaseUntil tag. 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:

  • The watchdog, against a stub metadata service:
    • It shuts down when the lease runs out, not before.
    • A renewal keeps it up.
    • With no tag, it shuts down when the initial lease runs out.
    • With the metadata service down, the last lease it read still runs out.
    • A tag that isn't a time is ignored.
    • A restart keeps the lease it had.
  • The renewer, against a stub aws:
    • It waits for the instance id.
    • It sets the lease to now plus 5 minutes.
    • It caps the lease at the deadline.
    • It stops when its owner exits.
  • The launch template, built by aws_request_instance with a stub aws:
    • A build instance gets the watchdog, metadata tags, and terminate-on-shutdown.
    • AWS_SHUTDOWN_TERMINATES=0 keeps the watchdog and leaves shutdown as stop.
    • NO_TERMINATE=1 gets 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_test now 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.

  • Renewal: a build's instance shows LeaseUntil moving every minute.
  • Launcher killed: SIGKILL bootstrap_ec2 mid-build; the instance should terminate itself within about 5 minutes.
  • Launcher never renews: an instance whose launcher never renews should terminate itself 10 minutes after boot. This also confirms cloud-init runs the user data on the build AMI, and how quickly tag changes show up in instance metadata.

… 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.
@charlielye
charlielye marked this pull request as ready for review November 25, 2026 14:13
@charlielye
charlielye merged commit 89c5ce1 into main Nov 25, 2026
1 check passed
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.

1 participant