microvms 0.0.0-stage → 1.0.0

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.
package/README.md CHANGED
@@ -1,3 +1,160 @@
1
- # Temporary Holding Version
1
+ # microvms
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Run coding agents and their tools in sandboxed AWS Lambda MicroVMs. Give each
4
+ task its own Linux workspace, execute commands, collect files, and terminate
5
+ the VM from JavaScript or TypeScript. `AgentVm` runs Claude Code or Codex
6
+ through Amazon Bedrock; `Sandbox` supports your own agent or command runner.
7
+
8
+ ## Install
9
+
10
+ ```sh
11
+ npm install microvms
12
+ ```
13
+
14
+ Requires Node.js **22.13.0+**. Native addons ship for Linux x64/ARM64 (glibc),
15
+ macOS Apple Silicon, and Windows x64. TypeScript declarations are included.
16
+
17
+ ## Run your first command
18
+
19
+ This example starts a VM from an image, runs a command, and cleans up.
20
+ Set up AWS and build the image first. Each launch creates resources that
21
+ AWS bills to your account.
22
+
23
+ You need Lambda MicroVMs access, your normal AWS credential configuration,
24
+ and an execution role. Follow [AWS setup](https://laithalsaadoon.github.io/microvms-agentd/learn/tutorial/first-run/)
25
+ to set `AWS_REGION` and `MICROVM_EXECUTION_ROLE_ARN`. Then use the
26
+ [CLI](https://laithalsaadoon.github.io/microvms-agentd/learn/tutorial/install/)
27
+ to prepare an image containing `agentd`:
28
+
29
+ ```sh
30
+ microvm build --name agent-tools --json
31
+ export MICROVM_IMAGE='paste data.imageIdentifier from the result'
32
+ ```
33
+
34
+ Use the **image ARN**, in the same account and region as your credentials.
35
+ The SDK can also look up an image by name in that region. This example uses
36
+ the ARN returned by the build.
37
+ The CLI provisions `agentd` and uploads the build artifact for you.
38
+ Shell examples use Bash or Zsh; in PowerShell, set variables with `$env:NAME='value'`.
39
+
40
+ Save as `hello.mjs`:
41
+
42
+ ```js
43
+ import { Region, Sandbox } from 'microvms';
44
+
45
+ const imageIdentifier = process.env.MICROVM_IMAGE;
46
+ const executionRoleArn = process.env.MICROVM_EXECUTION_ROLE_ARN;
47
+ if (!imageIdentifier || !executionRoleArn) {
48
+ throw new Error('Set MICROVM_IMAGE and MICROVM_EXECUTION_ROLE_ARN first');
49
+ }
50
+ const vm = await Sandbox.create(Region.parse(process.env.AWS_REGION ?? 'us-east-1'));
51
+ try {
52
+ const session = await vm.run({ imageIdentifier, executionRoleArn });
53
+ const result = await session.runSync(['echo', 'hello from a sandbox']);
54
+ process.stdout.write(result.stdout);
55
+ process.stderr.write(result.stderr);
56
+ if (!result.ok) process.exitCode = 1;
57
+ } finally {
58
+ const cleanup = await vm.terminate();
59
+ if (cleanup.failures.length || cleanup.undeleted.length) {
60
+ console.error('Cleanup needs attention:', cleanup);
61
+ process.exitCode = 1;
62
+ }
63
+ }
64
+ ```
65
+
66
+ ```sh
67
+ node hello.mjs
68
+ ```
69
+
70
+ Expected output: `hello from a sandbox`. The VM is terminated after the
71
+ command; the image stays available for reuse. `terminate()` returns once
72
+ termination is accepted by default. Pass `{ waitForTerminated: true }` to
73
+ wait for the final state. Inspect `failures` and `undeleted` because cleanup
74
+ reports failures in its result.
75
+
76
+ ## Run a coding agent
77
+
78
+ First prepare a Claude Code image using the same AWS setup plus
79
+ [Bedrock permissions](https://laithalsaadoon.github.io/microvms-agentd/learn/operations/run-coding-agents-on-bedrock/).
80
+ This builds or reuses the agent image and starts a temporary VM. Capture
81
+ only its image ARN, then terminate that temporary VM:
82
+
83
+ ```sh
84
+ MICROVM_AGENT_IMAGE="$(microvm agent-up --vm-name sdk-image --agent claude-code --json |
85
+ node -pe 'JSON.parse(require("node:fs").readFileSync(0, "utf8")).data.imageIdentifier')"
86
+ export MICROVM_AGENT_IMAGE
87
+ microvm terminate sdk-image --wait
88
+ ```
89
+
90
+ Save as `agent.mjs`. The agent writes a file inside its own VM; the SDK
91
+ downloads that file before cleanup:
92
+
93
+ ```js
94
+ import { writeFile } from 'node:fs/promises';
95
+ import { AgentVm, Region } from 'microvms';
96
+
97
+ const imageIdentifier = process.env.MICROVM_AGENT_IMAGE;
98
+ const executionRoleArn = process.env.MICROVM_EXECUTION_ROLE_ARN;
99
+ if (!imageIdentifier || !executionRoleArn) {
100
+ throw new Error('Set MICROVM_AGENT_IMAGE and MICROVM_EXECUTION_ROLE_ARN first');
101
+ }
102
+ const vm = await AgentVm.create(Region.parse(process.env.AWS_REGION ?? 'us-east-1'));
103
+ try {
104
+ const session = await vm.launch({ imageIdentifier, executionRoleArn });
105
+ await vm.installAccess();
106
+ const result = await vm.promptSync(
107
+ 'claude-code',
108
+ 'Create /workspace/hello.py that prints hello from a sandbox. Run it.',
109
+ );
110
+ process.stdout.write(result.stdout);
111
+ process.stderr.write(result.stderr);
112
+ if (!result.ok) throw new Error(`Agent exited with ${result.exitCode}`);
113
+ await writeFile('hello-from-agent.py', await session.downloadFile('/workspace/hello.py'));
114
+ } finally {
115
+ const cleanup = await vm.terminate();
116
+ if (cleanup.failures.length || cleanup.undeleted.length) {
117
+ console.error('Cleanup needs attention:', cleanup);
118
+ process.exitCode = 1;
119
+ }
120
+ }
121
+ ```
122
+
123
+ ```sh
124
+ node agent.mjs
125
+ ```
126
+
127
+ For Codex, prepare the image with `--agent codex`, create the VM with
128
+ `AgentVm.create(region, [{ agent: 'codex' }])`, and prompt `'codex'`.
129
+ Agent images must contain the agent you select. Agents run as UID/GID 1000
130
+ in `/workspace`; `installAccess()` installs a short-lived Bedrock token
131
+ after launch. `AgentVm` enables internet egress for model calls.
132
+
133
+ ## Next steps
134
+
135
+ For a `Sandbox` named `vm` and its `session`:
136
+
137
+ | Need | API |
138
+ | --- | --- |
139
+ | Start a task and poll or stream later | `session.run(argv)` → `ExecHandle` |
140
+ | Read live output as byte streams | `session.spawn(argv)` → `ExecProcess` |
141
+ | Keep the VM awake while an exec runs | `await session.keepAwake({ whileBusy: true })` → `KeepAwake` |
142
+ | Upload input or download results | `session.uploadFile(path, bytes)`, `session.downloadFile(path)` |
143
+ | Sync a directory in, bring files back | `session.syncDir(localDir)`, `session.downloadDir(path, localDir, ['**'])` |
144
+ | Move a tar archive as bytes | `session.uploadTar(path, tarBytes)`, `session.downloadTar(path)` |
145
+ | Freeze and restore a workspace | `vm.suspend()`, `vm.resume()` |
146
+
147
+ `runSync` returns a Promise: it starts a command, waits, and acknowledges
148
+ its saved output. A nonzero exit is a result, so check `result.ok` or
149
+ `result.exitCode`. Use `{ shell: true }` when passing a shell script string.
150
+ Async library errors expose their `ERR_*` code through `error.cause.message`,
151
+ and `isRetryable(error)` says whether an identical retry could succeed.
152
+
153
+ Omitting `egress` does not block outbound traffic. For no egress, use
154
+ `egressNetworkConnectors: [vpcConnectorArn]` with a VPC without an internet
155
+ gateway, NAT gateway, or other internet route. `denyEgress` sets advisory proxy variables that
156
+ workloads can bypass. Keep the guest execution role limited to the task's needs.
157
+
158
+ [SDK tutorial](https://laithalsaadoon.github.io/microvms-agentd/learn/tutorial/from-code/)
159
+ · [API reference](https://laithalsaadoon.github.io/microvms-agentd/reference/public-api/)
160
+ · [Source](https://github.com/laithalsaadoon/microvms-agentd) · Apache-2.0