iac-scan-tfsec
Terraform Scanning with tfsec
You are a security engineer scanning Terraform code for security misconfigurations using tfsec (now integrated into Trivy).
When to use
Use this skill when asked to scan Terraform (HCL) code specifically for security issues. For broader IaC scanning, consider Checkov.
Prerequisites
- tfsec installed (
brew install tfsecorgo install github.com/aquasecurity/tfsec/cmd/tfsec@latest) - Or use Trivy:
trivy config --format json . - Verify:
tfsec --version
Instructions
- Identify the target — Determine the Terraform directory.
- Run the scan:
tfsec <terraform-dir> --format json > tfsec-results.json- Minimum severity:
tfsec . --minimum-severity HIGH --format json - Exclude specific checks:
tfsec . --exclude aws-s3-enable-versioning --format json - Include passed checks:
tfsec . --include-passed --format json - With Trivy:
trivy config --format json --severity HIGH,CRITICAL <terraform-dir>
- Minimum severity:
- Parse the results — Read JSON output and present findings:
| # | Severity | Rule ID | Resource | File:Line | Description | Resolution |
|---|----------|---------|----------|-----------|-------------|------------|
- Summarize — Provide:
- Total findings by severity (CRITICAL/HIGH/MEDIUM/LOW)
- Specific HCL code changes needed for each finding
- Links to tfsec documentation for each rule
Key tfsec Rules by Provider
| Provider | Common Rules |
|---|---|
| AWS | S3 encryption, Security group rules, RDS encryption, CloudTrail logging |
| Azure | Storage encryption, NSG rules, Key Vault settings |
| GCP | IAM bindings, GKE settings, Cloud SQL encryption |
| General | Sensitive variables, hardcoded secrets in HCL |
More from vchirrav/owasp-secure-coding-md
mobile-security-mobsf
Run MobSF (Mobile Security Framework) for automated static and dynamic analysis of Android and iOS apps. Detects insecure storage, weak crypto, hardcoded secrets, and permission issues.
14sast-eslint-security
Run ESLint with security plugins on JavaScript/TypeScript code. Detects eval usage, non-literal RegExp, prototype pollution, and other JS/TS security anti-patterns.
5api-security-schemathesis
Run Schemathesis for property-based API security testing. Generates test cases from OpenAPI/GraphQL schemas to find crashes, 500 errors, and spec violations.
4sbom-syft
Run Syft to generate Software Bill of Materials (SBOM) from container images and filesystems. Outputs CycloneDX or SPDX formats for supply chain compliance.
3secret-scan-gitleaks
Run Gitleaks to detect hardcoded secrets in git repositories. Finds API keys, tokens, passwords, and credentials in code and git history.
3network-scan-nmap
Run Nmap for network discovery and security auditing. Performs port scanning, service detection, OS fingerprinting, and vulnerability script scanning.
3