Verified SOP & TutorialAudited Score 98/100

The Complete Production SOP: How to Deploy and Run tfsec Pre-Deployment Terraform Security Linter in Autonomous AI Agents

Step-by-step SOP to install and run the tfsec Terraform security linter in Codex, Claude Code, and Cursor. Fix alerts, automate scans, and secure cloud IaC.

Target Runtimes:codexclaude-codecursorwindsurf
5 min read • Updated for 2026

Technical Overview & Architectural Context

Autonomous coding agents frequently generate syntactically valid Terraform code that violates cloud security baseline standards. When synthesizing AWS S3 buckets, Azure Network Security Groups, or GCP Cloud Storage blocks, LLMs naturally lean toward default parameters: unencrypted storage volumes, wildcards in IAM policies, exposed ingress on port 22/3389, and unmasked sensitive variables. Without an enforceable static analysis harness, agents proceed directly to plan and apply phases, committing vulnerable infrastructure definitions into version control. The tfsec Pre-Deployment Terraform Security Linter skill bridges this gap by imposing a deterministic validation gate. Rather than hoping the model remembers CIS, NIST, or cloud-specific hardening rules in its attention heads, this skill constrains the agent to run static analysis directly against HCL abstract syntax trees before emitting code changes. The SOP forces the agent to interpret machine-readable diagnostics, apply remediation attributes (such as server-side encryption or bucket policy enforcements), and ensure sensitive outputs are properly shielded. By executing in workspace isolation with explicit permission boundaries (read-only shell execution and workspace-only filesystem writes), the skill provides a closed-loop self-correction cycle. If tfsec flags critical findings, the autonomous agent is systematically instructed to fix the declared resources and re-verify until the security linter exits with return code 0.

Prerequisites & Environment Requirements

Codex Desktop, Claude Code CLI, Cursor, or Windsurf installed with agent capabilities
tfsec binary (v1.28.0+) installed via Homebrew, apt, or direct binary release in system PATH
Terraform CLI (v1.5.0+) installed in the workspace environment
Git 2.40+ initialized in your target infrastructure repository

Verified Search Intents & Long-Tail Queries Solved

SEO Audit Score: 98/100

This operational tutorial is engineered to provide deterministic solutions for these developer search queries:

#install tfsec-terraform-security-linter in cursor ide#claude code tfsec terraform security rules#codex desktop tfsec-terraform-security-linter setup tutorial#how to fix tfsec terraform security alert agent execution error#deterministic terraform security agent SOP#tfsec-terraform-security-linter sandbox permissions and security score#run tfsec scan autonomous agent prompts

Step-by-Step Execution Playbook

1

Local Skill Manifest Installation & Runtime Mounting

Mount the skill manifest into your agent runtime's global or workspace-level configuration.

Autonomous runtimes discover capabilities by reading standardized markdown manifests containing YAML frontmatter. For Claude Code, Cursor, Windsurf, or Codex, mount the SKILL.md definition into the dedicated workspace skills directory (`.agents/skills/`) or runtime rule path (`.cursor/rules/` or global agent cache). This injects the trigger phrases and bounded permissions directly into the LLM system prompt.

Terminal Command
mkdir -p .agents/skills/tfsec-terraform-security-linter && curl -fsSL https://askills.dev/api/skills/tfsec-terraform-security-linter/raw -o .agents/skills/tfsec-terraform-security-linter/SKILL.md
.agents/skills/tfsec-terraform-security-linter/SKILL.md
---
name: "tfsec-terraform-security-linter"
description: "Detect cloud infrastructure security flaws locally before provisioning resources."
metadata:
  author: "Aqua Security"
  version: "1.0.0"
  runtime_compatibility:
    - "codex"
    - "claude-code"
    - "cursor"
    - "windsurf"
triggers:
  - "run tfsec scan"
  - "fix terraform security alert tfsec"
  - "audit aws terraform tfsec"
permissions:
  fs_read: true
  fs_write: "workspace"
  shell_exec: false
  net_access: false
---

# tfsec Pre-Deployment Terraform Security Linter

