Running agent-generated code in AWS Lambda functions vs a sandbox
AWS Lambda is a service for running code in functions without managing servers directly. AWS runs the functions on Firecracker, scales them, and charges per request and per GB-second of duration. Using Lambda for an AI agent means the agent sends code it generated to a function, which executes that code and returns a result. For example, an agent might generate a short calculation or data transformation. A standard function can run such snippets within its time, memory, and disk limits. But the code may come from untrusted input, so its access to files, credentials, and other resources matters as much as whether it completes successfully. Lambda freezes an execution environment after an invocation and may reuse it later. That helps ordinary applications keep caches and connections, but can let one user’s code affect a later user’s run. The article describes mitigations, but says they require care. A per-run sandbox offers a different choice: create, use, and stop a separate microVM when isolation between tool calls matters.
What is AWS Lambda, and what does it mean to use it to run code an AI agent generated?
AWS Lambda is a service for running code in functions without managing servers directly. AWS runs the functions on Firecracker, scales them, and charges per request and per GB-second of duration. Using Lambda for an AI agent means the agent sends code it generated to a function, which executes that code and returns a result.
For example, an agent might generate a short calculation or data transformation. A standard function can run such snippets within its time, memory, and disk limits. But the code may come from untrusted input, so its access to files, credentials, and other resources matters as much as whether it completes successfully.
Lambda freezes an execution environment after an invocation and may reuse it later. That helps ordinary applications keep caches and connections, but can let one user’s code affect a later user’s run. The article describes mitigations, but says they require care. A per-run sandbox offers a different choice: create, use, and stop a separate microVM when isolation between tool calls matters.
How much time, memory, and temporary disk space can a standard Lambda function use?
Standard Lambda functions have a default timeout of 3 seconds. The maximum is 1,800 seconds, or 30 minutes. These limits matter when deciding whether a tool call can finish within one invocation or needs a different execution setup.
A function can be configured with 128 MB to 10,240 MB of memory, in 1 MB steps. AWS allocates CPU in proportion to memory; at 1,769 MB, a function has the equivalent of one vCPU. Each execution environment also has a /tmp directory, with configurable space from 512 MB to 10,240 MB.
These ranges can be comfortable for short, CPU-light tasks, such as a calculation or small data transformation. They are limits for standard functions, not a promise that every workload will be suitable. The article’s contrast is that sandbox runs can last longer: their timeout has a 3,600-second server default and a maximum of 86,400 seconds, within the plan.
What can go wrong if code from different users runs in the same reused Lambda environment?
Lambda freezes an execution environment after an invocation and may thaw it for a later invocation. The environment, rather than the individual invocation, is therefore the isolation unit for code that runs there. Successive invocations of the same function can share that environment, and you do not control which invocations do.
Suppose Alice’s agent code writes a file to /tmp or starts a background process. If Bob’s invocation reuses that environment, his code may find the file, or the unfinished process may resume while his invocation runs. Code that changed global runtime state can also leave that change behind.
This reuse is useful for ordinary functions because initialized objects, caches, and connections can survive between calls. For untrusted agent code, it creates cross-run risks. The article recommends mitigations such as child processes and tenant-specific functions, but notes that process cleanup can miss code that writes elsewhere or forks beyond the process group. Isolation needs deliberate design.
What safeguards can reduce the risks of running untrusted code in Lambda, and what risks may remain?
Safeguards aim to limit what untrusted code can reach and what it can leave behind. One approach is to run it in a child process with its own temporary directory, then kill the whole process group and delete that directory. Another is to use one function per tenant, so environment reuse stays within a tenant.
The article gives a concrete warning: a child process can still write outside its temporary directory, and a forked process may escape the process group. Separately, Lambda’s execution role is available to code through standard AWS credential mechanisms. The role should grant nothing, and environment variables should contain no secrets. VPC configuration and security groups can restrict egress.
These measures are possible, but together they require infrastructure and careful setup. They reduce exposure; the article does not claim they remove every risk. For a tool call needing stronger per-run boundaries, it contrasts Lambda with a sandbox that is created, used, and stopped, with no role attached and no network by default unless an allowlist is requested.
How does a per-run sandbox handle isolation, saved state, credentials, and network access differently from Lambda?
The article describes Sandscale as a per-run option: each sandbox is its own Firecracker microVM that you create, use, and stop. You choose whether the isolation unit is a tool call, task, or conversation. Two tasks share a sandbox only if your code gives them the same sandbox ID.
If a conversation needs files to persist between turns, you can keep its sandbox and pause it. Resuming brings back memory and disk; stopping deletes the disk. Unlike Lambda’s execution role, a sandbox has no role attached. If a run needs a secret, Vault provides named values to that sandbox’s environment. Network access defaults to none unless an organization’s allowlist is requested.
This makes state and access explicit choices rather than relying on environment reuse. Sandboxes also offer longer timeouts: a 3,600-second server default and a maximum of 86,400 seconds, within the plan. Each command has its own limit, defaulting to 60 seconds, which can be set per run.
When might Lambda be cheaper or more convenient than a sandbox, and when might a sandbox be the better fit?
Lambda can be a convenient fit when an application already runs on AWS and its agent needs to execute short, CPU-light snippets. The article gives calculations and small data transformations as examples. Lambda scales on its own, and its per-request and per-GB-second pricing suits many short invocations.
A sandbox may fit better when untrusted runs need an isolation boundary you choose. You can use one sandbox per tool call, task, or conversation; keep it paused to preserve state; or stop it to delete its disk. The article also describes no attached role and no network access by default, plus longer possible timeouts than standard Lambda.
The decision depends on the workload and the safeguards you need. Lambda reuse can require process cleanup, tenant-specific functions, restricted credentials, and egress controls. A sandbox returns an exit code, output, and timeout or memory-kill indicators. The article does not provide a price comparison, so it cannot establish which option is cheaper for a particular workload.
What is a microVM, and how does virtualization help isolate one program from another?
A microVM is a small virtual machine. The article identifies Firecracker as the technology Lambda runs on and says each Sandscale sandbox is its own Firecracker microVM. It does not give a technical definition of microVMs or detail their internal design.
In general, virtualization creates separate machine environments on shared physical computing hardware. A program running in one virtual machine uses that environment’s own memory and disk state, rather than simply sharing another program’s process state. This separation can help contain changes and reduce accidental interaction between programs.
For the article’s use case, the practical point is the chosen isolation unit. Sandscale creates, uses, and stops a sandbox, and nothing is reused unless the user chooses to reuse it. Lambda, by contrast, may reuse an execution environment across invocations. Virtualization is the mechanism behind the separate microVM, while the decision to use one sandbox per tool call, task, or conversation determines which runs share it.
This brief was written by AI from the original reporting and checked by other models. Names, figures and quotes come from the source; read it for full context.
Read more in the JupiteX app
Pulse is free. New stories every 4 hours, each one broken into the questions that explain it.
Or read more news on the web