Why AI Should Never Go Straight to terraform apply
Published: 2026-03-12
URL: https://sumantthakur.substack.com/p/how-devops-agents-should-actually
A Tweet started circulating widely among engineers last week. It claimed that an AI coding agent had executed a Terraform command that wiped production infrastructure. Databases disappeared. Services went offline. Years of data were reportedly lost. Whether every detail of the story is accurate is almost beside the point. Because every engineer who saw it had the same thought: What happens when an AI agent runs the wrong command in production?
Most discussions about incidents like this focus on the model. Was it Claude? GPT? A bad prompt? But the real issue isn’t the model. The real issue is architecture. Most agent systems today still look like this: Prompt ↓ Agent reasoning ↓ Execute command That works fine for demos.
But when the command becomes terraform apply, the consequences change dramatically. If agents are going to operate real infrastructure, they need the same safety layers we already use in modern DevOps pipelines. Which leads to a simple rule: An agent should never go directly from intent to execution.
Instead, the system should move through a controlled sequence of stages. PR ↓ Plan ↓ Validate ↓ Approve ↓ Execute This article walks through a reference architecture for DevOps agents built around that idea. Thanks for reading Autonomous AI Architect! Subscribe for free to receive new posts and support my work.
The Core Principle Infrastructure automation should be progressively constrained. Each stage in the system reduces risk and increases visibility. Instead of letting the agent immediately act on production systems, the architecture forces it to produce artifacts that can be inspected, validated, and approved.
Think about how deployment pipelines evolved. We moved from: ssh into server run deploy script to modern CI/CD systems: commit ↓ build ↓ test ↓ deploy Agent systems need a similar progression. Step 1: The Trigger Every run starts with a deterministic event. Typical triggers include: GitHub pull requests Issue comments CI pipeline events scheduled automation jobs Example: A developer opens a pull request: “Add a staging database.” Instead of immediately executing infrastructure commands, the system begins a run.
The run becomes the container for everything that follows: the generated plan infrastructure code policy validation results approval decisions execution logs Every stage attaches to the same run identifier.
This single identifier becomes the backbone of traceability. Step 2: The Planner The planner agent answers one question: What needs to happen? Not how. Not with which API. Just the intended actions. Example planner output: Plan
- Create VPC for staging environment
- Deploy ECS service for application
- Create PostgreSQL database
- Configure security groups
- Attach load balancer
The planner produces a structured plan, not executable commands. This distinction is critical. Plans can be inspected, validated, and rejected before any infrastructure changes occur. Step 3: The Generator Once a plan exists, a generator agent produces the actual infrastructure configuration. Outputs might include: Terraform modules Kubernetes manifests Helm charts CloudFormation templates Example Terraform resource: resource "awsdbinstance" "stagingdb" { engine = "postgres" instanceclass = "db.t3.medium" allocated_storage = 20 } The generator’s job is translation.
It converts the plan into executable infrastructure code. Separating planning from generation dramatically improves reliability. Step 4: Policy Validation Before any execution happens, the generated configuration passes through a policy engine.
This is where governance lives. Example rules might include: Prevent deletion of production resources Require encryption for databases Block public storage buckets Limit deployment cost A simple rule might look like: deny if resource.intent == "delete" and environment == "production" If validation fails, the run stops. No infrastructure changes occur.
This stage transforms infrastructure automation from dangerous into governed. Share Step 5: The Approval Gate Even with strong policy rules, some actions are too risky to run automatically. Examples include: deleting infrastructure large cost increases architecture migrations In these cases the system pauses and asks a human operator for approval.
The operator should see: the generated plan infrastructure changes validation results estimated cost impact Then they choose one of three actions: Approve Reject Escalate This step ensures that humans remain in control of high-impact decisions. Step 6: Execution Only after all previous stages succeed does execution begin.
At this point the executor runs the deployment command. Examples include: terraform apply kubectl apply helm upgrade Execution should occur in isolated environments such as: ephemeral containers sandboxed runners dedicated deployment workers This prevents agents from directly interacting with production infrastructure outside controlled workflows. Leave a comment Observability: The Missing Layer Most agent failures are not caused by bad plans. They are caused by invisible decision chains. To operate agent systems safely, every stage must emit trace events.
Example trace sequence: trigger.created plan.generated code.generated validation.result approval.decision execution.started execution.completed These traces allow operators to answer critical questions: Why did the agent propose this plan? Which rule blocked the run?
What changed between two executions? Without observability, debugging autonomous systems becomes nearly impossible. Why This Architecture Works This model works because it mirrors patterns that already exist in modern software delivery. CI/CD pipelines rely on progressive validation: commit ↓ build ↓ test ↓ deploy Infrastructure agents simply extend this idea with planning and governance layers. Instead of one opaque decision, the system becomes a sequence of inspectable artifacts.
Each stage becomes observable, testable, and controllable. The Real Lesson The viral story about the AI agent deleting infrastructure wasn’t really about AI. It was about missing control layers. Any automation system — human or AI — can cause damage if it has direct access to production systems without safeguards. Agents simply make the problem more visible.
The solution isn’t to remove agents. The solution is to design systems around controlled autonomy. Final Thought The biggest mistake teams make with AI agents is trying to make them fully autonomous too early. Autonomy should be earned. Start with an architecture that looks like this: PR ↓ Plan ↓ Validate ↓ Approve ↓ Execute Once those control layers exist, agents can become smarter and more autonomous over time.
But if the architecture is missing, better models won’t save you.