microvm-ctl 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. microvm_ctl-0.1.0/LICENSE +201 -0
  2. microvm_ctl-0.1.0/PKG-INFO +173 -0
  3. microvm_ctl-0.1.0/README.md +141 -0
  4. microvm_ctl-0.1.0/microvm/__init__.py +28 -0
  5. microvm_ctl-0.1.0/microvm/bootstrap.py +99 -0
  6. microvm_ctl-0.1.0/microvm/cli.py +382 -0
  7. microvm_ctl-0.1.0/microvm/client.py +60 -0
  8. microvm_ctl-0.1.0/microvm/config.py +81 -0
  9. microvm_ctl-0.1.0/microvm/data/lambda-microvms/2025-09-09/endpoint-rule-set-1.json.gz +0 -0
  10. microvm_ctl-0.1.0/microvm/data/lambda-microvms/2025-09-09/paginators-1.json +40 -0
  11. microvm_ctl-0.1.0/microvm/data/lambda-microvms/2025-09-09/service-2.json.gz +0 -0
  12. microvm_ctl-0.1.0/microvm/endpoint.py +136 -0
  13. microvm_ctl-0.1.0/microvm/fleet.py +291 -0
  14. microvm_ctl-0.1.0/microvm/hooks/__init__.py +3 -0
  15. microvm_ctl-0.1.0/microvm/hooks/server.py +168 -0
  16. microvm_ctl-0.1.0/microvm/images.py +204 -0
  17. microvm_ctl-0.1.0/microvm/monitor.py +107 -0
  18. microvm_ctl-0.1.0/microvm/throttle.py +65 -0
  19. microvm_ctl-0.1.0/microvm_ctl.egg-info/PKG-INFO +173 -0
  20. microvm_ctl-0.1.0/microvm_ctl.egg-info/SOURCES.txt +31 -0
  21. microvm_ctl-0.1.0/microvm_ctl.egg-info/dependency_links.txt +1 -0
  22. microvm_ctl-0.1.0/microvm_ctl.egg-info/entry_points.txt +2 -0
  23. microvm_ctl-0.1.0/microvm_ctl.egg-info/requires.txt +7 -0
  24. microvm_ctl-0.1.0/microvm_ctl.egg-info/top_level.txt +1 -0
  25. microvm_ctl-0.1.0/pyproject.toml +56 -0
  26. microvm_ctl-0.1.0/setup.cfg +4 -0
  27. microvm_ctl-0.1.0/tests/test_config.py +24 -0
  28. microvm_ctl-0.1.0/tests/test_cost_model.py +20 -0
  29. microvm_ctl-0.1.0/tests/test_endpoint.py +96 -0
  30. microvm_ctl-0.1.0/tests/test_fleet.py +69 -0
  31. microvm_ctl-0.1.0/tests/test_hooks.py +81 -0
  32. microvm_ctl-0.1.0/tests/test_hooks_validate.py +52 -0
  33. microvm_ctl-0.1.0/tests/test_throttle.py +63 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,173 @@
