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.
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
Verified Search Intents & Long-Tail Queries Solved
This operational tutorial is engineered to provide deterministic solutions for these developer search queries:
Step-by-Step Execution Playbook
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.
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---
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.Use `tfsec . --concise-output --format json` when invoking the tool inside agent workflows to minimize JSON parsing token bloat in LLM context windows.
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`.
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.
which tfsec || (brew install tfsec || curl -s https://raw.githubusercontent.com/aquasecurity/tfsec/master/scripts/install_linux.sh | bash)---
minimum_severity: "MEDIUM"
severity_overrides:
aws-s3-enable-bucket-logging: "LOW"
exclude:
- "aws-vpc-no-public-ingress"Define explicit severity baselines using `minimum_severity: HIGH` in non-prod staging branches to keep automated agent remediation focused strictly on critical vulnerabilities.
Do not let the agent append `#tfsec:ignore` comments arbitrarily without documenting rationale; enforce that all ignores must be approved human overrides.
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.
# In Claude Code CLI:
claude "audit aws terraform tfsec in ./modules/storage and fix all reported security alerts"# 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
}Target specific module subdirectories in your prompt (e.g., `./modules/network`) to keep the diff and tool execution output small.
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.
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.
terraform fmt -check && terraform validate && tfsec . --concise-output#!/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."Include `--exclude-downloaded-modules` in your tfsec execution to avoid scanning vendored upstream Terraform registry modules that you cannot directly patch.
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
“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.”
Runtime Troubleshooting & Failure Modes
Root Cause: The agent execution environment lacks the `tfsec` binary in its accessible PATH, or the runtime sandbox prevents invoking unlisted external binaries.
export PATH="$PATH:/usr/local/bin:~/.local/bin" && curl -s https://raw.githubusercontent.com/aquasecurity/tfsec/master/scripts/install_linux.sh | bashRoot 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.
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.