Best practices for Lambda MicroVMs
Follow these best practices to build reliable, cost-effective, and secure applications with AWS Lambda MicroVMs.
Application design
Design your application to work well with MicroVM snapshots and lifecycle hooks:
-
Use lifecycle hooks – Implement
/runfor post-run initialization,/suspendfor graceful cleanup,/resumefor connection re-establishment, and/terminatefor data flushing. -
Handle snapshot re-use – Generate UUIDs, secrets, and random values in the
/runhook, not during image build. Use CSPRNGs for all random number generation. -
Design for resume – Validate all network connections and cached state in the
/resumehook. AWS SDK connections typically recover automatically, but custom connections may not.
Performance
Optimize MicroVM performance with the following practices:
-
Right-size your MicroVM – Choose the smallest MicroVM size that meets your CPU, memory, and bandwidth requirements. Bandwidth scales with configured memory.
-
Minimize snapshot size – Keep your application footprint small to reduce run and resume times. Remove build-time dependencies and temporary files before the image snapshot is captured.
-
Use suspend/resume for latency-sensitive workloads – Keep frequently-used environments in suspended state to quickly resume execution.
Security
Follow these practices to secure your MicroVM workloads:
-
Rotate auth tokens – Generate short-lived tokens (15–30 minutes). Implement token refresh logic in your client before expiry.
-
Scope tokens to specific ports – Use the
allowedPortsparameter to limit token scope to only the ports your application uses. -
Use VPC egress for sensitive traffic – Route traffic to databases and internal APIs through a VPC egress connector rather than the public internet.
-
Apply least-privilege IAM – Separate build roles (image creation) from execution roles (runtime). Grant minimum required permissions.
Cost optimization
Reduce costs with the following strategies:
-
Configure idle policies – Set
maxIdleDurationSecondsto suspend idle MicroVMs automatically. Suspended MicroVMs do not incur compute charges. -
Set maximumDurationInSeconds – Prevent runaway costs by setting a hard termination time on every MicroVM.
-
Terminate unused MicroVMs – Do not leave MicroVMs running indefinitely. Use
suspendedDurationSecondsto auto-terminate after extended idle periods. -
Tag resources – Use tags on MicroVM images for cost allocation and reporting in AWS Cost Explorer.
-
Right-size your MicroVMs – Start with the smallest baseline that meets your performance needs. You can burst to 4x baseline during peak activity without re-provisioning.
-
Clean up unused images – Delete MicroVM image versions you no longer need to reduce storage costs.
Monitoring
Monitor your MicroVMs effectively:
-
Review stateReason – When MicroVMs terminate unexpectedly, the
get-microvmresponse includes astateReasonwith the reason for the unexpected failure.