Contents
- 1. The basic approach
- 2. 1. Set the working boundary first
- 3. 2. Use scripts for deterministic checks
- 4. 3. Put infrastructure rules in tfpolicy
- 5. 4. Check what the Plan changes, not only whether it is compliant
- 6. 5. Let AI change code, not production directly
- 7. 6. Keep runtime checks running after Apply
- 8. 7. Let AI draft policy, but not approve it
- 9. Rollout order
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?
The basic approach
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"]
1. Set the working boundary first
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.mdas 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.
2. Use scripts for deterministic checks
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 -checkfor formatting. - Run
terraform validatefor 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.
3. Put infrastructure rules in tfpolicy
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.
4. Check what the Plan changes, not only whether it is compliant
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
jqto 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
-targetas 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.
5. Let AI change code, not production directly
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.
6. Keep runtime checks running after Apply
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.
7. Let AI draft policy, but not approve it
AI can help with governance rules as well as application code.
It can draft tfpolicy from platform standards, generate tests, or assist with migrating existing Sentinel or OPA rules. That removes a fair amount of repetitive work.
Policy, CI workflows, CODEOWNERS, and validation scripts are all governance configuration. They should not change inside the application-code 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.
Rules can be wrong, and tests can miss important cases. AI can draft and revise them, but a person still decides whether a rule is reasonable or weakens an existing requirement.
Rollout order
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![]()
