Claude Code + Terraform: Safe AI-Assisted IaC with Guardrails
AI can write Terraform faster than any person, and one wrong apply can take down production. These are the guardrails we use with Claude Code: plan-only, written rules, no production credentials, and review before merge.
By VVVHQ Team ·
AI-assisted infrastructure as code saves real time, and it can also do real damage. An agent that can run shell commands can run terraform apply -auto-approve, and one wrong resource argument or missing lifecycle block can leave you looking at a deleted RDS instance. We use Claude Code for Terraform work, inside a set of guardrails that keeps every change to real infrastructure behind a reviewed plan.
Why Terraform suits AI, and where it goes wrong
Terraform is declarative, which makes it a good fit for code generation. You describe the end state, the assistant writes the HCL, and terraform plan shows exactly what will change. The risk sits between plan and apply. If the assistant can apply, a mistake in the plan goes straight into your infrastructure, and the mistake is often one a reviewer would have caught in seconds.
So the rule is simple. The assistant writes code and runs plans. People review, and the pipeline applies.
Four layers of guardrails
Plan only
The assistant may run terraform plan, validate and fmt, and never apply, destroy, import, taint or any state command. Claude Code enforces this in two ways. Permission deny rules in .claude/settings.json refuse the commands outright, and a PreToolUse hook inspects every shell command before it runs:
{
"permissions": {
"deny": [
"Bash(terraform apply:*)",
"Bash(terraform destroy:*)",
"Bash(terraform state:*)",
"Bash(terraform import:*)",
"Bash(terraform taint:*)"
]
},
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{ "type": "command", "command": ".claude/hooks/check-terraform-command.sh" }
]
}
]
}
}
#!/usr/bin/env bash
# Blocks state-changing Terraform and OpenTofu commands anywhere in the command line.
cmd=$(jq -r '.tool_input.command')
if grep -Eq '(terraform|tofu)[[:space:]]+(apply|destroy|import|taint|untaint|state)' <<<"$cmd"; then
echo "Blocked: infrastructure changes are applied by CI after review." >&2
exit 2
fi
exit 0
Exit code 2 blocks the call and returns the message to the assistant. The hook catches commands the prefix-based deny rules would miss, such as cd modules && terraform apply. Treat both as a convenience layer. The control that cannot be talked around is the credential boundary below.
Written rules
Claude Code reads project instructions (CLAUDE.md and the files in .claude/rules/) at the start of every session, so the safety rules travel with the repository:
## Terraform safety rules
- Never run terraform apply, destroy, taint, untaint, import or state commands.
- Never write IAM policies with wildcard actions or resources.
- Never open a security group to 0.0.0.0/0.
- Never edit state files.
- Never remove a prevent_destroy lifecycle block.
- Add prevent_destroy to stateful resources such as databases and buckets.
- Run terraform validate and terraform plan before presenting a change.
- Use variables for anything that differs between environments.
Rules shape what the assistant writes. They do not stop a determined mistake, which is why the next two layers exist.
No production credentials
The assistant works only with credentials for development and staging accounts. Production credentials exist only in the CI/CD pipeline, behind a manual approval. Each environment has its own state:
terraform {
backend "s3" {
bucket = "acme-terraform-state-dev"
key = "infra/terraform.tfstate"
region = "us-east-1"
encrypt = true
use_lockfile = true
}
}
# The assistant's session only has credentials for the dev and staging accounts.
# Production state and credentials are reachable from the pipeline only.
Without production credentials, the assistant cannot change production, whatever command it runs.
Review before merge
The assistant commits to a feature branch and opens a pull request. CI posts the plan as a comment, and a person reviews the code diff and the plan together. Production is applied by the pipeline after approval, never from the assistant's session.
Automated checks on every commit
Every commit, written by a person or an assistant, goes through the same checks:
# .pre-commit-config.yaml
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.96.1 # pin to the current release
hooks:
- id: terraform_fmt
- id: terraform_validate
- id: terraform_trivy
- id: terraform_checkov
Trivy (which absorbed tfsec) and Checkov catch public buckets, unencrypted volumes and open security groups. CI runs them again, because local hooks can be skipped.
Where the assistant helps most
- Writing new modules: variables, outputs, documentation and sensible defaults from a plain description, followed by a careful review.
- Refactoring: pulling hardcoded values into variables, updating every reference, writing a
terraform.tfvars.example. - Documentation: module READMEs generated from the variables, outputs and resources.
- Security review: flagging missing encryption, wide CIDR ranges, missing logging and missing tags.
- Debugging failed plans: dependency cycles, provider version conflicts, drift.
- Planned refactors: writing
moved,importandremovedblocks, which show up in the plan and get reviewed like any other change.
What stays with people
- Applying to production, through CI with manual approval.
- Any
terraform statecommand run from a shell. - IAM policies with broad permissions. Assistants tend to grant more than needed to make something work, so every policy gets a least-privilege review.
- Deleting or replacing resources. The dependencies behind a destroy are often outside the code the assistant can see.
Assistant proposes, person decides
The assistant writes HCL quickly, never forgets the tags, and writes the README nobody else wanted to write. It does not know the blast radius of a security group change or what five minutes of downtime costs your business. The guardrails keep each side doing the part it is good at.
We set up AI coding assistants for engineering teams with the access controls and audit trail a security team will ask about. To put the same guardrails around your own Terraform, get in touch.