1
+ Metadata-Version: 2.4
2
+ Name: microvm-ctl
3
+ Version: 0.1.0
4
+ Summary: Control and execution plane for AWS Lambda MicroVMs: build images, launch Firecracker microVMs, scale fleets inside your quotas, call into them securely, and watch it all live.
5
+ Author: Vivek Raja P S
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/Vivek0712/microvm-ctl
8
+ Project-URL: Repository, https://github.com/Vivek0712/microvm-ctl
9
+ Project-URL: Documentation, https://github.com/Vivek0712/microvm-ctl/tree/main/docs
10
+ Project-URL: Changelog, https://github.com/Vivek0712/microvm-ctl/blob/main/CHANGELOG.md
11
+ Project-URL: Examples, https://github.com/Vivek0712/awesome-microvm
12
+ Keywords: aws,lambda,microvm,firecracker,sandbox,serverless,fleet,orchestrator
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: Apache Software License
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Topic :: System :: Distributed Computing
21
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
22
+ Requires-Python: >=3.9
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Requires-Dist: boto3>=1.34
26
+ Requires-Dist: requests>=2.31
27
+ Requires-Dist: rich>=13.7
28
+ Provides-Extra: dev
29
+ Requires-Dist: pytest>=8; extra == "dev"
30
+ Requires-Dist: ruff>=0.5; extra == "dev"
31
+ Dynamic: license-file
32
+
33
+ # microvm-ctl
34
+
35
+ **The control and execution plane for [AWS Lambda MicroVMs](https://docs.aws.amazon.com/lambda/latest/dg/lambda-microvms-guide.html).** Build a snapshot image from a Dockerfile, launch Firecracker microVMs in seconds, scale a fleet inside the quotas your account actually has, call into every VM over its authenticated endpoint, and watch the whole thing live. One Python SDK, one `mvm` command.
36
+
37
+ [![ci](https://github.com/Vivek0712/microvm-ctl/actions/workflows/ci.yml/badge.svg)](https://github.com/Vivek0712/microvm-ctl/actions/workflows/ci.yml)
38
+ [![pypi](https://img.shields.io/pypi/v/microvm-ctl)](https://pypi.org/project/microvm-ctl/)
39
+ ![python](https://img.shields.io/badge/python-3.9%20to%203.12-blue)
40
+ ![license](https://img.shields.io/badge/license-Apache--2.0-green)
41
+
42
+ ```console
43
+ pip install microvm-ctl
44
+
45
+ mvm bootstrap # one time: S3 artifact bucket + build/execution IAM roles
46
+ mvm image build my-sandbox ./my-app # Dockerfile at ./my-app root -> runnable snapshot
47
+ mvm run my-sandbox --wait # RunMicrovm, then poll to RUNNING
48
+ mvm call <microvm-id> /execute -X POST -d '{"code":"print(2+2)"}'
49
+ mvm scale my-sandbox 10 # converge the fleet, throttled to your applied quota
50
+ mvm top --watch # live state table
51
+ ```
52
+
53
+ ## Why this exists
54
+
55
+ Lambda MicroVMs exposes the primitive under Lambda itself: a Firecracker VM with a full AL2023 userland, a dedicated HTTPS endpoint, and a lifecycle you control (run, suspend, resume, terminate). The service deliberately stops there. There is no load balancer (one endpoint per VM), no fleet abstraction, no token management, no dashboard, and a fresh account runs quotas far below the published defaults. microvm-ctl fills that gap.
56
+
57
+ | Concern | What the plane does | Module |
58
+ |---|---|---|
59
+ | Image factory | app directory to zip, to S3, to `CreateMicrovmImage`, to an ACTIVE version; injects the hook runtime into every image | `microvm/images.py` |
60
+ | Lifecycle and fleets | run, suspend, resume, terminate, `Fleet.scale_to(n)`, drain, reap | `microvm/fleet.py` |
61
+ | Quota-aware throttling | reads the quotas *applied* to your account, not the published defaults, and token-buckets every mutating call at 80% of them | `microvm/fleet.py`, `microvm/throttle.py` |
62
+ | Execution plane | mints and caches port-scoped JWE tokens, sets `X-aws-proxy-auth`, backs off on 429, waits out a 502 while a suspended VM auto-resumes | `microvm/endpoint.py` |
63
+ | In-VM hook runtime | zero-dependency server for `/ready`, `/validate`, `/run`, `/resume`, `/suspend`, `/terminate` plus your own routes | `microvm/hooks/server.py` |
64
+ | Observability and cost | live fleet table, CloudWatch tail, a cost model that prices a session shape before you commit to it | `microvm/monitor.py` |
65
+ | Account bootstrap | artifact bucket plus separate build and execution roles | `microvm/bootstrap.py` |
66
+
67
+ The `lambda-microvms` botocore service model ships inside the package, so the plane works on whatever boto3 you already have.
68
+
69
+ ![architecture](docs/img/architecture.png)
70
+
71
+ ## Measured, not quoted
72
+
73
+ Every number in this repo comes from the live service in `us-east-1`. The harness that produced them is [`benchmarks/benchmark.py`](benchmarks/benchmark.py); run it against your own account.
74
+
75
+ | What | Measured |
76
+ |---|---|
77
+ | Image build, Dockerfile to runnable snapshot | 123 to 145 s |
78
+ | `RunMicrovm` to serving authenticated traffic | **p50 3.54 s**, p95 4.49 s |
79
+ | Warm authenticated request (real Python execution inside the VM) | **p50 111 ms** |
80
+ | Explicit suspend / resume | 2.5 s / 2.6 s, same PID, state intact |
81
+ | First request to a suspended VM (auto-resume) | **200 OK in 0.7 s** |
82
+ | Fleet scale-out, 0 to 6 running VMs, on a 1 launch/s quota | **9.7 s** wall |
83
+ | 30 min active + 8 h suspended session vs always-on | **93.8% cheaper** |
84
+
85
+ ![benchmark transcript](docs/img/benchmark.png)
86
+
87
+ ## The SDK in 20 lines
88
+
89
+ ```python
90
+ from microvm import PlaneConfig, FleetManager, EndpointClient, Fleet
91
+ from microvm.fleet import IdlePolicy
92
+
93
+ cfg = PlaneConfig() # region, profile, bucket, roles from MVM_* env vars
94
+ fm = FleetManager(cfg) # throttled to your account's applied TPS
95
+
96
+ vm = fm.run("my-sandbox",
97
+ idle_policy=IdlePolicy(max_idle=300, suspended_for=3600, auto_resume=True),
98
+ run_payload='{"tenant_id": "acme"}')
99
+
100
+ client = EndpointClient(cfg, vm.microvm_id)
101
+ print(client.post("/execute", json={"code": "print(41+1)"}).json())
102
+
103
+ fleet = Fleet(fm, "my-sandbox")
104
+ fleet.scale_to(20, wait_running=True) # one call, token-bucketed, jittered retries
105
+ fleet.suspend_all() # park the fleet: snapshot storage billing only
106
+ fleet.drain() # terminate every member
107
+ ```
108
+
109
+ Inside the VM, declare the lifecycle with the injected hook runtime. There is nothing to install in the image.
110
+
111
+ ```python
112
+ from microvm_hooks import HookApp
113
+ app = HookApp()
114
+
115
+ @app.on_ready
116
+ def ready(ctx):
117
+ warm_caches()
118
+ return True # 200 means "snapshot me now"
119
+
120
+ @app.on_run
121
+ def run(ctx): # every clone, before traffic; RNG is reseeded for you
122
+ load_tenant(ctx.get("runHookPayload"))
123
+
124
+ @app.route("POST", "/execute")
125
+ def execute(body, headers):
126
+ return 200, {"out": sandbox_exec(body["code"])}
127
+
128
+ app.serve(port=8080)
129
+ ```
130
+
131
+ ## What the CLI looks like
132
+
133
+ `mvm quotas` shows the difference between the published defaults and what your account can actually do. This is a fresh account: one launch per second and 8 GB of total microVM memory.
134
+
135
+ ![mvm quotas](docs/img/mvm-quotas.png)
136
+
137
+ `mvm image ls` after building the eight example images from the companion repo.
138
+
139
+ ![mvm image ls](docs/img/mvm-image-ls.png)
140
+
141
+ `mvm cost` prices a session shape from the published rates. This is the 30 minutes active plus 8 hours suspended shape on a 2 GB VM with the measured 0.61 GB snapshot.
142
+
143
+ ![mvm cost](docs/img/mvm-cost.png)
144
+
145
+ ## Documentation
146
+
147
+ - [Quickstart](docs/quickstart.md): from an empty account to a serving VM, with the environment variables explained.
148
+ - [CLI reference](docs/cli.md): every `mvm` command and flag.
149
+ - [SDK reference](docs/sdk.md): `PlaneConfig`, `ImageBuilder`, `FleetManager`, `Fleet`, `EndpointClient`, `FleetMonitor`, `CostModel`.
150
+ - [Architecture](docs/architecture.md): the two planes, the lifecycle state machine, the fleet design decisions, and the security model.
151
+ - [The hook contract](docs/hooks.md): what each hook is for and what breaks when you ignore it.
152
+ - [Quotas and cost](docs/quotas-and-cost.md): the quota walls, what counts against them, and the cost model with worked examples.
153
+ - [Troubleshooting](docs/troubleshooting.md): the errors we hit on the live service and what each one meant.
154
+
155
+ ## Examples and the article series
156
+
157
+ Eight production-shaped apps run on this plane: a code execution sandbox, an AI code runner with a self-repair loop, an agent evaluation fleet, a stateful notebook kernel, sandboxed DuckDB analytics, an ephemeral CI runner, an HTML to PDF service, and multi-tenant AI agents. Each has a Dockerfile, a single-file app, a recorded live transcript, and an article in the series **Building on AWS Lambda MicroVMs**. They live in the companion repo [awesome-microvm](https://github.com/Vivek0712/awesome-microvm).
158
+
159
+ ## Requirements and regions
160
+
161
+ Python 3.9 or newer on the machine running the plane. The VMs themselves are ARM64 (Graviton) only, so audit binary wheels before you build an image. The service is available in `us-east-1`, `us-east-2`, `us-west-2`, `eu-west-1`, and `ap-northeast-1`.
162
+
163
+ ## Credits and inspiration
164
+
165
+ This project grew out of [lambda-microvm-starter](https://github.com/vidanov/lambda-microvm-starter) by [Alexey Vidanov](https://github.com/vidanov), the one-command on-ramp that deploys any Dockerfile to a Lambda MicroVM behind a public CloudFront URL. His starter kit and its troubleshooting notes were the first working map of the service we had, and several of the gotchas documented here were first written down there. microvm-ctl takes the next step from one deployed app to fleets, tokens, quotas, and cost, and we are grateful for the ground he covered first.
166
+
167
+ ## Contributing
168
+
169
+ Issues and pull requests are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) for the local test loop (no AWS account needed for the unit tests) and the conventions used in this repo.
170
+
171
+ ## License
172
+
173
+ Apache-2.0
@@ -0,0 +1,141 @@
1
+ # microvm-ctl
2
+
3
+ **The control and execution plane for [AWS Lambda MicroVMs](https://docs.aws.amazon.com/lambda/latest/dg/lambda-microvms-guide.html).** Build a snapshot image from a Dockerfile, launch Firecracker microVMs in seconds, scale a fleet inside the quotas your account actually has, call into every VM over its authenticated endpoint, and watch the whole thing live. One Python SDK, one `mvm` command.
4
+
5
+ [![ci](https://github.com/Vivek0712/microvm-ctl/actions/workflows/ci.yml/badge.svg)](https://github.com/Vivek0712/microvm-ctl/actions/workflows/ci.yml)
6
+ [![pypi](https://img.shields.io/pypi/v/microvm-ctl)](https://pypi.org/project/microvm-ctl/)
7
+ ![python](https://img.shields.io/badge/python-3.9%20to%203.12-blue)
8
+ ![license](https://img.shields.io/badge/license-Apache--2.0-green)
9
+
10
+ ```console
11
+ pip install microvm-ctl
12
+
13
+ mvm bootstrap # one time: S3 artifact bucket + build/execution IAM roles
14
+ mvm image build my-sandbox ./my-app # Dockerfile at ./my-app root -> runnable snapshot
15
+ mvm run my-sandbox --wait # RunMicrovm, then poll to RUNNING
16
+ mvm call <microvm-id> /execute -X POST -d '{"code":"print(2+2)"}'
17
+ mvm scale my-sandbox 10 # converge the fleet, throttled to your applied quota
18
+ mvm top --watch # live state table
19
+ ```
20
+
21
+ ## Why this exists
22
+
23
+ Lambda MicroVMs exposes the primitive under Lambda itself: a Firecracker VM with a full AL2023 userland, a dedicated HTTPS endpoint, and a lifecycle you control (run, suspend, resume, terminate). The service deliberately stops there. There is no load balancer (one endpoint per VM), no fleet abstraction, no token management, no dashboard, and a fresh account runs quotas far below the published defaults. microvm-ctl fills that gap.
24
+
25
+ | Concern | What the plane does | Module |
26
+ |---|---|---|
27
+ | Image factory | app directory to zip, to S3, to `CreateMicrovmImage`, to an ACTIVE version; injects the hook runtime into every image | `microvm/images.py` |
28
+ | Lifecycle and fleets | run, suspend, resume, terminate, `Fleet.scale_to(n)`, drain, reap | `microvm/fleet.py` |
29
+ | Quota-aware throttling | reads the quotas *applied* to your account, not the published defaults, and token-buckets every mutating call at 80% of them | `microvm/fleet.py`, `microvm/throttle.py` |
30
+ | Execution plane | mints and caches port-scoped JWE tokens, sets `X-aws-proxy-auth`, backs off on 429, waits out a 502 while a suspended VM auto-resumes | `microvm/endpoint.py` |
31
+ | In-VM hook runtime | zero-dependency server for `/ready`, `/validate`, `/run`, `/resume`, `/suspend`, `/terminate` plus your own routes | `microvm/hooks/server.py` |
32
+ | Observability and cost | live fleet table, CloudWatch tail, a cost model that prices a session shape before you commit to it | `microvm/monitor.py` |
33
+ | Account bootstrap | artifact bucket plus separate build and execution roles | `microvm/bootstrap.py` |
34
+
35
+ The `lambda-microvms` botocore service model ships inside the package, so the plane works on whatever boto3 you already have.
36
+
37
+ ![architecture](docs/img/architecture.png)
38
+
39
+ ## Measured, not quoted
40
+
41
+ Every number in this repo comes from the live service in `us-east-1`. The harness that produced them is [`benchmarks/benchmark.py`](benchmarks/benchmark.py); run it against your own account.
42
+
43
+ | What | Measured |
44
+ |---|---|
45
+ | Image build, Dockerfile to runnable snapshot | 123 to 145 s |
46
+ | `RunMicrovm` to serving authenticated traffic | **p50 3.54 s**, p95 4.49 s |
47
+ | Warm authenticated request (real Python execution inside the VM) | **p50 111 ms** |
48
+ | Explicit suspend / resume | 2.5 s / 2.6 s, same PID, state intact |
49
+ | First request to a suspended VM (auto-resume) | **200 OK in 0.7 s** |
50
+ | Fleet scale-out, 0 to 6 running VMs, on a 1 launch/s quota | **9.7 s** wall |
51
+ | 30 min active + 8 h suspended session vs always-on | **93.8% cheaper** |
52
+
53
+ ![benchmark transcript](docs/img/benchmark.png)
54
+
55
+ ## The SDK in 20 lines
56
+
57
+ ```python
58
+ from microvm import PlaneConfig, FleetManager, EndpointClient, Fleet
59
+ from microvm.fleet import IdlePolicy
60
+
61
+ cfg = PlaneConfig() # region, profile, bucket, roles from MVM_* env vars
62
+ fm = FleetManager(cfg) # throttled to your account's applied TPS
63
+
64
+ vm = fm.run("my-sandbox",
65
+ idle_policy=IdlePolicy(max_idle=300, suspended_for=3600, auto_resume=True),
66
+ run_payload='{"tenant_id": "acme"}')
67
+
68
+ client = EndpointClient(cfg, vm.microvm_id)
69
+ print(client.post("/execute", json={"code": "print(41+1)"}).json())
70
+
71
+ fleet = Fleet(fm, "my-sandbox")
72
+ fleet.scale_to(20, wait_running=True) # one call, token-bucketed, jittered retries
73
+ fleet.suspend_all() # park the fleet: snapshot storage billing only
74
+ fleet.drain() # terminate every member
75
+ ```
76
+
77
+ Inside the VM, declare the lifecycle with the injected hook runtime. There is nothing to install in the image.
78
+
79
+ ```python
80
+ from microvm_hooks import HookApp
81
+ app = HookApp()
82
+
83
+ @app.on_ready
84
+ def ready(ctx):
85
+ warm_caches()
86
+ return True # 200 means "snapshot me now"
87
+
88
+ @app.on_run
89
+ def run(ctx): # every clone, before traffic; RNG is reseeded for you
90
+ load_tenant(ctx.get("runHookPayload"))
91
+
92
+ @app.route("POST", "/execute")
93
+ def execute(body, headers):
94
+ return 200, {"out": sandbox_exec(body["code"])}
95
+
96
+ app.serve(port=8080)
97
+ ```
98
+
99
+ ## What the CLI looks like
100
+
101
+ `mvm quotas` shows the difference between the published defaults and what your account can actually do. This is a fresh account: one launch per second and 8 GB of total microVM memory.
102
+
103
+ ![mvm quotas](docs/img/mvm-quotas.png)
104
+
105
+ `mvm image ls` after building the eight example images from the companion repo.
106
+
107
+ ![mvm image ls](docs/img/mvm-image-ls.png)
108
+
109
+ `mvm cost` prices a session shape from the published rates. This is the 30 minutes active plus 8 hours suspended shape on a 2 GB VM with the measured 0.61 GB snapshot.
110
+
111
+ ![mvm cost](docs/img/mvm-cost.png)
112
+
113
+ ## Documentation
114
+
115
+ - [Quickstart](docs/quickstart.md): from an empty account to a serving VM, with the environment variables explained.
116
+ - [CLI reference](docs/cli.md): every `mvm` command and flag.
117
+ - [SDK reference](docs/sdk.md): `PlaneConfig`, `ImageBuilder`, `FleetManager`, `Fleet`, `EndpointClient`, `FleetMonitor`, `CostModel`.
118
+ - [Architecture](docs/architecture.md): the two planes, the lifecycle state machine, the fleet design decisions, and the security model.
119
+ - [The hook contract](docs/hooks.md): what each hook is for and what breaks when you ignore it.
120
+ - [Quotas and cost](docs/quotas-and-cost.md): the quota walls, what counts against them, and the cost model with worked examples.
121
+ - [Troubleshooting](docs/troubleshooting.md): the errors we hit on the live service and what each one meant.
122
+
123
+ ## Examples and the article series
124
+
125
+ Eight production-shaped apps run on this plane: a code execution sandbox, an AI code runner with a self-repair loop, an agent evaluation fleet, a stateful notebook kernel, sandboxed DuckDB analytics, an ephemeral CI runner, an HTML to PDF service, and multi-tenant AI agents. Each has a Dockerfile, a single-file app, a recorded live transcript, and an article in the series **Building on AWS Lambda MicroVMs**. They live in the companion repo [awesome-microvm](https://github.com/Vivek0712/awesome-microvm).
126
+
127
+ ## Requirements and regions
128
+
129
+ Python 3.9 or newer on the machine running the plane. The VMs themselves are ARM64 (Graviton) only, so audit binary wheels before you build an image. The service is available in `us-east-1`, `us-east-2`, `us-west-2`, `eu-west-1`, and `ap-northeast-1`.
130
+
131
+ ## Credits and inspiration
132
+
133
+ This project grew out of [lambda-microvm-starter](https://github.com/vidanov/lambda-microvm-starter) by [Alexey Vidanov](https://github.com/vidanov), the one-command on-ramp that deploys any Dockerfile to a Lambda MicroVM behind a public CloudFront URL. His starter kit and its troubleshooting notes were the first working map of the service we had, and several of the gotchas documented here were first written down there. microvm-ctl takes the next step from one deployed app to fleets, tokens, quotas, and cost, and we are grateful for the ground he covered first.
134
+
135
+ ## Contributing
136
+
137
+ Issues and pull requests are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) for the local test loop (no AWS account needed for the unit tests) and the conventions used in this repo.
138
+
139
+ ## License
140
+
141
+ Apache-2.0
@@ -0,0 +1,28 @@
1
+ """microvm-ctl: control and execution plane for AWS Lambda MicroVMs.
2
+
3
+ Control plane : build images, run/suspend/resume/terminate, scale fleets.
4
+ Execution plane: authenticated calls into the microVM endpoint, in-VM hook server.
5
+ """
6
+
7
+ from microvm.client import lambda_client, microvm_client
8
+ from microvm.config import PlaneConfig
9
+ from microvm.endpoint import EndpointClient, EndpointError
10
+ from microvm.fleet import Fleet, FleetManager
11
+ from microvm.images import ImageBuilder, ImageBuildError
12
+ from microvm.monitor import CostModel, FleetMonitor
13
+
14
+ __version__ = "0.1.0"
15
+
16
+ __all__ = [
17
+ "microvm_client",
18
+ "lambda_client",
19
+ "PlaneConfig",
20
+ "ImageBuilder",
21
+ "ImageBuildError",
22
+ "Fleet",
23
+ "FleetManager",
24
+ "EndpointClient",
25
+ "EndpointError",
26
+ "FleetMonitor",
27
+ "CostModel",
28
+ ]
@@ -0,0 +1,99 @@
1
+ """One-time account bootstrap: artifact bucket + build/execution IAM roles.
2
+
3
+ Build role — assumed by the image build infrastructure: reads the code
4
+ artifact zip from S3 and writes build logs.
5
+ Execution — assumed by the running microVM (credentials are vended inside
6
+ role the VM): runtime logs plus whatever your workload needs.
7
+ Build vs execution role separation is deliberate — never let
8
+ the sandbox role read your artifact bucket.
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import json
14
+ import time
15
+
16
+ from microvm.client import lambda_client
17
+ from microvm.config import PlaneConfig
18
+
19
+ TRUST = {
20
+ "Version": "2012-10-17",
21
+ "Statement": [
22
+ {
23
+ "Effect": "Allow",
24
+ "Principal": {"Service": "lambda.amazonaws.com"},
25
+ "Action": ["sts:AssumeRole", "sts:TagSession"],
26
+ }
27
+ ],
28
+ }
29
+
30
+
31
+ def _ensure_role(iam, name: str, policy_doc: dict) -> str:
32
+ try:
33
+ arn = iam.get_role(RoleName=name)["Role"]["Arn"]
34
+ except iam.exceptions.NoSuchEntityException:
35
+ arn = iam.create_role(
36
+ RoleName=name,
37
+ AssumeRolePolicyDocument=json.dumps(TRUST),
38
+ Description="microvm-ctl control plane",
39
+ )["Role"]["Arn"]
40
+ time.sleep(8) # IAM eventual consistency before first use
41
+ iam.put_role_policy(
42
+ RoleName=name, PolicyName=f"{name}-inline", PolicyDocument=json.dumps(policy_doc)
43
+ )
44
+ return arn
45
+
46
+
47
+ def bootstrap(cfg: PlaneConfig, prefix: str = "microvm-ctl") -> dict:
48
+ """Create (idempotently) the artifact bucket and the two IAM roles.
49
+
50
+ Returns the three values the rest of the plane needs, ready to export as
51
+ MVM_ARTIFACT_BUCKET / MVM_BUILD_ROLE_ARN / MVM_EXECUTION_ROLE_ARN.
52
+ """
53
+ sts = lambda_client("sts", cfg.region, cfg.profile)
54
+ account = sts.get_caller_identity()["Account"]
55
+ bucket = cfg.artifact_bucket or f"{prefix}-artifacts-{account}-{cfg.region}"
56
+
57
+ s3 = lambda_client("s3", cfg.region, cfg.profile)
58
+ try:
59
+ s3.head_bucket(Bucket=bucket)
60
+ except Exception:
61
+ params = {"Bucket": bucket}
62
+ if cfg.region != "us-east-1":
63
+ params["CreateBucketConfiguration"] = {"LocationConstraint": cfg.region}
64
+ s3.create_bucket(**params)
65
+ s3.put_public_access_block(
66
+ Bucket=bucket,
67
+ PublicAccessBlockConfiguration={
68
+ "BlockPublicAcls": True, "IgnorePublicAcls": True,
69
+ "BlockPublicPolicy": True, "RestrictPublicBuckets": True,
70
+ },
71
+ )
72
+
73
+ iam = lambda_client("iam", cfg.region, cfg.profile)
74
+ logs_stmt = {
75
+ "Effect": "Allow",
76
+ "Action": ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"],
77
+ "Resource": f"arn:aws:logs:{cfg.region}:{account}:log-group:/aws/lambda/microvms/*",
78
+ }
79
+ build_role = _ensure_role(
80
+ iam,
81
+ f"{prefix}-build-role",
82
+ {
83
+ "Version": "2012-10-17",
84
+ "Statement": [
85
+ {"Effect": "Allow", "Action": "s3:GetObject", "Resource": f"arn:aws:s3:::{bucket}/*"},
86
+ logs_stmt,
87
+ ],
88
+ },
89
+ )
90
+ execution_role = _ensure_role(
91
+ iam,
92
+ f"{prefix}-execution-role",
93
+ {"Version": "2012-10-17", "Statement": [logs_stmt]},
94
+ )
95
+ return {
96
+ "artifact_bucket": bucket,
97
+ "build_role_arn": build_role,
98
+ "execution_role_arn": execution_role,
99
+ }