← All articles

Infrastructure as Code Security Scanning: Catch Cloud Misconfigs Before Deploy

Infrastructure as code security scanning means running static checks against your Terraform, CloudFormation, Kubernetes manifests or Dockerfiles before they're applied, so a security group open to the internet or a publicly accessible database fails code review instead of reaching production. Open-source scanners like Checkov, Trivy and KICS do this in seconds and fit into a pull request check. The catch: they only see what's in the repo, so you still need to scan the live environment for drift, console changes and assets that were never in code.

This guide covers what IaC scanning catches, how to wire it into CI without drowning in noise, and where it stops being useful.

Why infrastructure as code security scanning pays off

Most cloud misconfigurations are written by someone, in a file, before they exist. If your team provisions with Terraform, the line cidr_blocks = ["0.0.0.0/0"] on port 22 sits in a diff for a reviewer to miss before it ever becomes an exposed SSH server. Catching it there has three advantages over catching it in a runtime scan:

  • The fix is a one-line code change rather than a console change that the next terraform apply will revert.
  • The author still has context. The engineer who wrote the module is the one reading the PR comment.
  • It never becomes exposed. Runtime scanning finds the problem after the window has opened.

The same misconfigurations attackers look for first in a live account, covered in our AWS security misconfigurations checklist, are mostly detectable statically.

What IaC security scanning catches (with Terraform examples)

Here are four patterns that IaC scanners flag reliably, shown as Terraform with the fix.

Security group open to the world

# Flagged: SSH reachable from any IPv4 address
resource "aws_security_group_rule" "ssh" {
  type              = "ingress"
  from_port         = 22
  to_port           = 22
  protocol          = "tcp"
  cidr_blocks       = ["0.0.0.0/0"]
  security_group_id = aws_security_group.app.id
}

Fix: restrict cidr_blocks to a known range, or drop SSH entirely and use AWS Systems Manager Session Manager.

Instance metadata allowing IMDSv1

resource "aws_instance" "api" {
  ami           = var.ami_id
  instance_type = "t3.small"

  metadata_options {
    http_tokens   = "required"   # "optional" allows IMDSv1, which SSRF can abuse
    http_endpoint = "enabled"
  }
}

AWS recommends requiring IMDSv2. Set http_tokens = "required" explicitly rather than relying on defaults, so the intent is visible in code and to the scanner.

Publicly accessible database

resource "aws_db_instance" "main" {
  # ...
  publicly_accessible = false  # true + a permissive security group = database on the internet
  storage_encrypted   = true
}

Public bucket ACL

resource "aws_s3_bucket_acl" "assets" {
  bucket = aws_s3_bucket.assets.id
  acl    = "public-read"   # flagged
}

Sometimes a public bucket is intended, for example a static site. That's what suppressions are for (more below).

Beyond AWS, the same scanners cover Azure and GCP resources, Kubernetes manifests (privileged containers, host networking, missing resource limits) and Dockerfiles (running as root, unpinned base images). Trivy's docs list Docker, Kubernetes, Terraform, CloudFormation, Azure ARM templates and Helm charts among supported formats.

Choosing an IaC scanner

ToolNotesTypical command
CheckovPython CLI; broad framework coverage; supports Terraform plan JSONcheckov -d infra/
TrivySingle binary that also does image CVE and secret scanning; absorbed tfsectrivy config infra/
KICSCheckmarx open-source scanner with a large query library across IaC platformssee project docs
tfsecNow part of Trivy; new projects should use trivy config—

For most small teams the choice matters less than actually running one on every PR. If you already use Trivy for container images, use trivy config for IaC and keep one tool. If you want plan-level scanning (below), Checkov documents it well. Our roundup of free and open-source automated pentesting tools covers where these fit alongside runtime tools.

Scan the plan, not just the source

Static scanning of .tf files has a blind spot: values that come from variables, modules and data sources may not be resolved. A module that takes allowed_cidrs as input looks fine in isolation; the problem is in the caller passing ["0.0.0.0/0"].

Scanning the Terraform plan fixes most of this, because the plan has the resolved values. Checkov's documented approach:

terraform plan --out tfplan.binary
terraform show -json tfplan.binary | jq > tfplan.json
checkov -f tfplan.json

Checkov notes that plan evaluation "provides Checkov additional dependencies and context that can result in a more complete scan result." Do both: scan source on every PR for fast feedback, and scan the plan in the pipeline step right before apply.

Wiring IaC scanning into CI

A minimal GitHub Actions job. Checkov exits non-zero when checks fail unless you pass --soft-fail, so this blocks the PR on findings:

name: iac-scan
on: [pull_request]
jobs:
  checkov:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install Checkov
        run: pip install checkov
      - name: Scan Terraform
        run: checkov -d infra/ --framework terraform

