Skip to main content
An AWS Lambda MicroVM is a Firecracker VM that Lambda builds from your Dockerfile, snapshots, and restores whenever you start one. When a MicroVM terminates, its files go with it. Mount an Archil disk in the MicroVM and anything written there stays on the disk, where the next MicroVM can mount it and your app can read it with the Archil SDK. Lambda MicroVMs support FUSE when the image is built with full OS capabilities, so the disk mounts as a regular directory. Mount it from the MicroVM’s 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, or aws-us-east-2 (in preview).
  • AWS CLI 2.35.10 or later, which adds the aws lambda-microvms commands, and jq.
  • An S3 bucket in the same region to hold the image’s build artifact.
Set your Archil API key and region, and the matching AWS region for the AWS CLI:
Create the disk with the disk CLI. The output includes the disk ID and a one-time mount token, which grants access to this disk only:
Export both for the steps below:

Build the image

The image needs the archil 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
Zip both files and upload the zip to your bucket:
Lambda needs two IAM roles: one to build the image, which reads the artifact from S3, and one for the running MicroVM, which writes your app’s output to CloudWatch Logs. Both trust lambda.amazonaws.com:
Now create the image. --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:
The build takes a few minutes. Wait until the first version reports 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:
The MicroVM stays 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.
Request the app on port 8080 with an auth token for the MicroVM:
Terminate the MicroVM. Its terminate hook unmounts the disk, which sends every buffered write to the disk and releases it:
Start a second MicroVM with the same commands and request the app again. It finds the line the first MicroVM wrote:
If you would rather not pass the token at launch, store it in AWS Secrets Manager, pass the secret’s ARN in the payload instead, and read the secret in the run hook. Grant the execution role secretsmanager:GetSecretValue on that secret.

How the disk behaves in a MicroVM

Mount in the run 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