This guide covers secure AI sandboxes with production-focused examples and trade-offs.
If your coding agent can shell out on a shared laptop or a long-lived runner, you do not have a sandbox — you have a future incident. Run agent tools inside ephemeral ECS tasks: no long-lived credentials, disk quotas, egress allowlists, and hard TTLs so a compromised session cannot reach production data stores.⚡ TL;DR: Spawn Fargate/ECS tasks per agent session; inject short-lived task roles; block IMDS where possible; allowlist egress; destroy the task on idle. Mirror patterns from LLM coding agents in Lambda sandboxes and Claude Code risky shell hooks.
Ephemeral task per session — secure AI sandboxes
// sandbox_launcher.ts
import { ECSClient, RunTaskCommand, StopTaskCommand } from "@aws-sdk/client-ecs";
export async function startAgentSandbox(sessionId: string) {
const ecs = new ECSClient({});
const out = await ecs.send(
new RunTaskCommand({
cluster: process.env.SANDBOX_CLUSTER!,
taskDefinition: "agent-sandbox:47",
launchType: "FARGATE",
networkConfiguration: {
awsvpcConfiguration: {
subnets: process.env.SANDBOX_SUBNETS!.split(","),
securityGroups: [process.env.SANDBOX_SG!],
assignPublicIp: "DISABLED", // ✅ private only
},
},
overrides: {
containerOverrides: [
{
name: "agent",
environment: [
{ name: "SESSION_ID", value: sessionId },
{ name: "MAX_RUNTIME_SEC", value: "1800" },
],
},
],
// Task role is session-scoped via intermediate assume-role if needed
},
tags: [{ key: "sessionId", value: sessionId }],
})
);
return out.tasks?.[0]?.taskArn!;
}
// ❌ Reuse one fat EC2 instance for all tenants' agents
❌ Shared Docker socket on a CI box with prod kubeconfig mounted.
Credentials, disk, egress
{
"family": "agent-sandbox",
"taskRoleArn": "arn:aws:iam::123:role/agent-sandbox-session",
"ephemeralStorage": { "sizeInGiB": 20 },
"containerDefinitions": [{
"name": "agent",
"linuxParameters": {
"capabilities": { "drop": ["ALL"] },
"readonlyRootFilesystem": true
},
"environment": [
{ "name": "HTTP_PROXY", "value": "http://egress-proxy.internal:3128" }
]
}]
}
# IAM — illustrative least privilege for sandbox role
# ✅ S3 read on session bucket prefix only
# ✅ ECR pull
# ❌ s3:* on *
# ❌ iam:CreateAccessKey
# ❌ sts:AssumeRole on prod account roles
Kill switches and audit
# watchdog.py
def enforce_ttl(task_arn: str, started_at: float, max_sec: int = 1800):
if time.time() - started_at > max_sec:
stop_task(task_arn, reason="ttl_exceeded") # ✅ hard stop
emit_metric("SandboxTtlKill", 1)
# Log every tool: command hash, exit code, bytes written — no secret values
| Control | Purpose |
|---|---|
| Task TTL | Bound blast window |
| Disk quota / readonly root | Stop crypto-miner scratch |
| Egress allowlist | Block exfil |
| Session task role | No long-lived keys on disk |
| CloudTrail + exec logs | Forensics |
Closing checklist
✅ Dos– ✅ One ECS/Fargate task per agent session
– ✅ Short-lived roles + private subnets
– ✅ Egress proxy allowlists
– ✅ TTL watchdog and idle stop
– ✅ Drop Linux caps; readonly root where feasible ❌ Don’ts
– ❌ Don’t mount prod kubeconfigs or cloud admin keys
– ❌ Don’t share sandboxes across tenants
– ❌ Don’t allow unrestricted outbound curl
– ❌ Don’t skip idempotency if tools write
– ❌ Don’t treat “Docker on the laptop” as production isolation
Related reading
- LLM Coding Agents: Lambda Sandboxes
- Claude Code Hooks: Gate Risky Shell Commands Before CI Runs
- AI Coding in VPC: Private Bedrock Endpoints and Secret Hygiene
- Bedrock Agents: Idempotent Tool Calls Against DynamoDB Writes
Related: CodeBuild Spec Sandboxes for AI Coding Agents: Ephemeral Builds Without ECS Sprawl · AI Coding Agent Tool Schemas: Strict JSON Contracts That Survive Retries · Building Production AI Agents: Tool Calling, Memory, and Orchestration That Actually Work
Related: Policy-as-Code for AI: OPA Checks Before Every Shell Execution · Agent Tool Allowlists: Least Privilege for Filesystem and Shell Access · Day 70: Project: Secure Coding-Agent Sandbox on ECS Fargate
Watch: 100 Days of AI on YouTube
Short videos from CheatCoders, one AI engineering topic per day.
Most viewed
- Python Decorators Explained: From Simple Wrappers to Production Patterns
- Claude Code vs Cursor vs GitHub Copilot: Choose by Workflow, and Set Up One Repo That Works With All Three
- AI Agent Frameworks in 2025: LangGraph vs CrewAI vs AutoGen vs Raw API
- Python String Methods: Every str Method With Real Production Examples
- Css Animation Performance: Performance-Optimized Motion for Modern UIs
Newly added
- AWS Systems Manager Parameter Store: Hierarchical Runtime Config for Coding Agents
- Amazon Cognito Identity Pools: Federate Coding-Agent Tool Callers Without Long-Lived Keys
- Amazon Athena for Coding Agents: Query Tool Audit Logs and Eval Traces on S3 Without a Warehouse
- AWS Batch for Coding Agents: Parallel Overnight PR Reviews and Long-Running Jobs Without Lambda Timeouts
- Amazon Bedrock Prompt Management: Version and A/B Test Coding-Agent System Prompts
Deep-dive PDF
Get the expanded guide for this post — extra diagrams-style checklists, failure modes, and a production walkthrough. Free when you subscribe to CheatCoders.
Already subscribed? or open the subscribe page.
Discover more from CheatCoders
Subscribe to get the latest posts sent to your email.

Pingback: Lambda Warm Pools: Low-Latency Backends for Coding Agent Tools - CheatCoders
Pingback: Ephemeral Debug Sidecars: Audited Access Without Permanent Prod Shells - CheatCoders