Scan Terraform for security issues with tfsec and Checkov
✓Works with OpenClaudeYou are a DevOps security engineer. The user wants to scan Terraform configuration files for security vulnerabilities using tfsec and Checkov, then interpret and remediate findings.
What to check first
- Run
terraform versionto confirm Terraform is installed - Verify your Terraform files exist in the current directory (look for
*.tffiles) - Check if tfsec and Checkov are installed:
tfsec --versionandcheckov --version
Steps
- Install tfsec using
brew install tfsec(macOS) orchoco install tfsec(Windows), or download from https://github.com/aquasecurity/tfsec/releases - Install Checkov using
pip install checkovorbrew install checkov - Run tfsec with severity filtering:
tfsec . --minimum-severity HIGH --format json > tfsec-report.json - Run Checkov with framework specification:
checkov -d . --framework terraform --output json > checkov-report.json - Review tfsec output focusing on rule IDs (e.g.,
AVD-AWS-0001for public S3 buckets) and affected resource lines - Review Checkov output for CKV check IDs and policy violations with remediation guidance
- Cross-reference findings between both tools to identify critical overlaps
- Apply fixes to your Terraform code (e.g., adding
block_public_acls = trueto S3 buckets) - Re-run both scanners to confirm remediations:
tfsec . && checkov -d .
Code
#!/usr/bin/env python3
"""
Terraform security scanning with tfsec and Checkov integration.
Parses, deduplicates, and summarizes findings.
"""
import json
import subprocess
import sys
from pathlib import Path
from collections import defaultdict
def run_tfsec(directory="."):
"""Run tfsec and return parsed results."""
try:
result = subprocess.run(
["tfsec", directory, "--format", "json", "--minimum-severity", "MEDIUM"],
capture_output=True,
text=True,
check=False
)
if result.stdout:
return json.loads(result.stdout)
except FileNotFoundError:
print("Error: tfsec not found. Install with: pip install tfsec or brew install tfsec")
sys.exit(1)
return {"results": []}
def run_checkov(directory="."):
"""Run Checkov and return parsed results."""
try:
result = subprocess.run(
["checkov", "-d", directory, "--framework", "terraform", "--output", "json"],
capture_output=True,
text=True,
check=False
)
if result.stdout:
return json.loads(result.stdout)
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Terraform Skills
Other Claude Code skills in the same category — free to download.
Terraform Module
Create reusable Terraform modules with variables and outputs
Terraform State
Manage Terraform state with remote backends (S3, Azure, GCS)
Terraform Workspace
Configure Terraform workspaces for multi-environment management
Terraform Provider
Write custom Terraform providers with Go
Terraform Import
Import existing infrastructure into Terraform state
Terraform Testing
Write Terraform tests with Terratest and terraform test
Terraform CI/CD
Set up Terraform CI/CD with GitHub Actions and Atlantis
Want a Terraform skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.