Back openDesk Edu for a sovereign, open-source education — every vote counts.
Vote nowSave products you love by clicking the heart icon.
Quick reference for Terraform infrastructure as code, HCL syntax, and best practices
Can artificial intelligence write Infrastructure as Code that's as secure as human-written? We benchmark the top LLMs to find out.
Infrastructure as Code (IaC) has transformed DevOps, enabling reproducible, version-controlled infrastructure deployments. But with this power comes responsibility — security vulnerabilities in IaC templates can have catastrophic consequences.
Common IaC security issues:
* permissions in IAM rolesThe OWASP Top 10 for IaC highlights these risks, with misconfigurations being the #1 cause of cloud security breaches.
Now, Large Language Models are entering the IaC space, promising to accelerate Terraform development. The question is: are they generating secure configurations, or are they introducing new risks?
The stakes are high:
A single insecure Terraform module can expose your entire cloud estate.
We evaluated 6 leading LLMs (GPT-4o, Claude 3.5 Sonnet, Mistral-large, Llama-3.1-70B, Gemma-2-27B, DeepSeek-Coder) on their ability to generate secure Terraform configurations for common cloud infrastructure patterns.
Each model was scored across 8 security dimensions:
| Dimension | Weight | Description |
|---|---|---|
| Secrets Management | 25% | No hardcoded credentials, secure secret references |
| Network Security | 20% | Proper security groups, firewall rules, private subnets |
| IAM & Access Control | 20% | Least privilege, no wildcard permissions |
| Encryption | 15% | Encryption at rest and in transit |
| Resource Hardening | 10% | Disabled debug, logging enabled, proper configurations |
| Compliance Standards | 5% | Alignment with CIS benchmarks, industry standards |
| Code Quality | 3% | Valid Terraform syntax, efficient code |
| Explainability | 2% | Clear comments, documentation |
We evaluated each model on 10 common infrastructure patterns:
| Model | Avg Score | Speed | Cost/1K tokens | Best For |
|---|---|---|---|---|
| GPT-4o | 92.5% | ⚡ Fast | $$$$ | Overall, Complex patterns |
| Claude 3.5 Sonnet | 91.2% | ⚡ Fast | $$$$ | IAM policies, Compliance |
| Mistral-large | 88.7% | ⚡⚡ Very Fast | $$$ | Speed-sensitive applications |
| Llama-3.1-70B | 87.3% | ⚡⚡ Very Fast | $$ | General IaC, Cost-effective |
| Gemma-2-27B | 86.1% | ⚡⚡⚡ Fastest | $ | Simple patterns, Low cost |
| DeepSeek-Coder | 84.5% | ⚡ Fast | $$ | Specialized IaC patterns |
Best: GPT-4o (97%), Claude 3.5 Sonnet (95%)
All models correctly avoided hardcoded secrets in the generated code. However, there were differences in how they handled secrets:
Example of Good Secrets Management:
# ✅ Secure: Reference AWS Secrets Manager
data "aws_secretsmanager_secret_version" "db_password" {
secret_id = "prod/rds/password"
}
resource "aws_db_instance" "example" {
# ...
password = data.aws_secretsmanager_secret_version.db_password.secret_string
# ...
}
Example of Bad Secrets Management:
# ❌ Insecure: Hardcoded password
resource "aws_db_instance" "example" {
password = "SuperSecretPassword123!"
}
Best: GPT-4o (90%), Claude 3.5 Sonnet (88%)
Most models created secure VPC configurations, but many missed key security controls:
| Issue | GPT-4o | Claude | Mistral | Llama | Gemma | DeepSeek |
|---|---|---|---|---|---|---|
| Restrict inbound traffic | ✅ | ✅ | ✅ | ⚠️ | ⚠️ | ✅ |
| Use private subnets | ✅ | ✅ | ✅ | ⚠️ | ⚠️ | ✅ |
| Enable VPC Flow Logs | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ |
| Proper NACL rules | ✅ | ✅ | ⚠️ | ⚠️ | ⚠️ | ⚠️ |
| Security group minimization | ✅ | ⚠️ | ⚠️ | ⚠️ | ⚠️ | ⚠️ |
The Network Security Gap: Most models generated functional network configurations but missed defense-in-depth principles.
Best: Claude 3.5 Sonnet (93%), GPT-4o (91%)
IAM policies were where models struggled the most:
| Issue | GPT-4o | Claude | Mistral | Llama | Gemma | DeepSeek |
|---|---|---|---|---|---|---|
| Least privilege | ✅ | ✅ | ⚠️ | ⚠️ | ❌ | ⚠️ |
| No wildcard permissions | ✅ | ✅ | ⚠️ | ❌ | ❌ | ⚠️ |
| Condition blocks | ✅ | ✅ | ⚠️ | ❌ | ❌ | ⚠️ |
| Resource-level permissions | ✅ | ✅ | ⚠️ | ⚠️ | ❌ | ⚠️ |
Example of Poor IAM Policy:
# ❌ Overly permissive
resource "aws_iam_role_policy" "lambda_policy" {
role = aws_iam_role.lambda.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "*" # 🚨 All actions!
Effect = "Allow"
Resource = "*" # 🚨 All resources!
}
]
})
}
Example of Good IAM Policy:
# ✅ Least privilege
resource "aws_iam_role_policy" "lambda_policy" {
role = aws_iam_role.lambda.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:UpdateItem"
]
Resource = aws_dynamodb_table.my_table.arn
},
{
Effect = "Allow"
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
Resource = "*"
}
]
})
}
Best: GPT-4o (95%), Claude 3.5 Sonnet (94%)
Most models enabled encryption, but often in inconsistent ways:
| Feature | Score |
|---|---|
| encryption at rest | 95% |
| encryption in transit | 87% |
| KMS key management | 62% |
| Customer-managed CMK | 45% |
Missing Encryption was the most common critical finding.
Based on our findings, we developed a Security Maturity Model for LLM-generated Terraform:
Based on our benchmarking, here are our recommendations:
Here's a prompt template that significantly improves security:
You are a professional DevOps engineer specializing in SECURE Infrastructure as Code.
When generating Terraform code, you MUST follow these security requirements:
1. NEVER hardcode secrets, passwords, or API keys. Use instead:
- AWS: SSM Parameter Store or Secrets Manager
- Azure: Key Vault
- GCP: Secret Manager
- General: Environment variables with sensitive = true
2. Network Security:
- Use private subnets by default
- Restrict inbound traffic to minimum required ports
- Enable VPC Flow Logs
- Use security groups as a secondary layer of defense
3. IAM & Access Control:
- Follow the principle of least privilege
- NEVER use wildcard (*) permissions
- Use condition blocks to restrict access further
- Prefer resource-level permissions over broad policies
4. Encryption:
- Enable encryption at rest for all data stores
- Use customer-managed KMS keys when possible
- Enable TLS 1.2+ for all communications
5. Resource Hardening:
- Disable debug endpoints in production
- Enable detailed logging with appropriate retention
- Use recent, supported versions of resources
6. Compliance:
- Follow CIS benchmarks where applicable
- Tag resources appropriately
- Enable deletion protection on critical resources
Now, please generate Terraform code for: [YOUR REQUEST]
For each decision you make, explain your security rationale.
Always scan LLM-generated Terraform with security tools:
# Install security scanning tools
npm install -g tfsec
pip install checkov
# Scan your Terraform
checkov -d /path/to/terraform
tfsec /path/to/terraform
# For CI/CD integration
checkov -d . --soft-fail && tfsec . --soft-fail
Recommended Tools:
The most secure approach combines LLM generation with security automation:
LLM generates Terraform
│
▼
┌─────────────────┐
│ Security Scanner │──▶ Report vulnerabilities
└─────────────────┘
│
▼
Fix issues
│
▼
┌─────────────────┐
│ Human Review │──▶ Approve or reject
└─────────────────┘
│
▼
┌─────────────────┐
│ Deployment │
└─────────────────┘
│
▼
┌─────────────────┐
│ Continuous │
│ Monitoring │
└─────────────────┘
Challenge: Fast-growing fintech needed to scale infrastructure while maintaining PCI-DSS compliance
Solution:
Results:
Challenge: Needed HIPAA-compliant infrastructure with minimal DevOps staff
Solution:
Results:
AI models specifically trained for security — not just general-purpose LLMs. These models would:
AI that doesn't just find security issues, but fixes them:
AI generates Terraform ---> Security scan ---> Fix suggested ---> Auto-apply
Real-time monitoring and compliance checking:
AI that generates code based on your specific security context:
LLMs show great promise for generating Terraform configurations, with top models achieving 90%+ security scores. However, no model is perfect — all generated code needs review and validation.
The key to success with AI-powered IaC is an approach:
AI can dramatically accelerate IaC development, but security must come first. With the right approach, you can have both speed and security — the holy grail of modern DevOps.
Want to dive deeper? Check out our Text to Terraform Security Analysis and Kubernetes v1.37 Release Notes for more on infrastructure security.
For hands-on learning, explore our Open Source Infrastructure Stack for 2026.