{"id":48689,"date":"2025-03-05T03:03:15","date_gmt":"2025-03-05T03:03:15","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=48689"},"modified":"2025-03-05T03:03:15","modified_gmt":"2025-03-05T03:03:15","slug":"terraform-tutorials-tfsec-for-security-scanning","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/terraform-tutorials-tfsec-for-security-scanning\/","title":{"rendered":"Terraform Tutorials: TFSec for Security Scanning"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><strong>Comprehensive Guide to TFSec: Terraform Security Scannin<\/strong>g<\/h3>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. What is TFSec?<\/strong><\/h2>\n\n\n\n<p><strong>TFSec<\/strong> is a <strong>static analysis security scanner<\/strong> designed to identify security vulnerabilities in <strong>Terraform Infrastructure-as-Code (IaC)<\/strong> configurations before they are applied to cloud environments.<\/p>\n\n\n\n<p>TFSec helps DevSecOps teams <strong>shift left<\/strong> by detecting misconfigurations, enforcing best security practices, and ensuring compliance with security policies.<\/p>\n\n\n\n<p>TFSec works by analyzing Terraform configuration files (<code>.tf<\/code> and <code>.tfvars<\/code>) <strong>without requiring access to cloud provider APIs<\/strong>, making it a fast and efficient security tool.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. TFSec Features<\/strong><\/h2>\n\n\n\n<p>TFSec is a feature-rich tool designed for Terraform security scanning. Here are its <strong>key features<\/strong>:<\/p>\n\n\n\n<p>\u2714 <strong>Deep Security Analysis<\/strong> &#8211; Detects insecure configurations in Terraform files before deployment.<br>\u2714 <strong>Supports Multiple Cloud Providers<\/strong> &#8211; Works with <strong>AWS, Azure, GCP, Kubernetes, and DigitalOcean<\/strong>.<br>\u2714 <strong>Built-in Compliance Policies<\/strong> &#8211; Checks against <strong>CIS Benchmarks, NIST, PCI-DSS, and ISO27001<\/strong>.<br>\u2714 <strong>Custom Rule Support<\/strong> &#8211; Allows organizations to create custom security policies.<br>\u2714 <strong>Automatic Module Discovery<\/strong> &#8211; Scans Terraform modules automatically.<br>\u2714 <strong>Context-Aware Scanning<\/strong> &#8211; Detects dynamic issues such as <strong>publicly exposed resources, weak IAM permissions, and unencrypted data storage<\/strong>.<br>\u2714 <strong>Fast &amp; Offline Execution<\/strong> &#8211; Does not require Terraform state files or cloud API access.<br>\u2714 <strong>CI\/CD Integration<\/strong> &#8211; Works with <strong>GitHub Actions, GitLab CI\/CD, Jenkins, CircleCI, and Azure DevOps<\/strong>.<br>\u2714 <strong>Flexible Output Formats<\/strong> &#8211; Generates reports in <strong>JSON, CSV, SARIF, and JUnit<\/strong> for security reporting.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. TFSec Benefits<\/strong><\/h2>\n\n\n\n<p>Using TFSec in a DevOps or SecOps workflow offers <strong>several advantages<\/strong>:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 <strong>Security Benefits<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Prevents misconfigurations<\/strong> before they reach production.<\/li>\n\n\n\n<li><strong>Enforces cloud security best practices<\/strong> (e.g., encrypting S3 buckets, restricting IAM policies).<\/li>\n\n\n\n<li><strong>Identifies public exposure risks<\/strong> (e.g., open security groups, unencrypted databases).<\/li>\n\n\n\n<li><strong>Improves compliance<\/strong> with CIS, NIST, SOC 2, PCI-DSS, and ISO27001 standards.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 <strong>Operational Benefits<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Faster security reviews<\/strong> \u2013 Detects issues in Terraform code early in the development process.<\/li>\n\n\n\n<li><strong>Lightweight and fast<\/strong> \u2013 Runs without requiring Terraform state or cloud access.<\/li>\n\n\n\n<li><strong>Easy CI\/CD integration<\/strong> \u2013 Automates security checks in GitHub Actions, GitLab CI, Jenkins, etc.<\/li>\n\n\n\n<li><strong>Custom security checks<\/strong> \u2013 Organizations can define their own security policies.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. How to Install TFSec?<\/strong><\/h2>\n\n\n\n<p>TFSec supports multiple installation methods across various operating systems.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd39 Install TFSec on macOS<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">brew install tfsec\n<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd39 Install TFSec on Linux<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">curl -s https:<span class=\"hljs-comment\">\/\/raw.githubusercontent.com\/aquasecurity\/tfsec\/master\/scripts\/install_linux.sh | bash<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd39 Install TFSec on Windows<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">choco install tfsec\n<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd39 Install via Docker<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">docker run --rm -v <span class=\"hljs-string\">\"$(pwd):\/src\"<\/span> aquasec\/tfsec \/src\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd39 Install via Go (for developers)<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">go install github.com\/aquasecurity\/tfsec\/cmd\/tfsec@latest\n<\/code><\/span><\/pre>\n\n\n<p>After installation, verify TFSec is working:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">tfsec --version\n<\/code><\/span><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. How to Use TFSec?<\/strong><\/h2>\n\n\n\n<p>Once installed, <strong>TFSec is easy to use<\/strong>. Navigate to your Terraform project directory and run:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">tfsec .\n<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd39 Example Output<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-attr\">&#91;HIGH]<\/span> <span class=\"hljs-selector-tag\">AWS<\/span> <span class=\"hljs-selector-tag\">S3<\/span> <span class=\"hljs-selector-tag\">bucket<\/span> <span class=\"hljs-selector-tag\">allows<\/span> <span class=\"hljs-selector-tag\">public<\/span> <span class=\"hljs-selector-tag\">access<\/span> (<span class=\"hljs-selector-tag\">aws-s3-enable-bucket-logging<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<p>[aws_s3_bucket.public]<\/p>\n\n\n\n<p>.\/s3.tf:15 14 | resource &#8220;aws_s3_bucket&#8221; &#8220;public&#8221; { 15 | acl = &#8220;public-read&#8221; 16 | } Fix: Change `acl` to &#8220;private&#8221; or &#8220;log-delivery-write&#8221;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Common TFSec Commands<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>tfsec .<\/code><\/td><td>Scan current Terraform directory.<\/td><\/tr><tr><td><code>tfsec --exclude aws-s3-enable-bucket-logging<\/code><\/td><td>Ignore specific checks.<\/td><\/tr><tr><td><code>tfsec --soft-fail<\/code><\/td><td>Run without failing CI pipelines.<\/td><\/tr><tr><td><code>tfsec --format json<\/code><\/td><td>Output results in JSON format.<\/td><\/tr><tr><td><code>tfsec --minimum-severity HIGH<\/code><\/td><td>Show only HIGH severity issues.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. Working with TFSec Locally<\/strong><\/h2>\n\n\n\n<p>To <strong>scan a Terraform project locally<\/strong>, follow these steps:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Initialize Terraform (Optional)<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">terraform init\n<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Run TFSec<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">tfsec .\n<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Fix Security Issues<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Modify Terraform files based on TFSec suggestions.<\/li>\n\n\n\n<li>Run <code>tfsec .<\/code> again to confirm issues are resolved.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 4: Automate with Git Pre-Commit Hook<\/strong><\/h3>\n\n\n\n<p>To prevent insecure Terraform code from being committed:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Install <code>pre-commit<\/code>: <code>pip install pre-commit<\/code><\/li>\n\n\n\n<li>Add <code>.pre-commit-config.yaml<\/code>: <code>repos: - repo: https:\/\/github.com\/aquasecurity\/tfsec rev: v1.28.0 hooks: - id: tfsec<\/code><\/li>\n\n\n\n<li>Install pre-commit hooks: <code>pre-commit install<\/code><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. Using TFSec in SecOps Pipelines<\/strong><\/h2>\n\n\n\n<p>TFSec integrates with CI\/CD pipelines to enforce security compliance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>GitHub Actions<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">jobs:\n  security:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions\/checkout@v4\n      - name: Run TFSec\n        uses: aquasecurity\/tfsec-action@main\n<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>GitLab CI\/CD<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">stages:\n  - security\n\nsecurity:\n  image: aquasec\/tfsec:latest\n  script:\n    - tfsec .\n<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\"><strong>Jenkins Pipeline<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">pipeline {\n    agent any\n    stages {\n        stage(<span class=\"hljs-string\">'Security Scan'<\/span>) {\n            steps {\n                sh <span class=\"hljs-string\">'tfsec .'<\/span>\n            }\n        }\n    }\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8. TFSec Custom Checks<\/strong><\/h2>\n\n\n\n<p>TFSec allows custom security rules to be defined using <strong>Rego policies<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example: Custom Check for Public S3 Buckets<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a custom rule file<\/strong> (<code>custom.rego<\/code>): <code>package tfsec.custom deny[msg] { input.resource_type == \"aws_s3_bucket\" input.values.acl == \"public-read\" msg = \"S3 buckets should not be public!\" }<\/code><\/li>\n\n\n\n<li><strong>Run TFSec with Custom Rules<\/strong> <code>tfsec --config-file custom.rego<\/code><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9. TFSec Alternatives<\/strong><\/h2>\n\n\n\n<p>While TFSec is an excellent Terraform security tool, here are <strong>some alternatives<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Tool<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><strong>Checkov<\/strong><\/td><td>Static analysis security scanner for Terraform, Kubernetes, and CloudFormation.<\/td><\/tr><tr><td><strong>Terraform Compliance<\/strong><\/td><td>Policy-as-code framework for Terraform security and compliance enforcement.<\/td><\/tr><tr><td><strong>Terrascan<\/strong><\/td><td>Security scanner that checks Terraform against compliance frameworks.<\/td><\/tr><tr><td><strong>Snyk Infrastructure as Code<\/strong><\/td><td>Cloud security scanner with a developer-friendly UI.<\/td><\/tr><tr><td><strong>Kics (Keep Infrastructure as Code Secure)<\/strong><\/td><td>Security analysis for Terraform, Kubernetes, and CloudFormation.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>TFSec is a <strong>powerful security scanner<\/strong> that helps developers and security teams enforce best practices in Terraform IaC. By integrating TFSec into <strong>local development workflows<\/strong> and <strong>CI\/CD pipelines<\/strong>, teams can proactively detect and fix security vulnerabilities before infrastructure is deployed.<\/p>\n\n\n\n<p>By following this guide, you can <strong>install, configure, and use TFSec effectively<\/strong> in your projects. \ud83d\ude80 Happy SecOps!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Handling TFSec Scanning in the <code>.terraform<\/code> Directory<\/strong><\/h3>\n\n\n\n<p>When running <strong>TFSec<\/strong> on a Terraform project, it <strong>automatically scans all directories<\/strong>, including the <code>.terraform<\/code> directory, which contains downloaded Terraform modules and providers. This can result in <strong>many false positives<\/strong> or security issues in third-party modules that you cannot directly modify.<\/p>\n\n\n\n<p>To <strong>prevent TFSec from scanning the <code>.terraform<\/code> directory<\/strong> and reduce noise in your results, follow the best practices below:<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd39 Best Approaches to Exclude <code>.terraform<\/code> Directory<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1\ufe0f\u20e3 Use <code>--exclude-path<\/code> Flag<\/strong><\/h4>\n\n\n\n<p>TFSec allows you to <strong>ignore specific directories<\/strong> using the <code>--exclude-path<\/code> option.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">tfsec<\/span> <span class=\"hljs-selector-tag\">--exclude-path<\/span> <span class=\"hljs-selector-class\">.terraform<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This tells TFSec to <strong>skip scanning<\/strong> the <code>.terraform<\/code> directory, reducing unnecessary warnings.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2\ufe0f\u20e3 Use <code>.tfsecignore<\/code> File<\/strong><\/h4>\n\n\n\n<p>You can create a <strong><code>.tfsecignore<\/code><\/strong> file in your Terraform project and <strong>exclude specific directories or checks<\/strong>.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\".terraform\/\"<\/span> &gt;&gt; .tfsecignore\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Or manually create <code>.tfsecignore<\/code> and add:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">.terraform\/\n<\/code><\/span><\/pre>\n\n\n<p>This ensures <strong>TFSec always ignores<\/strong> the <code>.terraform<\/code> directory for all future scans.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3\ufe0f\u20e3 Use <code>--force-all-dirs=false<\/code> Flag<\/strong><\/h4>\n\n\n\n<p>By default, <strong>TFSec scans all directories<\/strong>, even hidden ones. To <strong>disable scanning hidden directories<\/strong>, use:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">tfsec --force-all-dirs=<span class=\"hljs-literal\">false<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This stops TFSec from scanning <code>.terraform\/<\/code>, reducing noise from third-party modules.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>4\ufe0f\u20e3 Ignore Specific Module Directories<\/strong><\/h4>\n\n\n\n<p>If your Terraform project uses <strong>external modules<\/strong> (like AWS modules from Terraform Registry) and you don&#8217;t want to scan them, use:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">tfsec --exclude-path .terraform\/modules\n<\/code><\/span><\/pre>\n\n\n<p>This prevents scanning <strong>only the <code>modules\/<\/code> subdirectory<\/strong> inside <code>.terraform<\/code>, while keeping other checks active.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>5\ufe0f\u20e3 Use <code>--minimum-severity<\/code> to Filter Results<\/strong><\/h4>\n\n\n\n<p>If you want to <strong>only see critical security issues<\/strong>, set a minimum severity level:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">tfsec --minimum-severity HIGH\n<\/code><\/span><\/pre>\n\n\n<p>This <strong>filters out low-severity and medium-severity warnings<\/strong>, making results more relevant.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u2705 Recommended TFSec Configuration for Excluding <code>.terraform<\/code><\/strong><\/h3>\n\n\n\n<p>To ensure TFSec <strong>ignores irrelevant issues in <code>.terraform\/<\/code><\/strong>, use <strong>all the best practices<\/strong> together:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a <code>.tfsecignore<\/code> file<\/strong>: <code>echo \".terraform\/\" >> .tfsecignore<\/code><\/li>\n\n\n\n<li><strong>Run TFSec with proper flags<\/strong>: <code>tfsec . --exclude-path .terraform --force-all-dirs=false --minimum-severity HIGH<\/code><\/li>\n\n\n\n<li><strong>Modify <code>.pre-commit-config.yaml<\/code> (if using pre-commit hooks)<\/strong>: <code>repos: - repo: https:\/\/github.com\/aquasecurity\/tfsec rev: v1.28.0 hooks: - id: tfsec args: [ \"--exclude-path=.terraform\", \"--minimum-severity=HIGH\" ]<\/code><\/li>\n<\/ol>\n\n\n\n<p>By applying these <strong>best practices<\/strong>, you can: \u2705 Exclude <code>.terraform\/<\/code> from scans.<br>\u2705 Reduce <strong>false positives<\/strong> from third-party Terraform modules.<br>\u2705 Focus on <strong>real security risks<\/strong> in your own Terraform code.<br>\u2705 Ensure <strong>clean security reports<\/strong> without unnecessary warnings.<\/p>\n\n\n\n<p>This <strong>keeps your TFSec scans efficient and relevant<\/strong> while still enforcing security best practices. \ud83d\ude80<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Comprehensive Guide to <code>tfsec<\/code> Commands with Examples<\/strong><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Introduction to <code>tfsec<\/code><\/strong><\/h2>\n\n\n\n<p><code>tfsec<\/code> is a static analysis security scanner for Terraform configurations. It detects potential security misconfigurations, enforces best practices, and provides remediation suggestions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Basic Usage<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">tfsec .\n<\/code><\/span><\/pre>\n\n\n<p>This scans the current directory (<code>.<\/code>) for Terraform security issues.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Table of <code>tfsec<\/code> Commands and Examples<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>Command<\/strong><\/th><th><strong>Description<\/strong><\/th><th><strong>Example Usage<\/strong><\/th><\/tr><\/thead><tbody><tr><td><code>tfsec [directory]<\/code><\/td><td>Scans the given directory for security issues in Terraform configurations.<\/td><td><code>tfsec \/path\/to\/terraform\/code<\/code><\/td><\/tr><tr><td><code>--code-theme<\/code><\/td><td>Sets the theme for annotated code output (<code>light<\/code> or <code>dark<\/code>).<\/td><td><code>tfsec . --code-theme light<\/code><\/td><\/tr><tr><td><code>--concise-output<\/code><\/td><td>Reduces output verbosity by hiding statistics.<\/td><td><code>tfsec . --concise-output<\/code><\/td><\/tr><tr><td><code>--config-file<\/code><\/td><td>Specifies a configuration file for <code>tfsec<\/code>.<\/td><td><code>tfsec . --config-file tfsec-config.json<\/code><\/td><\/tr><tr><td><code>--config-file-url<\/code><\/td><td>Downloads a remote configuration file (must be JSON or YAML).<\/td><td><code>tfsec . --config-file-url https:\/\/example.com\/tfsec-config.yaml<\/code><\/td><\/tr><tr><td><code>--custom-check-dir<\/code><\/td><td>Defines a directory containing custom security checks.<\/td><td><code>tfsec . --custom-check-dir \/path\/to\/custom\/rules<\/code><\/td><\/tr><tr><td><code>--custom-check-url<\/code><\/td><td>Downloads a custom check file from a remote location (JSON\/YAML).<\/td><td><code>tfsec . --custom-check-url https:\/\/example.com\/custom-checks.json<\/code><\/td><\/tr><tr><td><code>--debug<\/code><\/td><td>Enables debug logging for detailed troubleshooting.<\/td><td><code>tfsec . --debug<\/code><\/td><\/tr><tr><td><code>--disable-grouping<\/code><\/td><td>Disables grouping of similar results in output.<\/td><td><code>tfsec . --disable-grouping<\/code><\/td><\/tr><tr><td><code>-e, --exclude<\/code><\/td><td>Excludes specific rule IDs from scanning.<\/td><td><code>tfsec . --exclude AWS001,AWS002<\/code><\/td><\/tr><tr><td><code>--exclude-downloaded-modules<\/code><\/td><td>Ignores <code>.terraform<\/code> directory to avoid scanning dependencies.<\/td><td><code>tfsec . --exclude-downloaded-modules<\/code><\/td><\/tr><tr><td><code>-E, --exclude-ignores<\/code><\/td><td>Ignores rules that were manually marked as ignored.<\/td><td><code>tfsec . --exclude-ignores AWS001,AWS002<\/code><\/td><\/tr><tr><td><code>--exclude-path<\/code><\/td><td>Excludes specific directories or files from scanning.<\/td><td><code>tfsec . --exclude-path .terraform\/modules<\/code><\/td><\/tr><tr><td><code>--filter-results<\/code><\/td><td>Filters results to return specific checks only.<\/td><td><code>tfsec . --filter-results AWS002,AWS003<\/code><\/td><\/tr><tr><td><code>--force-all-dirs<\/code><\/td><td>Scans all directories without searching for <code>.tf<\/code> files.<\/td><td><code>tfsec . --force-all-dirs<\/code><\/td><\/tr><tr><td><code>-f, --format<\/code><\/td><td>Specifies the output format (<code>json<\/code>, <code>html<\/code>, <code>csv<\/code>, <code>sarif<\/code>, etc.).<\/td><td><code>tfsec . --format json<\/code><\/td><\/tr><tr><td><code>-h, --help<\/code><\/td><td>Displays the help menu with available commands.<\/td><td><code>tfsec --help<\/code><\/td><\/tr><tr><td><code>--ignore-hcl-errors<\/code><\/td><td>Ignores errors related to HCL parsing failures.<\/td><td><code>tfsec . --ignore-hcl-errors<\/code><\/td><\/tr><tr><td><code>--include-ignored<\/code><\/td><td>Displays ignored security issues in the output.<\/td><td><code>tfsec . --include-ignored<\/code><\/td><\/tr><tr><td><code>--include-passed<\/code><\/td><td>Shows passed checks in the output.<\/td><td><code>tfsec . --include-passed<\/code><\/td><\/tr><tr><td><code>--migrate-ignores<\/code><\/td><td>Migrates ignore codes to a new ID structure.<\/td><td><code>tfsec . --migrate-ignores<\/code><\/td><\/tr><tr><td><code>-m, --minimum-severity<\/code><\/td><td>Sets the minimum severity level (<code>LOW<\/code>, <code>MEDIUM<\/code>, <code>HIGH<\/code>, <code>CRITICAL<\/code>).<\/td><td><code>tfsec . --minimum-severity HIGH<\/code><\/td><\/tr><tr><td><code>--no-code<\/code><\/td><td>Disables inclusion of code snippets in the output.<\/td><td><code>tfsec . --no-code<\/code><\/td><\/tr><tr><td><code>--no-color<\/code><\/td><td>Disables colored output.<\/td><td><code>tfsec . --no-color<\/code><\/td><\/tr><tr><td><code>--no-ignores<\/code><\/td><td>Forces <code>tfsec<\/code> to consider ignored checks as active failures.<\/td><td><code>tfsec . --no-ignores<\/code><\/td><\/tr><tr><td><code>--no-module-downloads<\/code><\/td><td>Prevents downloading of remote modules during scans.<\/td><td><code>tfsec . --no-module-downloads<\/code><\/td><\/tr><tr><td><code>-O, --out<\/code><\/td><td>Saves output to a file with the specified format.<\/td><td><code>tfsec . --format json --out tfsec-results.json<\/code><\/td><\/tr><tr><td><code>--print-rego-input<\/code><\/td><td>Displays JSON representation of input for Rego policies.<\/td><td><code>tfsec . --print-rego-input<\/code><\/td><\/tr><tr><td><code>--rego-only<\/code><\/td><td>Runs only Rego-based security policies.<\/td><td><code>tfsec . --rego-only<\/code><\/td><\/tr><tr><td><code>--rego-policy-dir<\/code><\/td><td>Specifies the directory containing Rego policies for security analysis.<\/td><td><code>tfsec . --rego-policy-dir policies\/<\/code><\/td><\/tr><tr><td><code>--run-statistics<\/code><\/td><td>Displays statistical insights about the scan results.<\/td><td><code>tfsec . --run-statistics<\/code><\/td><\/tr><tr><td><code>--single-thread<\/code><\/td><td>Runs checks using a single thread (useful for debugging).<\/td><td><code>tfsec . --single-thread<\/code><\/td><\/tr><tr><td><code>-s, --soft-fail<\/code><\/td><td>Runs checks but suppresses error exit codes.<\/td><td><code>tfsec . --soft-fail<\/code><\/td><\/tr><tr><td><code>--tfvars-file<\/code><\/td><td>Uses a <code>.tfvars<\/code> file to set variables for evaluation.<\/td><td><code>tfsec . --tfvars-file terraform.tfvars<\/code><\/td><\/tr><tr><td><code>--update<\/code><\/td><td>Updates <code>tfsec<\/code> to the latest version.<\/td><td><code>tfsec --update<\/code><\/td><\/tr><tr><td><code>--var-file<\/code><\/td><td>Specifies a <code>.tfvars<\/code> file (same as <code>--tfvars-file<\/code>).<\/td><td><code>tfsec . --var-file terraform.tfvars<\/code><\/td><\/tr><tr><td><code>--verbose<\/code><\/td><td>Enables verbose logging output.<\/td><td><code>tfsec . --verbose<\/code><\/td><\/tr><tr><td><code>-v, --version<\/code><\/td><td>Displays the current <code>tfsec<\/code> version.<\/td><td><code>tfsec --version<\/code><\/td><\/tr><tr><td><code>-w, --workspace<\/code><\/td><td>Defines a workspace for ignore rules.<\/td><td><code>tfsec . --workspace dev<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\ud83d\udd39 Practical Usage Examples<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1\ufe0f\u20e3 Running a Basic Scan<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">tfsec .\n<\/code><\/span><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Scans the current directory and displays detected security issues.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2\ufe0f\u20e3 Running a Scan with a Custom Configuration File<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">tfsec<\/span> . <span class=\"hljs-selector-tag\">--config-file<\/span> <span class=\"hljs-selector-tag\">tfsec-config<\/span><span class=\"hljs-selector-class\">.json<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Uses <code>tfsec-config.json<\/code> for custom security rules.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3\ufe0f\u20e3 Excluding Specific Checks<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">tfsec . --exclude AWS001,AWS002\n<\/code><\/span><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Skips <code>AWS001<\/code> and <code>AWS002<\/code> security rules.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4\ufe0f\u20e3 Filtering for Specific Checks<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">tfsec . --filter-results AWS003,AWS004\n<\/code><\/span><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Shows only results matching the specified rules.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5\ufe0f\u20e3 Excluding <code>.terraform<\/code> Modules<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">tfsec . --exclude-downloaded-modules\n<\/code><\/span><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Prevents scanning of Terraform dependency modules.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6\ufe0f\u20e3 Running <code>tfsec<\/code> with High Severity Threshold<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">tfsec . --minimum-severity HIGH\n<\/code><\/span><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Reports only <strong>HIGH and CRITICAL<\/strong> security issues.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7\ufe0f\u20e3 Exporting Scan Results to a JSON File<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">tfsec<\/span> . <span class=\"hljs-selector-tag\">--format<\/span> <span class=\"hljs-selector-tag\">json<\/span> <span class=\"hljs-selector-tag\">--out<\/span> <span class=\"hljs-selector-tag\">tfsec-results<\/span><span class=\"hljs-selector-class\">.json<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Saves results as a JSON file.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8\ufe0f\u20e3 Running <code>tfsec<\/code> in a CI\/CD Pipeline<\/strong><\/h3>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">jobs:\n  security-scan:\n    runs-on: ubuntu-latest\n    steps:\n      - name: Checkout repository\n        uses: actions\/checkout@v4\n      \n      - name: Run tfsec\n        run: |\n          tfsec . --minimum-severity HIGH --format sarif --out tfsec-results.sarif\n<\/code><\/span><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Integrates <code>tfsec<\/code> into a GitHub Actions pipeline.<\/li>\n\n\n\n<li>Runs security checks and exports results in <strong>SARIF format<\/strong> for GitHub Security Analysis.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\ud83d\udd39 Conclusion<\/strong><\/h2>\n\n\n\n<p>Using <code>tfsec<\/code> ensures <strong>secure, compliant, and well-architected Terraform code<\/strong> by detecting <strong>misconfigurations before deployment<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Best Practices for Using <code>tfsec<\/code><\/strong><\/h3>\n\n\n\n<p>\u2705 Run <code>tfsec<\/code> <strong>before every Terraform deployment<\/strong>.<br>\u2705 <strong>Exclude<\/strong> <code>.terraform\/modules\/<\/code> from scans to avoid third-party module issues.<br>\u2705 Use <code>--minimum-severity HIGH<\/code> to <strong>focus on critical risks<\/strong>.<br>\u2705 Save reports (<code>--format json --out<\/code>) for compliance tracking.<br>\u2705 Integrate <code>tfsec<\/code> into <strong>CI\/CD pipelines<\/strong> for continuous security.<\/p>\n\n\n\n<p>By following these <strong>best practices<\/strong>, you can significantly <strong>enhance the security posture<\/strong> of your Terraform infrastructure. \ud83d\ude80<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Comprehensive Guide to TFSec: Terraform Security Scanning 1. What is TFSec? TFSec is a static analysis security scanner designed to identify security vulnerabilities in Terraform Infrastructure-as-Code (IaC) configurations before they&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-48689","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=48689"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48689\/revisions"}],"predecessor-version":[{"id":48690,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/48689\/revisions\/48690"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=48689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=48689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=48689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}