cd /news/ai-agents/ai-driven-terraform-move-fast-withou… · home › topics › ai-agents › article
[ARTICLE · art-177141] src=newbmiao.github.io ↗ pub= topic=ai-agents verified=true sentiment=· neutral

AI-Driven Terraform: Move Fast Without Losing Control

A practitioner guide recommends constraining AI-written Terraform to a controlled branch with scripted checks — terraform fmt -check, terraform validate, TFLint, Checkov and custom scope scripts — before human approval and an independent CI/CD identity runs Apply. The guide advises against granting AI production Apply, Destroy or state-write permissions and against treating a prompt or AGENTS.md file as an authorization system. It argues that as AI-generated infrastructure-as-code volume grows, manual line-by-line review stops scaling and handing review to another AI adds an opinion rather than a guarantee.

recommendation

Give AI a limited identity and controlled branch with no Apply, Destroy or state-write rights, and block edits to CI workflows, CODEOWNERS and policy files by default.

more in playbook: Securing agents and AI tools
read8 min views8 publishedNov 27, 2026

Having AI write IaC is no longer unusual. It can add a module, change a few resources, and draft policy much faster than a person.

That speed brings a different problem. A change can pull in unrelated resources. Terraform that is perfectly valid can still delete or replace resources, or loosen permissions. Those issues are not always obvious in code review.

As the volume grows, reviewing every line by hand stops scaling. Handing the review to another AI adds another opinion, not another guarantee.

The real question is this: how can AI keep writing and iterating while its mistakes are caught consistently and kept out of production?

#

I reduce it to three steps: define what AI may change, verify the result with scripts and policy, then leave production writes to human approval and CI/CD.

Within that scope, AI can generate code, make small commits, and revise its work from concrete errors. Scripts check formatting, syntax, change scope, and plan risk. Policy checks infrastructure rules.

Repository changes and production writes follow separate paths. AI may change code on a controlled branch. A person approves merges, live policy changes, and production changes; CI/CD performs the approved operation.

flowchart TD
    A["Person defines task, permissions, and resource scope"] --> B["AI makes small commits on a controlled branch"]
    B --> C["Scripts check commits and scope"]
    C --> D["fmt / validate / lint"]
    D --> E["Terraform Plan"]
    E --> F["tfpolicy and plan checks"]
    F --> G{"Checks pass?"}
    G -- No --> H["Return concrete errors"]
    H --> B
    G -- Yes --> I["Save plan and check report"]
    I --> J["Human review and approval"]
    J --> K["Independent CI/CD identity runs Apply"]
    K --> L["tfpolicy Apply-phase checks"]
    L --> M["Azure Policy / runtime checks"]
    M --> N["Monitoring, drift checks, and rule updates"]

#

AI does not need to be limited to a few lines, but every task needs a clear scope.

If the task is to adjust one database connection, it should not also change unrelated resources or refactor a module.

Do

  • Define the files, modules, and resources that may change before work starts.
  • Use a separate identity with limited Git, cloud, and Terraform Enterprise permissions.
  • Run Plan in an isolated runner with only the read access to cloud resources and state that Plan requires.
  • Block changes to CI workflows, CODEOWNERS, validation scripts, and active policy by default.
  • Split the work into small commits, with one purpose per commit, so each step is easy to review and revert.
  • Compare the base commit with the current branch in a script, and check every commit against file and resource allowlists.
  • If the task genuinely needs a wider scope, get human approval before updating that scope.

Don’t

  • Do not treat a prompt or AGENTS.md as an authorization system.
  • Do not give AI production Apply, Destroy, or state-write permissions.
  • Do not provide full production cloud credentials just to generate a Plan.
  • Do not pack unrelated changes into one large commit.
  • Do not open an entire stack because one task needs an extra permission.

#

AI can generate code. Basic checks do not need another model judgment.

These checks do not require a model to understand the business context, and their result should not depend on how the model interprets them.

Do

  • Run terraform fmt -check for formatting.
  • Run terraform validate for configuration validity.
  • Use TFLint for common Terraform configuration problems.
  • Use Checkov for additional security scanning.
  • Use custom scripts for file, naming, version, and resource-scope checks.
  • Give every critical check a clear success or failure result, and script anything that can be scripted.

Don’t

  • Do not replace tool output with AI’s own review.
  • Do not let AI skip a failed check or reinterpret it as acceptable.
  • Do not use different standards locally and in CI.

Local checks give AI a quick feedback loop. CI provides the final gate. Reusing the same scripts and rules in both places avoids changes that pass locally and fail under a different standard after submission.

#

Formatting and syntax only cover the code layer. Policy should manage the rules that govern infrastructure security and platform standards.

If the current environment supports tfpolicy, existing rules can move over gradually.

Do

  • Restrict provider and module sources and versions during Setup.
  • Check resource attributes, networking, security, IAM, and tags during Plan.
  • Check relationships between resources, such as whether a Storage Account has a Private Endpoint.
  • Use the Apply phase for attributes that are known only after resource creation.
  • Add both passing and failing tests for important rules.

