Terraform Security Scanning
A building inspector reviews the blueprints before construction begins. Fixing a missing fire exit on paper costs almost nothing. Fixing it after the walls go up costs a fortune. Security scanners play the inspector for Terraform. They read your code before anything reaches the cloud and flag risky settings such as open firewalls or unencrypted storage. Early checks protect your data and your budget.
What a Scanner Looks For
Scanners compare your configuration against a library of known good practices. Each rule describes one risky pattern.
| Risky Pattern | Why It Matters |
|---|---|
| Firewall open to every address | Anyone on the internet can reach the server |
| Storage without encryption | Stored data stays readable if a disk is exposed |
| Public storage buckets | Private files become visible to everyone |
| Database open to the internet | Sensitive records face unnecessary exposure |
| Passwords written in code | Anyone with repository access can read them |
| Missing logging | Teams cannot investigate problems later |
Shift Left: Check Early
Shift left means moving checks toward the start of the work. A problem found while writing code costs minutes. A problem found in production costs days.
Diagram: Where Scanning Fits
Write code --> Format --> Validate --> SCAN --> Plan --> Review --> Apply
^
|
risky settings stop the pipeline herePopular Scanning Tools
| Tool | Strength |
|---|---|
| Trivy | Scans Terraform, containers, and more. It absorbed the older tfsec project. |
| Checkov | Large rule library with support for many frameworks |
| Terrascan | Policy checks aligned with common compliance standards |
| OPA and Sentinel | Custom rules written by your own team |
Scanning With Trivy
Trivy reads Terraform files directly. No cloud login is needed.
trivy config .The report lists each finding with a severity, the file, the line number, and a short fix suggestion.
Scanning With Checkov
pip install checkov
checkov -d .Checkov prints passed and failed checks with a link to guidance for each failure.
A Risky Example and Its Fix
Before: Open Firewall
resource "aws_security_group" "web" {
name = "web-sg"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}The scanner flags the rule because every internet address can attempt a connection to port 22.
After: Limited Access
resource "aws_security_group" "web" {
name = "web-sg"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["203.0.113.0/24"]
}
}Only the office network range can connect now.
Fix: Encrypted Storage
resource "aws_ebs_volume" "data" {
availability_zone = "us-east-1a"
size = 50
encrypted = true
}Setting encrypted = true satisfies the rule that flagged the original volume.
Handling Findings
Each finding deserves a decision.
- Fix the issue when the finding is valid. Most findings fall here.
- Document an exception when the risk is accepted on purpose.
- Suppress a rule with an inline comment only when you can explain the reason to a reviewer.
#trivy:ignore:AVD-AWS-0107
ingress {
# Reason: public load balancer receives web traffic by design
}Adding Scans to a Pipeline
- name: Scan Terraform
run: trivy config --exit-code 1 --severity HIGH,CRITICAL .The --exit-code 1 option makes the step fail when serious findings exist. A failed step blocks the merge.
Policy as Code
Built-in rules cover general practices. Companies also write their own rules, such as requiring an owner tag on every resource. Tools like Open Policy Agent and Sentinel evaluate those rules against the plan output. A plan that breaks a rule never reaches the apply stage.
Key Points
- Scanners read Terraform code and flag risky settings before deployment.
- Early checks cost far less than fixes in production.
- Trivy and Checkov run with one command and need no cloud login.
- Common findings include open firewalls, public buckets, and missing encryption.
- Fix findings first, and document any exception you accept on purpose.
- Pipeline steps that fail on serious findings enforce the rules for the whole team.
- Policy as code adds company-specific rules on top of built-in checks.
