Introduction
AI coding agents can now generate significantly more than a code snippet.
An agent can create files, install packages, execute shell commands, run tests, compile applications, inspect logs, and modify an entire project. That makes AI agents more useful, but it also creates a security problem.
The code generated by an AI model should not automatically receive the same environment, credentials, filesystem, or network access as the application that requested it.
A generated program can be incorrect by accident. It can contain a malicious dependency. It can be manipulated through prompt injection. It can execute destructive commands or attempt to access resources that were never intended to be available.
This creates a fundamental architectural requirement:
AI Agent
↓
Generated Code
↓
Isolated Execution Environment
↓
Result
AWS Lambda MicroVMs introduce a serverless execution model specifically designed for isolated, stateful workloads such as AI-generated code, coding assistants, vulnerability scanning, and interactive development environments. Lambda MicroVMs use Firecracker virtualization and provide a separate kernel, memory space, and disk state for each MicroVM.
The important engineering question is not simply whether an AI-generated program can execute.
The question is whether it can execute without becoming a security boundary failure for the rest of the system.
Why AI-Generated Code Needs Isolation
Consider an AI agent that receives this request:
Create a .NET application that reads uploaded CSV files,
calculates statistics, and produces a report.
The agent may generate:
Program
├── Source files
├── NuGet packages
├── Build commands
├── Test commands
└── Runtime process
If this code executes directly inside the application's main environment, the generated program potentially shares resources with trusted application code.
That is dangerous.
A safer architecture is:
┌──────────────────┐
│ AI Agent │
└────────┬─────────┘
│
Generated Code
│
▼
┌──────────────────┐
│ Sandbox │
│ │
│ .NET Runtime │
│ Filesystem │
│ Processes │
│ Network Policy │
└────────┬─────────┘
│
Result
The sandbox becomes the execution boundary.
What Makes a Good Code Sandbox?
A useful sandbox should provide several properties.
Isolation
Generated code should not be able to access unrelated workloads.
Resource Limits
CPU, memory, disk, and execution duration should be constrained.
Network Control
The sandbox should have explicitly controlled network access.
Ephemeral Execution
A session should be disposable when the job completes.
State Management
If an interactive coding session requires persistence, state should be preserved without sharing it with other users.
Auditing
Commands, inputs, outputs, and lifecycle events should be observable.
No single control is sufficient.
A secure design combines multiple boundaries.
Why MicroVMs Are Interesting
Traditional sandbox architectures often use processes or containers.
Those approaches can be useful, but they should not automatically be treated as equivalent security boundaries for mutually untrusted workloads.
AWS Lambda's standard execution environments already use Firecracker virtualization for workload isolation. Lambda MicroVMs expose that isolation model as a dedicated compute primitive for workloads that need isolated execution environments.
AWS describes Lambda MicroVMs as providing VM-level isolation with a separate kernel, memory space, and disk state for each environment. They are designed for use cases including AI coding assistants, vulnerability scanning, data analytics, and CI/CD workloads.
Conceptually:
User A
↓
MicroVM A
├── Kernel
├── Memory
└── Filesystem
User B
↓
MicroVM B
├── Kernel
├── Memory
└── Filesystem
The objective is to avoid making different untrusted execution sessions share the same security boundary.
A Secure AI Code Execution Architecture
A production architecture can separate the system into four layers:
┌───────────────────────────────┐
│ AI Agent / Control Plane │
└───────────────┬───────────────┘
│
│ Generated Code
▼
┌───────────────────────────────┐
│ Sandbox Launcher │
└───────────────┬───────────────┘
│
▼
┌───────────────────────────────┐
│ Lambda MicroVM │
│ │
│ Source Code │
│ Build Tools │
│ Runtime │
│ Tests │
└───────────────┬───────────────┘
│
▼
┌───────────────────────────────┐
│ Result / Logs / Artifacts │
└───────────────────────────────┘
The control plane should not execute generated code itself.
Its job is to:
Accept the task.
Create or select an isolated environment.
Transfer the required code and inputs.
Execute the requested workload.
Collect results.
Destroy or suspend the environment according to the workflow.
Record security and operational telemetry.
Package the Sandbox as an Image
Lambda MicroVMs can be created from a container-based image. AWS documents support for applications packaged for Amazon Linux 2023, with MicroVM configurations supporting up to 32 GB of memory and 16 vCPUs.
For a .NET sandbox, the image might contain:
Amazon Linux
↓
.NET Runtime / SDK
↓
Build Tools
↓
Test Tools
↓
Sandbox Application
The image should contain only the tools required by the workload.
Avoid turning the sandbox image into a general-purpose development environment containing unnecessary credentials, utilities, or network clients.
Do Not Put Secrets in the Sandbox
One of the most important rules is simple:
Generated code should not automatically receive production credentials.
Avoid placing credentials in:
Environment Variables
Configuration Files
Source Code
Container Images
Local Files
Instead, use explicit authorization boundaries.
If generated code needs to access a service, provide only the capability required for that operation.
For example:
AI Agent
↓
Needs database metadata
↓
Controlled API
↓
Database
is safer than:
AI Agent
↓
Full Database Credentials
Least privilege is particularly important because generated code cannot be assumed to understand the security implications of every action it performs.
Control Network Access
Network access deserves special attention.
An unrestricted sandbox could potentially execute:
HTTP requests
DNS lookups
Package downloads
Cloud API calls
Internal service requests
That creates an opportunity for data exfiltration or unauthorized access.
A safer architecture starts with:
Default
↓
No Network
and enables only the connections required by the workload.
For workloads that need access to internal resources, AWS documents VPC egress options for Lambda MicroVMs.
For example:
MicroVM
│
├── Public Internet: DENY
│
├── Internal API: ALLOW
│
└── Database: ALLOW
Network policy should be part of the sandbox design rather than an afterthought.
Control Authentication to the MicroVM
The sandbox itself should not become an unauthenticated execution endpoint.
AWS Lambda MicroVMs use authentication tokens for inbound access, and those tokens can be scoped to specific ports. AWS recommends short-lived tokens and limiting token scope to the ports actually required by the application.
Conceptually:
Client
↓
Short-Lived Token
↓
Specific MicroVM
↓
Specific Port
This is substantially better than exposing a sandbox endpoint without authentication.
Treat AI Output as Untrusted Input
An important architectural mistake is assuming that code generated by a trusted AI model is trusted code.
It is not.
AI-generated code can contain:
Unexpected shell commands
Unsafe dependencies
Credential access
Network requests
Destructive filesystem operations
Infinite loops
Resource-intensive operations
The model may also be influenced by malicious instructions contained in files, repositories, documentation, or external content.
Therefore:
AI Output
↓
UNTRUSTED
↓
Sandbox
should be the default security model.
Limit CPU and Memory
Generated code can accidentally consume excessive resources.
For example:
while (true)
{
var data = new byte[1024 * 1024];
}
could consume significant resources.
A sandbox should therefore be sized deliberately.
AWS recommends right-sizing MicroVM resources and notes that bandwidth scales with configured memory.
A practical policy might look like:
Small Task
CPU: Low
Memory: 1–2 GB
Disk: Limited
Time: Short
Large Build
CPU: Higher
Memory: Larger
Disk: Larger
Time: Controlled

Join the conversation! Your thoughts help the community grow.