Runs tfsec static analysis against Terraform code: evaluating resource blocks, references, and variables against cloud security benchmarks (AWS, Azure, GCP).

## Execution SOP & Rules
1. Execute `tfsec . --format json` in the root of the Terraform project before running `terraform plan`.
2. Review remediation advice provided in tfsec output and apply recommended attributes directly to HCL files.
3. Ensure sensitive variables declare `sensitive = true` to prevent cleartext logging.
4. Re-run scan to verify zero critical or high severity alerts exist.
Production Pro-Tip

Use `tfsec . --concise-output --format json` when invoking the tool inside agent workflows to minimize JSON parsing token bloat in LLM context windows.

Sandbox Gotcha

Ensure the agent's runner has execution permissions for the `tfsec` binary; if running inside Dockerized sandboxes, mount the binary or install it in `/usr/local/bin`.

2

Local Binary Verification & Workspace Config

Ensure the tfsec binary is present in PATH and configure project-level ignore rules if necessary.

Before the agent starts running security audits, ensure that `tfsec` is installed and properly configured with a `.tfsec/config.yml` or root-level `.tfsecignore` file for intentional deviations. This prevents the agent from entering an infinite remediation loop on known exceptions.

Terminal Command
which tfsec || (brew install tfsec || curl -s https://raw.githubusercontent.com/aquasecurity/tfsec/master/scripts/install_linux.sh | bash)
.tfsec/config.yml
---
minimum_severity: "MEDIUM"
severity_overrides:
  aws-s3-enable-bucket-logging: "LOW"
exclude:
  - "aws-vpc-no-public-ingress"
Production Pro-Tip

Define explicit severity baselines using `minimum_severity: HIGH` in non-prod staging branches to keep automated agent remediation focused strictly on critical vulnerabilities.

Sandbox Gotcha

Do not let the agent append `#tfsec:ignore` comments arbitrarily without documenting rationale; enforce that all ignores must be approved human overrides.

3

Triggering Agent Remediation Workflow

Invoke the agent using explicit natural language trigger anchors to start deterministic auditing.

Autonomous runtimes parse user instructions against trigger lists defined in SKILL.md. Providing prompts with trigger phrases such as 'run tfsec scan' or 'fix terraform security alert tfsec' initiates the procedural steps outlined in the skill rather than a generic prompt response.

Terminal Command
# In Claude Code CLI: claude "audit aws terraform tfsec in ./modules/storage and fix all reported security alerts"
modules/storage/main.tf
# Pre-remediation resource flagged by tfsec
resource "aws_s3_bucket" "data_lake" {
  bucket = "corp-telemetry-lake"
}

# Post-remediation expected from agent
resource "aws_s3_bucket" "data_lake" {
  bucket = "corp-telemetry-lake"
}

resource "aws_s3_bucket_server_side_encryption_configuration" "data_lake" {
  bucket = aws_s3_bucket.data_lake.id

  rule {
    apply_server_side_encryption_by_default {
      sse_algorithm = "aws:kms"
    }
  }
}

resource "aws_s3_bucket_public_access_block" "data_lake" {
  bucket                  = aws_s3_bucket.data_lake.id
  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}
Production Pro-Tip

Target specific module subdirectories in your prompt (e.g., `./modules/network`) to keep the diff and tool execution output small.

Sandbox Gotcha

Agents may suggest deprecated inline S3 bucket parameters for legacy Terraform AWS providers (v3.x) instead of decoupled v4/v5 resources; ensure provider version constraints are declared.

4

Verification, Linting & Output Quality Gate

Validate that all tfsec security checks exit cleanly with code 0 before accepting agent deliverables.

Execute both standard Terraform validation and a strict tfsec check. The skill requires an automated quality gate verification step to guarantee that the remediation introduced no syntax regressions or syntax invalidations.

Terminal Command
terraform fmt -check && terraform validate && tfsec . --concise-output
verify-security.sh
#!/usr/bin/env bash
set -euo pipefail

echo "[1/3] Checking Terraform format..."
terraform fmt -check -diff

echo "[2/3] Validating configuration..."
terraform validate

echo "[3/3] Running tfsec static analysis..."
tfsec . --exclude-downloaded-modules --minimum-severity HIGH

echo "All security checks passed."
Production Pro-Tip

Include `--exclude-downloaded-modules` in your tfsec execution to avoid scanning vendored upstream Terraform registry modules that you cannot directly patch.

Sandbox Gotcha

If `terraform init` has not been run, `terraform validate` will fail. Ensure module blocks and providers are locally initialized before the verification step.

Real-World Scenario: Remediating High-Risk S3 Bucket and Unencrypted RDS Database

Tested Trigger Prompt (Feed directly into Codex, Claude, or Cursor):

“Run tfsec scan on ./infra/database and ./infra/storage. Fix all high or critical terraform security alerts reported by tfsec, verify the changes with terraform validate, and ensure all sensitive values are masked.”

Deterministic Agent Verification Pipeline:
Agent executes `tfsec ./infra/database ./infra/storage --format json` and detects missing customer-managed KMS encryption, missing public access blocks, and cleartext RDS master password variables.
Agent updates `infra/database/variables.tf` to declare `sensitive = true` for `db_password` and adds `storage_encrypted = true` with `kms_key_id` to the `aws_db_instance` resource.
Agent constructs `aws_s3_bucket_public_access_block` and `aws_s3_bucket_versioning` resources in `infra/storage/s3.tf` matching AWS CIS foundation benchmarks.
Agent runs `tfsec .` to verify zero remaining violations and executes `terraform validate` to guarantee syntax correctness.

Runtime Troubleshooting & Failure Modes

Error: ERROR: failed to load module: failed to read module ... fork/exec tfsec: no such file or directory

Root Cause: The agent execution environment lacks the `tfsec` binary in its accessible PATH, or the runtime sandbox prevents invoking unlisted external binaries.

Resolution Command / Fix:
export PATH="$PATH:/usr/local/bin:~/.local/bin" && curl -s https://raw.githubusercontent.com/aquasecurity/tfsec/master/scripts/install_linux.sh | bash
Error: Rule aws-s3-enable-bucket-encryption failed: Resource 'aws_s3_bucket.main' is missing server-side encryption configuration

Root Cause: The agent attempted an inline server_side_encryption_configuration block on an AWS provider v4+ resource definition where encryption must be an independent aws_s3_bucket_server_side_encryption_configuration resource.

Resolution Command / Fix:
resource "aws_s3_bucket_server_side_encryption_configuration" "main" {
  bucket = aws_s3_bucket.main.id
  rule {
    apply_server_side_encryption_by_default {
      sse_algorithm = "aws:kms"
    }
  }
}

Frequently Asked Questions (tfsec Pre-Deployment Terraform Security Linter SOP)

Q:Can I use tfsec Pre-Deployment Terraform Security Linter across Codex, Claude Code, and Cursor simultaneously?

Yes. The skill conforms to the universal SKILL.md specification. You can place the file in `.agents/skills/tfsec-terraform-security-linter/SKILL.md` or symlink it to `.cursor/rules/tfsec.mdc`, allowing seamless multi-agent support across Claude Code, Codex, Windsurf, and Cursor.

Q:How does tfsec Pre-Deployment Terraform Security Linter prevent context window overflow during long agent sessions?

The skill instructs the agent to run tfsec with filtered flags such as `--concise-output` and `--exclude-downloaded-modules` while scoping scans to relevant directories. This prevents hundreds of kilobytes of unformatted CLI text from polluting LLM token context.

Q:Does this skill require escalated root or container permissions?

No. The skill operates with a verified security score of 98/100. It requires read access to the workspace filesystem and write access only within the project directory to apply HCL patches. No outbound network access or root permissions are needed.

Ready to equip your autonomous agent with tfsec Pre-Deployment Terraform Security Linter?

Copy the complete audited SKILL.md specification or download the 1-click zip bundle directly from our verified registry.

Get 1-Click Install Spec