Or with Trivy, which exits 0 by default even when it finds issues unless you set --exit-code:

trivy config --severity HIGH,CRITICAL --exit-code 1 infra/

To block only on a chosen set of checks, Checkov's --hard-fail-on accepts specific check IDs (for example CKV_AWS_20) alongside --soft-fail-on for everything else; severity values work there too, but severity data is downloaded from Prisma Cloud, so check that your setup actually receives it. Our guide to security testing in your CI/CD pipeline covers where IaC scanning sits relative to SAST, dependency scanning and dynamic testing.

Rolling out without blocking everyone

Turning on a scanner against an existing repo usually produces dozens of findings on day one. A rollout that sticks:

  1. Week 1: report only. Run with soft-fail (checkov --soft-fail, or Trivy without --exit-code) and post results as PR comments.
  2. Triage the backlog. Fix the real ones, suppress the intended ones with a reason.
  3. Block on a short list. Hard-fail on public network exposure, public data stores, IMDSv1 and wildcard IAM first.
  4. Widen gradually. Add encryption, logging and tagging checks once the team trusts the signal.

Suppress with a reason, in code

Suppressions should live next to the resource and explain why. Checkov's inline syntax:

resource "aws_s3_bucket_acl" "marketing_site" {
  #checkov:skip=CKV_AWS_20:Public static marketing site, no user data
  bucket = aws_s3_bucket.marketing_site.id
  acl    = "public-read"
}

Reviewers can then see the exception and challenge it in the PR, which is better than a central ignore file nobody reads.

What IaC scanning won't catch

This is where teams get overconfident. Infrastructure as code security scanning is static analysis of intent. It misses:

  • Drift. Someone opens a security group in the console during an incident and never reverts it. The repo still looks clean.
  • Resources outside IaC. The bucket created by hand for a partner, the Firebase project a contractor set up, the forgotten staging account.
  • Application-layer issues. An SSRF in your code, a broken access check in your API, a secret shipped in a JavaScript bundle.
  • Exploitability. A scanner flags publicly_accessible = true; it doesn't tell you whether the database is actually reachable and answering.
  • Backend-as-a-service rules. Firebase Security Rules and Supabase RLS policies often live outside Terraform entirely. See Firebase and Supabase security misconfigurations.

That's why IaC scanning pairs with runtime, outside-in testing. SecurityInfinity's Infra-as-Code Audit covers the repo side, and the Cloud Misconfigs scanner checks the live side by auditing AWS, Azure and GCP for public resources reachable from domains you've verified you own. Scans are read-only, and each finding includes evidence and the exact fix. For what to expect from the runtime half, see what a cloud vulnerability scanner should catch.

If a customer or auditor needs third-party evidence on top of your own scanning, our Startup Pentest delivers a report and signed attestation letter. Otherwise, start a free scan to see whether your live environment matches what your Terraform says it should be.

Frequently asked questions

What is infrastructure as code security scanning?

It's static analysis of infrastructure definitions such as Terraform, CloudFormation, Kubernetes YAML, Helm charts and Dockerfiles to find security misconfigurations before they're deployed. Typical findings include network rules open to the internet, publicly accessible databases, missing encryption and over-permissive IAM policies. It usually runs in the IDE, as a pre-commit hook or in CI.

What is the best free IaC security scanner?

Checkov, Trivy and KICS are the most widely used open-source options, and all three cover Terraform, CloudFormation and Kubernetes. Trivy is convenient if you already use it for container scanning, since tfsec's checks were merged into it. Checkov has well-documented Terraform plan scanning. Pick one and run it on every pull request; consistency matters more than the brand.

Should I scan Terraform source files or the plan?

Both. Scanning source files on every pull request gives fast feedback, but it can miss values resolved from variables and modules. Scanning the JSON output of terraform plan before apply sees resolved values and catches misconfigurations that only appear when modules are wired together.

Does IaC scanning replace cloud security scanning?

No. IaC scanning only sees what's defined in code. It misses console changes, drift, resources created outside Terraform and application-layer vulnerabilities. You still need runtime scanning of the live environment, ideally from the outside, to confirm what is actually exposed.

How do I stop IaC scanners from blocking every pull request?

Start in report-only mode, triage the existing findings, and suppress intentional exceptions inline with a written reason. Then fail builds only on a short list of high-impact checks, such as public network exposure, public data stores and wildcard IAM, and widen the list as the team gains confidence in the results.

Find what attackers can see, before they do.

AI pentesting agents test your web app, API and cloud, and every finding comes with evidence and a fix. First report in about 10 minutes.

Stay ahead of attackers.

New vulnerabilities, breach write-ups and practical fixes. A few emails a month.