Don’t

  • Do not leave standards only in prompts or a wiki.
  • Do not treat tfpolicy Apply-phase checks as continuous runtime monitoring.
  • Do not change mandatory policy inside an application Terraform PR.
  • Do not remove useful security scanners merely to standardize on one tool.

The toolset can converge over time, but rule migration still needs semantic equivalence. Merge duplicate rules and keep checks that tfpolicy does not yet cover.

#

tfpolicy can tell whether a resource follows platform rules. It does not necessarily know what the original ticket intended to change.

For example, a ticket may only update a Storage Account tag while the Plan also replaces a database. The result might comply with resource policy and still be outside the task.

A script can cover that gap.

Do

  • Export the Plan with terraform show -json .
  • Use jq to extract resource addresses and actions.
  • Compare the actual changes with the resource scope defined for the task.
  • Block Delete by default for ordinary change requests.
  • Require additional approval for Replace.
  • Fail on out-of-scope resource changes and return the reason.

Don’t

  • Do not review only an AI-generated Plan summary.
  • Do not allow the entire stack by default.
  • Do not treat -target as an authorization boundary or routine option.

Reserve -target for recovery and a small number of exceptional cases. It does not replace change validation. Run a complete Plan afterward to check for missed dependencies and other changes.

#

The earlier checks establish whether the code and proposed changes match the task. The next question is who may actually change the infrastructure.

Separate code changes, production authorization, and production execution: AI changes a controlled branch, a person approves a specific commit and Plan, and CI/CD performs the production write.

Do

  • Let AI generate code on a controlled branch and run checks and Plan in an isolated runner.
  • Have CI save the Plan and check report for the specific commit.
  • Have a person review the diff, Plan, and report before approval.
  • Use an independent CI/CD identity to execute the approved Plan, for example terraform apply tfplan .
  • Apply the same Plan that was reviewed and approved.

Don’t

  • Do not give AI Merge, Apply, Destroy, or state-write permissions.
  • Do not let AI approve its own changes.
  • Do not let AI modify CI workflows, approval rules, or other release conditions.
  • Do not regenerate a different Plan after approval and apply unreviewed content.

AI can iterate quickly at the code layer while every write to the cloud environment still passes through an independent process.

#

tfpolicy Apply-phase checks end with the Terraform run. Continuous compliance, resource health, and drift then move to Azure Policy and monitoring systems.

Do

  • Use Azure Policy or equivalent cloud controls for continuous compliance checks.
  • Run Terraform Plan on a schedule to detect drift.
  • Monitor resource health, security risk, and cost changes.
  • Feed production findings back into modules, scripts, and policy.

Don’t

  • Do not run Apply automatically whenever drift appears.
  • Do not overwrite legitimate emergency or security-team changes.
  • Do not look only at whether the pipeline succeeded.

When drift appears, identify its source and impact before deciding whether to accept, repair, or revert it. Automatic detection is useful; automatic overwrite is not always appropriate.

#

AI can help write governance rules, not just application code.

It can turn platform standards into tfpolicy, generate tests, or help migrate existing Sentinel or OPA rules. It can also identify missing guardrails. When a recurring review finding or production incident reveals a repeated failure mode, AI can propose a new policy, validation check, or regression test.

A proposal is not an active rule. It must include passing and failing cases, be reviewed by platform or security owners, and merge through a dedicated governance PR. This creates a feedback loop from engineering practice and past failures, while people still decide what becomes mandatory.

Policy, CI workflows, CODEOWNERS, and validation scripts are all governance configuration. They should not change inside the application PR they are meant to evaluate.

Do

  • Submit application Terraform and policy in separate PRs.
  • Include positive and negative tests with policy changes.
  • Use CODEOWNERS to require review from platform or security engineers.
  • Put CI workflow, CODEOWNERS, and validation-script changes through separate approval.
  • Recheck affected application PRs after the rule change is merged.

Don’t

  • Do not let AI merge a policy PR automatically.
  • Do not let AI approve its own rule or exception request.
  • Do not let an application PR change its own release conditions.
  • Do not assume green tests prove that a rule is correct.

Passing tests show that a rule matches its test cases, not that the rule itself is correct. AI can draft and revise rules, but a person still decides whether they are appropriate or weaken an existing requirement.

#

Start by removing AI’s production write access and constraining file and resource scope. Then add fmt, validate, lint, commit, scope, and Plan checks. Move existing infrastructure standards to tfpolicy over time.

Once that path is stable, let AI assist with policy authoring. It can explore freely inside the boundary; actions beyond the code boundary still need human approval and ownership.

Questions? Leave a comment below or email:newbvirgil@gmail.com

── more in #ai-agents 4 stories · sorted by recency
── more on @terraform 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/ai-driven-terraform-…] indexed:0 read:8min 2026-11-27 · —