Skip to content

fix(ami): clean --command run state before CreateImage; run --command once per instance (#1057) - #1240

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/ami-clean-1057
Oct 20, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/ami-clean-1057

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #1057

What changes

spawn ami create / spawn create-ami clean the instance's run state before CreateImage.
Before calling CreateImage, spawn runs launcher.ImageStateCleanupScript on the
source instance over SSM (AWS-RunShellScript, as root). The script removes:

  • the completion file: the instance's spawn:completion-file tag, otherwise the default, and /tmp/SPAWN_COMPLETE in both cases;
  • every /tmp/SPAWN_* file, which includes the exit code;
  • /tmp/spawn-command.sh and /etc/spawn/command. The embedded command was also
    being baked in, so an instance launched from the image with no --command
    would have run the old command;
  • /etc/profile.d/spawn-params.sh and /run/spawn/required-gates;
  • command-ran-* markers inherited from a parent image. The source instance's own
    marker is kept.

The script also checks that every one of those paths is gone, and prints an ok
line. It refuses with exit 3 while --command is still running, meaning the marker
exists but there is no exit code and no completion file yet.

Refusal. spawn does not create the image if any of these happen:

  • SSM errors or returns no result;
  • the script fails or does not print the ok line;
  • the command is still running;
  • the instance is stopped, so SSM cannot reach it.

The error names --force, which images anyway and prints a warning. Windows
instances are skipped because the Windows bootstrap runs no --command (#1022).
Behaviour change: imaging a stopped Linux instance now needs --force.

The bootstrap runs --command once per instance. Inside the backgrounded
subshell, after the Spot-replacement check, it now:

  • checks /var/lib/spawn/command-ran-$INSTANCE_ID and, if the file exists, skips the command and writes no completion file;
  • otherwise writes that file, then runs the command.

Two effects:

  • After a cloud-init clean, the CreateImage reboot no longer re-runs the command on the source instance.
  • An instance launched from the image has a different ID, so it still runs its own --command.

To re-run a command on purpose, delete the marker. The log message says this.

Gates, each watched failing against a revert that compiles

  • TestImageStateCleanupRemovesEveryDiscoveredRunStatePath discovers the run-state set from the rendered bootstrap rather than from a list. It takes every file written under /tmp, the completion file, and every file the bootstrap both writes and tests with [ -s|-f|-e ]. It fails if discovery finds nothing.
    • Today it finds: /data/out dir/done.json, /etc/profile.d/spawn-params.sh, /etc/spawn/command, /run/spawn/required-gates, /tmp/SPAWN_EXITCODE, /tmp/spawn-command.sh and /var/lib/spawn/command-ran-$INSTANCE_ID.
    • It creates each file in a scratch root and runs the real script under sh. Then it asserts that everything is removed except this instance's marker and the command log.
    • It failed when I removed /etc/spawn/command from the script, and when I removed the keep-own-marker line.
  • TestCommandRunsOncePerInstanceID runs the real --command block three times in the existing bash sandbox: same instance ID, same ID again, then a new ID. The workload ran once, was skipped, then ran again. With the marker check replaced by if false, the workload ran twice on the same instance.
  • TestAMICreate_CleansRunStateOverSSMBeforeCreateImage uses a fake SSM runner and a fake CreateImage that record call order. It asserts SSM runs before CreateImage, and that the script is ImageStateCleanupScript(id, tag completion file).
  • TestAMICreate_RefusesWhenTheCleanFails covers six failure modes. Each one must not call CreateImage, and --force must create the image. It failed when I disabled the refusal.
  • TestRunCreateAMIGoesThroughTheCleaningImager is an AST check that runCreateAMI calls createFromInstance and never CreateAMI directly. It failed when I restored the direct call.

Checks

  • make check passes every step except check-build-tags, which fails identically on unmodified main on this machine.
  • lint: 0 issues. semgrep: clean.
  • api-snapshot is regenerated, with additions only.
  • gen-docs is committed (help text changed), and check-docs is clean.

Not done here

  • The run state above is limited to state that records or signals a --command run. Other per-instance state also gets baked in: /etc/spawn/plugins.json, which spored loads at every startup, /etc/profile.d/job-array.sh and the peers files. That is filed separately (see the follow-up issue linked in a comment).
  • No live AWS run. I have not checked on a real Rocky 9 instance that the SSM clean, CreateImage and the reboot behave as tested.

@scttfrdmn

Copy link
Copy Markdown
Contributor Author

Follow-up for the per-instance state this PR leaves out of scope: #1241 (Refs, not closing).

@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.09524% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/create_ami_clean.go 81.57% 6 Missing and 1 partial ⚠️
cmd/create_ami.go 25.00% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

… once per instance (#1057)

spawn ami create runs launcher.ImageStateCleanupScript over SSM before
CreateImage, removing the completion file (tag or default), /tmp/SPAWN_*,
the rendered and embedded command, the sweep env and the gate list; it
refuses to image (unless --force) when the clean cannot run or fails.

The bootstrap writes /var/lib/spawn/command-ran-<instance-id> before
starting --command and skips the command when it exists, so the
CreateImage reboot after a cloud-init clean does not re-run it, while an
instance launched from the image runs its own.
@scttfrdmn
scttfrdmn merged commit 882b244 into main Oct 20, 2026
9 checks passed
@scttfrdmn
scttfrdmn deleted the fix/ami-clean-1057 branch October 20, 2026 18:01
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.

ami create from a --command instance: SPAWN_* files written after the command get baked in, and the CreateImage reboot re-runs the command

1 participant