run lifecycle hook and unmount it from the terminate hook.
Prerequisites
- An Archil disk in an AWS region that also runs Lambda MicroVMs:
aws-us-east-1,aws-us-west-2,aws-eu-west-1, oraws-us-east-2(in preview). - AWS CLI 2.35.10 or later, which adds the
aws lambda-microvmscommands, andjq. - An S3 bucket in the same region to hold the image’s build artifact.
disk CLI. The output includes the disk ID and a one-time mount token, which grants access to this disk only:
Build the image
The image needs thearchil client and a process that serves the MicroVM’s lifecycle hooks. Lambda MicroVMs run on ARM64, and the installer picks the matching build.
Dockerfile
server.py serves the hooks on port 9000 and a small app on port 8080. The run hook mounts the disk using the disk ID, region, and token passed when the MicroVM starts. The terminate hook unmounts it. The app appends a line to a file on the disk for every request, and refuses requests while the disk isn’t mounted, so it never writes to the MicroVM’s local disk by mistake:
server.py
lambda.amazonaws.com:
--additional-os-capabilities ALL gives the MicroVM the /dev/fuse device and the capabilities that archil mount needs. The --hooks option enables the run and terminate hooks on port 9000, plus the ready hook that tells Lambda when the image is ready to snapshot. Set the hook timeouts explicitly: the defaults are only a few seconds, and Lambda terminates a MicroVM whose run hook doesn’t finish in time:
SUCCESSFUL before you start a MicroVM:
Start a MicroVM with the disk
Pass the disk ID, region, and token in--run-hook-payload. Lambda delivers the payload to the run hook as each MicroVM starts:
PENDING until its run hook returns, so wait for it to reach RUNNING. If the mount fails, the hook returns an error and Lambda terminates the MicroVM; get-microvm shows why in stateReason, and your app’s output is in CloudWatch Logs under /aws/lambda-microvms/archil-microvm.
terminate hook unmounts the disk, which sends every buffered write to the disk and releases it:
run hook. Grant the execution role secretsmanager:GetSecretValue on that secret.
How the disk behaves in a MicroVM
Mount in therun hook, not while the image builds. Lambda restores every MicroVM from one snapshot of the image, so a disk mounted before the snapshot would come back in every MicroVM as the same Archil client. Mounting in the run hook gives each MicroVM its own.
One MicroVM holds the disk at a time. A plain mount is exclusive. To mount the same disk in several MicroVMs at once, add --conditional to the archil mount command. Every MicroVM can then read and write anywhere on the disk; see Sharing disks.
Suspend and resume need no special handling. When a MicroVM suspends, the mount is suspended with it. On resume, the client reconnects on its own and finishes any request that was in flight.
Unmount in the terminate hook. Unmounting sends the client’s buffered writes to the disk and releases it for the next MicroVM. If a MicroVM ever goes away with the disk still mounted, see Release a stale lock.
Clean up
Delete the image, its build artifact, the roles, and the disk once you no longer need them:Next steps
- Python SDK: read, write, and search the disk without mounting it.
- Sharing disks: mount one disk in many MicroVMs at once.
archilCLI reference: everyarchil mountoption.