By NHI Mgmt Group Editorial TeamBased on Sonar: “AI is writing more of your Terraform” (June 18, 2026)

TL;DR: AI-generated Terraform repeatedly fails in four patterns, including permissive defaults, missing security blocks, hardcoded values, and stale provider syntax, and peer-reviewed benchmarks from NeurIPS 2024, FSE 2026, and ICSE 2026 show frontier LLMs still struggle with real IaC tasks. Sonar argues that syntax checks are not enough, because the control gap is semantic security analysis before misconfigurations reach production.


At a glance

What this is: Sonar argues that AI-generated Terraform commonly ships insecure or outdated patterns that validate and plan will not catch.

Why it matters: This matters because IAM, secrets, and network exposure mistakes can enter cloud environments through infrastructure code unless teams analyse intent and posture before merge.

By the numbers:

  • frontier LLMs pass real-world Terraform tasks at rates below 40% in the benchmarks Sonar cites
  • the same Sonar survey says 42% of code committed today is written or assisted by an AI agent
  • that share is expected to grow to 65% by 2027

Context

Infrastructure as code is now being produced by the same AI systems that draft application code, which changes the review problem from syntax checking to governance of intent, access, and security posture. Terraform is especially sensitive because small changes in provider syntax, IAM scope, encryption settings, and logging blocks can create materially different risk outcomes even when the file still parses cleanly.

The practical gap is that existing pipeline checks are designed to prove that configuration is valid, not that it is secure. For identity and cloud teams, that means the real control question is whether AI-generated resources respect least privilege, secret handling, and environment boundaries before they are merged into production.

Sonar's article is a good example of a broader pattern: AI-generated infrastructure often looks complete but omits or distorts the parts that security teams rely on most. That makes this a cloud governance problem with a clear identity angle wherever Terraform is used to define IAM policies, service credentials, or access paths.


Key questions

Q: What fails when AI-generated Terraform still passes validate and plan?

A: The failure is not parsing, it is security intent. validate and plan can accept Terraform that is syntactically correct but still grants wildcard access, omits encryption, or inlines secrets. That means teams need posture-aware checks that evaluate whether the configuration matches governance requirements before it reaches production.

Q: When should teams prioritise semantic Terraform analysis over basic pipeline checks?

A: Prioritise semantic analysis whenever Terraform defines IAM, network exposure, encryption, logging, or secrets handling. Those are the cases where a file can be valid yet still create unacceptable risk. If AI is generating the code, the need becomes more urgent because the same model can repeat the same unsafe pattern at scale.

Q: What are the signs that AI-generated infrastructure code is drifting into unsafe patterns?

A: Common signals include wildcard permissions, missing security blocks, hardcoded values, and stale resource syntax that reflects an older provider version. If those patterns appear repeatedly, the problem is not isolated developer error. It is a generation and review control gap that needs policy enforcement before merge.

Q: How should security teams govern AI-generated Terraform that touches IAM and secrets?

A: Treat it as identity-sensitive infrastructure code. Require scope checks for IAM policies, reject inline credentials, and enforce provider-version-aware review so generated code cannot silently expand access or preserve old syntax. This is a governance problem as much as an engineering one, because access decisions are being made in code.


Technical breakdown

Why terraform validate and plan miss semantic security errors

terraform validate checks whether Terraform syntax is internally consistent, and terraform plan checks the delta the provider expects to apply. Neither step reasons about whether the intended resource is secure, least privilege, or complete from a governance perspective. That is why a policy with Action = "*", a storage account without encryption, or a database without logging can still pass. AI code generation amplifies this gap because the model can produce structures that look plausible while leaving out the security semantics the organisation expected.

Practical implication: Treat validation and planning as correctness checks, not security gates.

How AI-generated Terraform drifts into unsafe IAM and exposure patterns

LLMs often optimise for immediate completion, not policy intent. In Terraform, that shows up as wildcard permissions, public exposure defaults, missing encryption or logging blocks, and literal values where variables or secret references should exist. The problem is not only that the output is wrong, but that it is wrong in ways that preserve deployability while expanding blast radius. That makes the review burden asymmetric: the dangerous parts are the ones least likely to fail fast.

Practical implication: Inspect Terraform for privilege scope, exposure, and secret handling before merge.

Why stale provider syntax creates hidden risk in infrastructure code

Terraform provider schemas evolve faster than model training data. When an AI assistant emits deprecated attributes or old resource patterns, the code may reflect an earlier provider version and fail only after it is committed or deployed. This is especially dangerous in cloud estates where teams assume provider drift is caught by tooling. In practice, drift survives because the model learned from old examples and the pipeline only proves that the file matches a schema, not that the schema is current.

Practical implication: Add provider-version-aware review and rule sets to catch deprecated patterns early.


Threat narrative

Attacker objective: The attacker objective is to exploit AI-shaped misconfiguration paths to gain access, expose data, or preserve credentials in infrastructure code.

  1. Entry occurs when an AI assistant generates Terraform with permissive IAM, missing encryption, or hardcoded values that appear valid during authoring.
  2. Credential or configuration abuse follows when those literals, wildcard permissions, or public endpoints are merged into infrastructure code and consumed by deployment pipelines.
  3. Impact is insecure cloud posture, including broader access than intended, exposed data paths, and credentials that persist in source history or deployed resources.

Read and download The State of NHI & AI Agent Breach Report 2026, covering 200+ breaches impacting Non-Human Identities including AI Agents.


NHI Mgmt Group analysis

Semantic security analysis is now a prerequisite for AI-generated infrastructure. Terraform validation was never designed to judge whether a configuration is safe, only whether it is structurally acceptable. Once AI agents begin drafting infrastructure at scale, the decisive control shifts from parse-time correctness to intent-aware review, because the dangerous failure is a valid but insecure configuration.

