TL;DR

  • Waking a stopped EC2 host took 77 seconds before work started.
  • Two waits dominated: the admission gate closed by the previous stop reopening, and a process restart after boot authentication failed.
  • The decision to reopen admission moved from a VM lifecycle worker outside the ALB to the host itself. The host only asks the control plane whether the current boot is different from the one before the stop.
  • The ALB target group now uses ip targets instead of instance targets. Registration survives the stop, so a wake does not wait for re-registration.

48 of 77 seconds went to re-authentication and an expiring admission lease

Shared EC2 hosts stop automatically when there is no demand and wake when work arrives. Before stopping, the host daemon closes admission for new work and writes that state to disk.

On staging I queued work, woke a stopped host, and measured each segment. From the recorded wake request to the start of work took 77.28 seconds. Two segments stood out.

  • 10.13 s: the host agent failed authentication right after boot, the process exited, and systemd restarted it.
  • 37.93 s: from finishing workspace folder registration until the admission lease left by the previous stop expired.

Boot authentication failures took the whole process down

At startup the host agent proves its identity with the EC2 role and gets a short-lived token from the control plane. When that failed, the process exited and paid systemd’s restart delay.

The failures were transient. Right after boot the VM clock can be off, and a token whose iat looked a few seconds in the future was rejected. The control plane could also answer 403 because it had not yet recorded the start.

Two changes fixed this.

  • iat may be up to 30 seconds in the future. Expiry is not extended.
  • Transient failures are retried in-process, starting at 200 ms, doubling up to 1 s, for at most 10 seconds in total. The control plane re-checks authorization on every attempt.
if value.IssuedAt <= 0 || value.IssuedAt > now+30 || value.ExpiresAt <= now ||
	value.ExpiresAt-value.IssuedAt > 900 {
	return claims{}, ErrInvalidToken
}

RFC 7519 also allows a small leeway for clock skew when checking exp and nbf.

Admission closed by the last stop stays closed after boot

This is the main problem. Admission closed before the stop is persisted, so it is still closed after boot. That is intentional. A host that merely restarted in the middle of a stop must not start taking work on its own.

Previously the only ways out were the lifecycle worker telling the host through the ALB that it could reopen, or the lease expiring.

The first fix had the control plane record the previous boot ID in the database before calling StartInstances. A wake completes only when an authenticated boot with a different ID appears and admission resumes successfully. Same boot ID means the pre-stop process, which must not reopen.

That brought 77 seconds down to about 42. Most of what remained was the ALB. Before the worker’s request could reach the host, the target sat in Elb.RegistrationInProgress for about 32 seconds. Inside the VM the host had been answering HTTP 401 for about 22 seconds by then.

The host reopens admission without waiting for the worker

So the host now resumes admission itself. Once the daemon has connected and its heartbeat is fresh, the host agent asks the control plane. The control plane permits it only when all of these hold:

  • the request is authenticated with the current boot’s token
  • the current boot differs from the recorded previous boot
  • the desired state is still RUNNING
  • a heartbeat arrived after this boot started

A stop request, a disabled workspace or a different workspace is refused. The lookup is read-only and changes no state.

With permission, the host reopens admission through the same local gate the worker uses. If the gate answers 409, a stop or deployment is in progress and the host does nothing. This runs once per boot with a 30-second budget.

When admission reopens, the daemon’s claim loop is notified at once. Previously it noticed on its next 5-second poll.

ALB instance targets re-register on every start

Claiming work no longer goes through the ALB. Browser and management traffic still does.

The ALB documentation lists a stopped instance target as unused (Target.InvalidState). After a start it comes back through initial (Elb.RegistrationInProgress). Shortening the health-check interval to 5 seconds does not shorten that registration step.

The target group now uses target_type = "ip" and registers the host’s primary private IPv4 address. That address does not change across stop and start. The same IP stays registered while the host is stopped, so after boot it only has to pass health checks.

resource "aws_lb_target_group" "management" {
  target_type = "ip" # instance targets re-register on every start
  lifecycle { create_before_destroy = true }
  health_check {
    path    = "/v1/readiness"
    matcher = "200,401"
  }
}

Changing the target type replaces the target group. create_before_destroy creates the new group first and then moves the listener reference.

The matcher accepts 401 because the ALB has no control-plane credential. A 401 only proves HTTP reachability. The control plane checks daemon readiness separately, with authentication.

Work starts about 18 seconds after the wake request

Finally, I also trimmed the daemon’s own startup work (loading records, preparing the agent). In three runs from a fully stopped host with work queued, the time from receiving the wake request to the start of work was 18.84, 18.52 and 17.88 seconds.

The admission gate that made stopping safe had become the wait on every start. Reopening it needs only one fact: the current boot is not the pre-stop boot. Once the control plane confirms that, the host can reopen admission by itself.