Back to KB
Difficulty
Intermediate
Read Time
15 min

A Practical Terraform Security Review with Codex and Claude Code

By Codcompass Team··15 min read

A Terraform repository is not just code. It is a map of your cloud control plane.

It defines who can reach production, which services are exposed to the internet, where logs are stored, how identities are trusted, and which systems have permission to change infrastructure. That is why an infrastructure-as-code review deserves the same discipline as an application security review, and sometimes more.

AI coding tools such as Codex and Claude Code can help you review Terraform faster, but they should not replace security judgment. They are useful assistants for reading large repositories, finding risky patterns, explaining possible attack paths, and drafting better remediation notes. The accountability still stays with the security engineer.

If you are a cybersecurity engineer early in your career and someone has asked you to review a Terraform repo before production, this guide is for you. I’ll walk you through a practical workflow that keeps the review safe, structured, and useful for the infrastructure team.

We will use two AI-assisted workflows:

  1. Codex connected to a GitHub repository for repository-aware review, GitHub workflows, and patch proposals.
  2. Claude Code launched from a locally cloned repository for terminal-first review, validation commands, and scanner-assisted analysis.

This is not about choosing one tool as the winner. It is about using each one where it fits best, with the same security discipline.


The Use Case

You have been asked:

“Can you review this Terraform repo before we deploy it to production?”

The repository might look like this:

terraform/
├── environments/
│   ├── dev/
│   ├── staging/
│   └── prod/
├── modules/
│   ├── networking/
│   ├── iam/
│   ├── compute/
│   ├── storage/
│   └── eks/
├── providers.tf
├── versions.tf
├── variables.tf
└── outputs.tf

Enter fullscreen mode Exit fullscreen mode

A vague prompt such as “find security issues” is not enough. A good Terraform review needs to answer precise questions:

  • Which cloud assets are managed by this repo?
  • Which resources are reachable from the internet?
  • Where is IAM over-permissioned?
  • Is sensitive data stored, transmitted, and logged safely?
  • Are logs enabled for investigation and incident response?
  • Are secrets, state files, or sensitive outputs exposed?
  • Which proposed fixes could break production?
  • Which findings require human verification before a pull request is opened?

Here is the mindset I want you to use:

You are not just looking for bad Terraform. You are looking for future production risk.

For example, if you find an aws_security_group_rule that allows 0.0.0.0/0 to TCP/22, your job is not only to write “public SSH is bad.” Your job is to confirm where that rule is attached, who uses it, whether there is an approved bastion or VPN access path, and whether tightening the rule could block production support.

That is the difference between a basic scan and a professional security review.


First, a Clear Definition of “Local”

In this article, local repository means a normal Git clone on your workstation, laptop, jump box, or review virtual machine.

For example:

git clone git@github.com:your-org/terraform-platform.git
cd terraform-platform

Enter fullscreen mode Exit fullscreen mode

It does not mean a local AI model.

Claude Code may still use a cloud-backed AI service depending on your setup and plan. The word “local” only describes where the Terraform files live while you are reviewing them.

The two workflows look like this:

Workflow

Where the repo sits

Best for

Codex

GitHub repo connected to ChatGPT/Codex

Repository-wide review, GitHub workflows, patch proposals

Claude Code

Locally cloned repo on your workstation or review VM

Terminal-first review, Terraform validation, scanner-assisted analysis

Choose based on your company policy, repository sensitivity, access model, and how much control you need during the review.


The One Rule I Want You to Remember

Give AI tools the code they need to review, but keep production authority with humans and approved automation.

That means the tool can help you read Terraform, understand risk, suggest changes, and write review notes. Production credentials, deployment rights, and approval authority should stay outside the AI-assisted review unless your organization has explicitly approved a different operating model.

Keep these outside the AI workflow unless your security policy says otherwise:

  • Production cloud credentials
  • Terraform state files such as terraform.tfstate
  • Variable files containing real secrets such as prod.tfvars
  • Private keys
  • Kubeconfigs
  • CI/CD secrets
  • Vault tokens
  • Break-glass credentials

A well-scoped AI review can still uncover serious issues from code alone, including public ingress, wildcard IAM, missing encryption, missing logging, exposed outputs, unsafe defaults, weak remote state configuration, and CI/CD over-permissioning.

You do not need to give an AI tool production authority to get useful security value from it.


Step 1: Prepare the Repository Before the Review

Before using Codex or Claude Code, inspect the repository for files that should not be casually shared or analyzed outside the approved review boundary.

From your review environment, run:

find . -type f \( \
  -name "*.tfstate" -o \
  -name "*.tfstate.backup" -o \
  -name "*.tfvars" -o \
  -name ".env" -o \
  -name "*.pem" -o \
  -name "*.key" -o \
  -name "kubeconfig" -o \
  -name "credentials" \
\)

Enter fullscreen mode Exit fullscreen mode

Then check for local Terraform working folders:

find . -type d -name ".terraform"

Enter fullscreen mode Exit fullscreen mode

If you find state files, private keys, credentials, or real production variable values, treat that as a security concern immediately. Raise it with the infrastructure owner before continuing.

A clean Terraform repository usually contains files such as:

main.tf
variables.tf
outputs.tf
providers.tf
versions.tf
modules/
environments/
README.md

Enter fullscreen mode Exit fullscreen mode

A repository should normally not contain:

terraform.tfstate
terraform.tfstate.backup
*.tfvars with real secrets
.terraform/
.env
*.pem
*.key
kubeconfig
credentials

Enter fullscreen mode Exit fullscreen mode

This preparation step protects both the organization and your review process.


Step 2: Create a Dedicated Review Branch

Create a separate branch for AI-assisted review work:

git checkout -b security/iac-review-ai-assisted
git push origin security/iac-review-ai-assisted

Enter fullscreen mode Exit fullscreen mode

A dedicated branch keeps your review activity separate from normal feature work. It also gives your team a clear audit trail for notes, proposed patches, scanner results, and pull request comments.

Good branch

🎉 Mid-Year Sale — Unlock Full Article

Base plan from just $4.99/mo or $49/yr

Sign in to read the full article and unlock all 635+ tutorials.

Sign In / Register — Start Free Trial

7-day free trial · Cancel anytime · 30-day money-back