AI-generated IaC creates a new kind of governance debt: security assumptions become invisible in code review. Missing encryption blocks, wildcard IAM, and literal secrets are not random mistakes, they are signs that the model has not been given enough boundary context to preserve the organisation's security intent. That means cloud and identity teams need review controls that test scope, not just syntax.

Terraform misconfiguration now sits at the intersection of cloud security and identity governance. When infrastructure code defines IAM policy, secret references, or access to production services, every generation error becomes an access control problem as well as an IaC problem. The right mental model is that AI is accelerating configuration production faster than traditional pipeline checks can meaningfully interpret it.

Stale schema output is a provider-governance issue, not just a coding quality issue. AI assistants can reproduce old Terraform patterns long after the provider has changed, which means teams are carrying training-data lag into live cloud controls. Practitioners should treat version-aware policy enforcement as part of the security architecture, not as an optional code hygiene step.

Wildcard access remains the most visible symptom of model-driven privilege drift. The pattern is familiar, but AI generation makes it easier to produce at scale and harder to notice before merge. For security leaders, the lesson is to govern the generation path as tightly as the deployment path, because the blast radius is decided earlier than most teams assume.

From our research library:

What this signals

Semantic IaC review is becoming a governance control, not a coding convenience. As AI agents generate more Terraform, teams need controls that inspect privilege, exposure, and secret handling before merge. The organisations that rely on syntax checks alone will keep discovering risk only after the cloud change has already been approved.

AI-generated configuration produces privilege drift faster than manual review can absorb. The issue is not only bad code, but bad code that looks deployable. That is why review workflows need to shift left into authoring, especially where Terraform creates IAM policy, secret references, and environment access paths.

Provider drift and secret exposure belong in the same control conversation. A model that can still emit obsolete resource patterns can also produce inline credentials or overbroad access. Teams should treat generated infrastructure as an identity and cloud governance surface, not just as code quality data.


For practitioners

  • Add semantic IaC checks before merge Use policy and posture analysis that evaluates what Terraform means, not just whether it parses, especially for IAM, encryption, and logging blocks.
  • Block wildcard IAM and public exposure defaults Create review rules that flag Action = "*", public ACLs, and security groups open to 0.0.0.0/0 before those patterns reach a deployment pipeline.
  • Require secret references instead of literals Reject inline passwords, tokens, and other hardcoded credentials in .tf and .tfvars files, and force secret manager references for all sensitive values.
  • Version-gate provider syntax in review Compare generated Terraform against the active provider schema so deprecated attributes and removed resource patterns fail before they are merged.
  • Extend identity reviews to infrastructure code Make IAM policy scope, service access paths, and workload permissions part of the same review path as application code when AI is generating Terraform.

Key takeaways

  • AI-generated Terraform can be syntactically valid and still encode risky access, exposure, or secret-handling decisions.
  • Benchmark results cited by Sonar show that frontier LLMs still struggle to produce reliable Terraform for real-world tasks.
  • The most effective control is semantic analysis before merge, especially for IAM, encryption, logging, and provider-version drift.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP Non-Human Identity Top 10 and MITRE ATT&CK address the attack and risk surface, while NIST CSF 2.0 sets the governance and control requirements practitioners need to meet.

FrameworkControl / ReferenceRelevance
NIST CSF 2.0PR.AA-05 — Access Permissions, Entitlements and AuthorizationsTerraform in the article directly affects access scope and entitlement design.
Recommendation — Use PR.AA-05 to review generated Terraform for overbroad permissions and exposed access paths.
OWASP Non-Human Identity Top 10NHI-02 — Secret LeakageHardcoded credentials in Terraform are a direct secret leakage pattern.
NHI-05 — Overprivileged NHIWildcard IAM and broad access in AI-generated Terraform are classic overprivilege conditions.
Recommendation — Scan generated Terraform for secret leakage and replace inline credentials with managed secret references. Block overprivileged NHI patterns by enforcing least-privilege policy generation and review.
MITRE ATT&CKTA0006 — Credential AccessExposed secrets and permissive access can be abused for credential access.
Recommendation — Map exposed Terraform secrets to TA0006 and prioritise immediate revocation or replacement.

Key terms

  • Semantic IaC Analysis: Security analysis that evaluates what infrastructure code means, not only whether it parses. It checks whether permissions, encryption, logging, and resource exposure are appropriate for the intended deployment, which is essential when AI systems generate code that can be syntactically valid but operationally unsafe.
  • Privilege Drift: Privilege drift is the gradual gap between the permissions an identity was meant to have and the permissions it actually retains. In AI agent environments, drift grows quickly because roles are reused, tasks change, and lifecycle reviews often lag behind deployment velocity.
  • Provider Schema Drift: The gap between current cloud provider resource definitions and the patterns a model has learned from older code. When providers rename or split arguments, AI-generated Terraform can emit stale syntax that looks plausible but no longer matches the active schema, creating deployment and governance risk.
  • Secrets handling: The set of practices used to create, store, transmit, and rotate credentials such as API keys, tokens, and certificates. Poor secrets handling in generated code can create standing access, leak privileged credentials, and undermine both application security and broader identity governance.

Deepen your knowledge

NHI Mgmt Group covers identity security, NHI governance, and agentic AI through independent research, practitioner guides, and the NHI Foundation Level course. If your role touches access control, workload identity, or secrets governance, the course helps connect those controls into a single programme.
NHIMG Editorial Note
Published by the NHIMG editorial team on August 18, 2026.
Updated on October 12, 2026.
NHI Mgmt Group, the independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org