Infrastructure as Code Security Scanning
Scan Terraform, CloudFormation, Kubernetes YAML, and Dockerfiles for security misconfigurations with Trivy — severity filtering, custom Rego policies, CI gate integration, SARIF output, and compliance frameworks.
Content
Overview
Trivy scans IaC templates for security misconfigurations before they reach production. It analyzes Terraform (HCL and plan files), CloudFormation (JSON/YAML), Kubernetes manifests, Dockerfiles, and Helm charts against hundreds of built-in security checks. The scans run locally in seconds with no network calls required — making them practical for pre-commit hooks and CI pipelines.
Infrastructure misconfigurations cause more breaches than application vulnerabilities. A public S3 bucket, an overly permissive security group, or a container running as root are all deployment-time decisions that Trivy catches before they become incidents.
Scanning Terraform
Scanning Terraform plan files is more thorough than scanning .tf files directly. Plan files contain resolved variables, module outputs, and provider-specific defaults — catching issues that static HCL analysis misses (e.g., a variable that defaults to "0.0.0.0/0").
Common Terraform Findings
| Check | Severity | Description |
|---|---|---|
| AVD-AWS-0086 | CRITICAL | S3 bucket without encryption |
| AVD-AWS-0107 | HIGH | Security group allows 0.0.0.0/0 ingress |
| AVD-AWS-0176 | HIGH | RDS instance publicly accessible |
| AVD-AWS-0057 | CRITICAL | IAM policy with wildcard (*) actions |
| AVD-AWS-0026 | HIGH | EBS volume not encrypted |
| AVD-AWS-0089 | MEDIUM | S3 bucket without versioning |
| AVD-AWS-0132 | HIGH | CloudTrail not enabled |
Scanning Kubernetes Manifests
Always scan Helm charts after rendering with helm template. Scanning the raw chart templates misses issues that only appear with specific values (e.g., securityContext that is conditionally included).
Common Kubernetes Findings
| Check | Severity | Description |
|---|---|---|
| KSV001 | MEDIUM | Container running as root |
| KSV003 | HIGH | Default capabilities not dropped |
| KSV006 | HIGH | Privileged container |
| KSV011 | LOW | CPU limits not set |
| KSV012 | LOW | Memory limits not set |
| KSV014 | HIGH | Root filesystem not read-only |
| KSV020 | MEDIUM | Container running with low UID |
| KSV021 | MEDIUM | No network policy defined |
| KSV106 | MEDIUM | Secrets in environment variables |
Scanning Dockerfiles
Common Dockerfile Findings
Custom Rego Policies
Trivy's built-in checks cover common misconfigurations, but every organization has specific requirements. Write custom checks in Rego (OPA's policy language) to enforce your standards.
Policy Structure
Run with Custom Policies
Example: Require Tags on All Resources
Output Formats and CI Integration
GitHub Actions Integration
The if: always() ensures the SARIF upload happens even when the scan fails (exit code 1), so findings appear in the GitHub Security tab regardless of the gate result.
GitLab CI Integration
Ignoring Findings
Always document the justification for each ignored finding. An uncommented .trivyignore is a liability — it silently suppresses real issues without context.
Scanning Multiple IaC Types Together
Best Practices
- -Scan IaC in the same CI pipeline as application code — do not defer security to a separate "security review" stage.
- -Use
--exit-code 1to block deployments with CRITICAL and HIGH misconfigurations. Start strict and relax selectively. - -Scan Terraform plans, not just
.tffiles — plans resolve variables, modules, and defaults that static analysis misses. - -Render Helm charts before scanning —
helm template | trivy config -catches value-dependent misconfigurations. - -Write custom Rego policies for organization-specific standards (naming conventions, required tags, region restrictions).
- -Test custom policies with
opa testin CI — policies are code and need the same rigor. - -Use SARIF output for GitHub Security tab integration — findings appear alongside code scanning alerts.
- -Combine IaC scanning with container scanning for full supply chain coverage.
Common Pitfalls
- -Scanning only container images but not IaC — infrastructure misconfigurations cause more breaches than application vulnerabilities.
- -Ignoring MEDIUM severity findings — they compound. A non-encrypted volume plus a public security group equals a data breach.
- -Not scanning Dockerfiles — running as root with unpinned base images is the default, and it is insecure.
- -Scanning IaC only in production pipelines — scan in development and PR checks to catch issues early.
- -Writing
.trivyignoreentries without justification — creates silent suppression of real issues. - -Not scanning Helm charts after rendering — raw templates may not reveal value-dependent misconfigurations.
FAQ
Discussion
Loading comments...