top of page

Shift Left, Secure Right: How Trivy Scans IaC Before Disaster Strikes

Feb 25

5 min read

0

6

0

Infrastructure as Code (IaC) has become a critical cornerstone of modern DevOps practices, allowing teams to define and manage infrastructure through code. While IaC enables faster deployments and greater consistency, it also introduces new security challenges that, if left unchecked, can expose organizations to significant risks. That’s where the concept of “shifting left” comes in—integrating security checks early in the development cycle, rather than waiting until later phases. One tool gaining prominence in this space is Trivy (https://github.com/aquasecurity/trivy), an open-source vulnerability scanner from Aqua Security. In this post, we’ll explore how Trivy helps teams proactively detect vulnerabilities in IaC and why shifting left is vital for modern DevOps and security strategies.


What Does Shifting Left Mean?


Shifting left involves moving security and quality checks to an earlier stage of the development lifecycle. Traditionally, security scans were often done at the end—just before deployment—leading to last-minute findings, project delays, and stress for both developers and security teams. By integrating security checks into code reviews and continuous integration (CI) pipelines, teams can:


  • Identify security risks and misconfigurations earlier.

  • Reduce the number of production-level issues.

  • Lower the overall cost and complexity of fixing vulnerabilities.


When it comes to IaC, shifting left means continuously scanning your configuration files—such as Terraform, CloudFormation, and Kubernetes manifests—to catch risky configurations early in the process.


Why Proactive IaC Vulnerability Detection Matters


As more organizations embrace IaC, the surface area for potential misconfigurations or vulnerabilities increases. A single stray configuration line can compromise an entire deployment. Proactive IaC vulnerability detection provides tangible benefits:


1. Early Discovery of Security Gaps

If your YAML or Terraform code contains a misconfigured resource—like publicly exposed storage buckets—catching it before the build saves you from potential breaches down the road.


2. Reduced Remediation Costs

Fixing bugs or vulnerabilities in production can be costly and time-consuming. Addressing them at the code stage drastically cuts down on remediation overhead.


3. Continuous Improvement of Security Posture

Integrating automated scans in your CI pipeline ensures that every commit undergoes security checks. This fosters a culture of continuous security improvement.


4. Compliance and Regulatory Adherence

Many industries are subject to regulations that mandate regular security scans or vulnerability management. Automatic IaC scanning can help maintain compliance in real-time.


Introducing Trivy for IaC Scanning


Trivy is an all-in-one security scanner widely used to detect vulnerabilities in container images, file systems, and even IaC templates. Initially known for container scanning, Trivy has evolved to include:


- IaC Security Checks

Scans popular IaC frameworks such as Dockerfiles, Kubernetes resource files, and Terraform.

-Support for Multiple Targets

With a single tool, you can identify potential vulnerabilities, exposed secrets, and misconfigurations across many environments.

-Simple Integration

Trivy can easily integrate into CI/CD pipelines, local development workflows, and even GitHub Actions.


Key Features That Stand Out


1. Extensive Vulnerability Database

Trivy taps into multiple sources, including the [NVD (National Vulnerability Database)](https://nvd.nist.gov/), vendor advisories, and community feeds, ensuring comprehensive coverage.


2. Fast and Lightweight

Built to be developer-friendly, it doesn't require heavy system resources or complex setups.


3. Customizable Policies

Tailor your scans to your organization’s specific compliance or security policies.


4. User-Friendly Output

Extract meaningful insights from scan reports to quickly pinpoint high-risk issues that need attention.


Setting Up Trivy for IaC Vulnerability Detection


Below is a general guide on how to get started with Trivy for detecting IaC vulnerabilities. For the most up-to-date instructions, refer to the Trivy documentation (https://aquasecurity.github.io/trivy/ or https://trivy.dev/)


1. Installation

Trivy can be installed through package managers like Homebrew (macOS), apt (Ubuntu/Debian), or by downloading the binary directly from its GitHub releases.


For example, on macOS with Homebrew:

```bash

brew install aquasecurity/trivy/trivy

Windows binary link - https://github.com/aquasecurity/trivy/releases/


Docker: docker run aquasec/trivy

2. Scanning IaC Files


Terragoat, an intentionally vulnerable Terraform repository, was scanned using Trivy to identify security risks and misconfigurations. The scan results highlight potential weaknesses in infrastructure code, providing valuable insights for improving security practices.


trivy fs --scanners misconfig C:\work\terragoat\terraform\aws

Since Terragoat is known to contain vulnerable code for training purposes, the Trivy scan successfully identified these vulnerabilities and categorized them based on severity levels: critical, high, medium, and low.

Few example output might look like this:


AVD-AWS-0080 (HIGH): Instance does not have storage encryption enabled.
════════════════════════════════════════
Encryption should be enabled for an RDS Database instances.
When enabling encryption by setting the kms_key_id.
See https://avd.aquasec.com/misconfig/avd-aws-0080
────────────────────────────────────────
 db-app.tf:19
   via db-app.tf:1-42 (aws_db_instance.default)
────────────────────────────────────────
   1   resource "aws_db_instance" "default" {
   .
  19 [   storage_encrypted       = false
  ..
  42   }
────────────────────────────────────────


AVD-AWS-0104 (CRITICAL): Security group rule allows unrestricted egress to any IP address.
════════════════════════════════════════
Opening up ports to connect out to the public internet is generally to be avoided. You should restrict access to IP addresses or ranges that are explicitly required where possible.
See https://avd.aquasec.com/misconfig/aws-vpc-no-public-egress-sgr
────────────────────────────────────────
 db-app.tf:150
   via db-app.tf:145-152 (aws_security_group_rule.egress)
────────────────────────────────────────
 145   resource "aws_security_group_rule" "egress" {
 146     type              = "egress"
 147     from_port         = 0
 148     to_port           = 0
 149     protocol          = "-1"
 150 [   cidr_blocks       = ["0.0.0.0/0"]
 151     security_group_id = "${aws_security_group.default.id}"
 152   }
────────────────────────────────────────

Each entry describes the file, type of misconfiguration, severity, and a message explaining the issue. Use this information to prioritize and remediate the most critical risks first.


Scanning Docker Image

C:\work\terragoat\terraform\aws>trivy image python:3-slim
2025-02-25T00:32:16-06:00       INFO    [vuln] Vulnerability scanning is enabled
2025-02-25T00:32:16-06:00       INFO    [secret] Secret scanning is enabled
2025-02-25T00:32:16-06:00       INFO    [secret] If your scanning is slow, please try '--scanners vuln' to disable secret scanning
2025-02-25T00:32:16-06:00       INFO    [secret] Please see also https://aquasecurity.github.io/trivy/v0.59/docs/scanner/secret#recommendation for faster secret detection
2025-02-25T00:32:28-06:00       INFO    [python] Licenses acquired from one or more METADATA files may be subject to additional terms. Use `--debug` flag to see all affected packages.
2025-02-25T00:32:28-06:00       INFO    Detected OS     family="debian" version="12.9"
2025-02-25T00:32:28-06:00       INFO    [debian] Detecting vulnerabilities...   os_version="12" pkg_num=97
2025-02-25T00:32:28-06:00       INFO    Number of language-specific files       num=1
2025-02-25T00:32:28-06:00       INFO    [python-pkg] Detecting vulnerabilities...
2025-02-25T00:32:28-06:00       WARN    Using severities from other vendors for some vulnerabilities. Read https://aquasecurity.github.io/trivy/v0.59/docs/scanner/vulnerability#severity-selection for details.
python:3-slim (debian 12.9)
===========================
Total: 82 (UNKNOWN: 0, LOW: 58, MEDIUM: 22, HIGH: 1, CRITICAL: 1)




4. Automating in CI/CD


To truly shift left, integrate Trivy scans into your CI pipeline. For instance, if you use GitHub Actions, you can add a workflow step like:

```yaml
- name: Trivy IaC Scan
  uses: aquasecurity/trivy-action@v0.10.0
  with:
    scan-type: 'config'
    target: './infrastructure'
```

This ensures every commit triggers an IaC security scan, catching issues as soon as they are introduced.


trivy-action (Community)

GitHub Action to scan vulnerability using Trivy. If vulnerabilities are found by Trivy, it creates a GitHub Issue.

👉 Get it at: https://github.com/marketplace/actions/trivy-action


trivy-github-issues (Community)

In this action, Trivy scans the dependency files such as package-lock.json and go.sum in your repository, then create GitHub issues according to the result.

👉 Get it at: https://github.com/marketplace/actions/trivy-github-issues




Advanced Tips and Best Practices


1. Integrate Policy as Code

Tools like Conftest (https://github.com/open-policy-agent/conftest) can complement Trivy by allowing you to define custom policies for your IaC files and enforce them in a pipeline.


2. Combine Static and Runtime Scanning

Shift-left scanning is crucial, but runtime security monitoring adds another layer of protection once your infrastructure is deployed.


3. Scan Early and Often

Make scans part of your developers’ local workflow. This way, they can catch misconfigurations even before pushing code to the main repository.


Conclusion


As organizations increasingly rely on Infrastructure as Code to define and manage environments, the need for proactive vulnerability detection becomes paramount. Shifting left with Trivy empowers teams to spot and fix security risks early, reducing the likelihood of costly production issues and improving overall compliance. By integrating Trivy into your CI/CD pipelines and local development processes, you’ll foster a culture of continuous security that can keep pace with the rapidly evolving DevOps landscape.


Take the next step and start scanning your IaC files with Trivy today. The sooner you integrate it into your workflows, the more secure—and efficient—your deployments will be. Happy scanning!

Feb 25

5 min read

0

6

0

Comments

Share Your ThoughtsBe the first to write a comment.

MeKrish LLC

5830 E 2nd St Suite 8
Casper, WY 82609
USA

bottom of page