Skip to content

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

Closed

Description

@scttfrdmn

Summary

Two problems make it hard to bake a clean AMI from an instance that ran --command, using spawn ami create --reboot. Seen with spawn v0.128.2 on Rocky 9.8.

  1. Completion files are written after the command exits. The bootstrap writes /tmp/SPAWN_EXITCODE and /tmp/SPAWN_COMPLETE after --command returns (pkg/launcher/bootstrap.go:766, :790; and :754-755 on the failure path). So a command can't clean them up itself before imaging. On distros where /tmp is on the root volume rather than a tmpfs (Rocky 9 Base AMIs), they get baked into the AMI. A later launch then starts with a stale SPAWN_COMPLETE.
  2. The CreateImage reboot re-runs the bootstrap and the command. A command that prepares an image typically ends with cloud-init clean, so that instances launched from the AMI boot fresh. spawn ami create --reboot then reboots the source instance, and cloud-init treats that boot as a first boot. The bootstrap and --command run again on the source instance while the snapshot is being taken. The snapshot itself is unaffected, but the re-run overwrites whatever the command uploads, such as results and markers.

Repro (sketch)

spawn launch img-src --ami <rocky9> --ttl 2h --cost-limit 1 \
  --command 'set +e; echo build; sudo rm -rf /etc/spawn; sudo cloud-init clean --logs' -y
# after the command completes:
spawn ami create img-src --name img-test --reboot --wait
# 1) launch from img-test without --command: /tmp/SPAWN_COMPLETE already exists at boot
# 2) /var/log/spawn-command.log on img-src shows the command ran a second time after the reboot

Suggestions

  • Have spawn ami create remove /tmp/SPAWN_* before it snapshots. It could run that over SSM or SSH, or the bootstrap could register a shutdown hook.
  • Or document an "image-prep" mode, e.g. --on-complete image, where spawn does the cleanup itself.
  • Have the bootstrap skip --command when a marker such as /var/lib/spawn/command-ran-<instance-id> exists, so a reboot of the same instance doesn't re-run it.

Downstream workaround

spore-host/sclero has a marked workaround: a transient systemd unit whose ExecStop removes /tmp/SPAWN_* at the CreateImage shutdown, plus a guard file keyed on the run ID that stops a re-run.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions