In the previous post I used Trivy to scan container images. But an image is only part of what we ship. The rest is the infrastructure around it: security groups, S3 buckets, databases. If you write that in Terraform, the same tool can check it too.
What does an IaC scan look for?
An image scan looks for known vulnerabilities in software, like an old OpenSSL version.
An IaC scan looks for risky settings in your infrastructure code. Trivy calls these misconfigurations. The code works fine. It just does something you probably don't want, for example:
- a security group that allows SSH from anywhere,
- an S3 bucket with no public access block,
- an RDS instance with unencrypted storage.
Trivy reads your .tf files and checks them against its built-in rules. It doesn't need AWS credentials and doesn't talk to AWS, so you can run it on your laptop or in CI before anything is applied.
A first scan
Here's a small main.tf with a few problems on purpose:
resource "aws_security_group" "web" { name = "web" description = "Web server" ingress { description = "SSH" from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } } resource "aws_s3_bucket" "uploads" { bucket = "my-app-uploads" } resource "aws_db_instance" "main" { identifier = "main" engine = "postgres" instance_class = "db.t4g.micro" allocated_storage = 20 username = "app" password = var.db_password skip_final_snapshot = true } variable "db_password" { type = string sensitive = true } Run Trivy on the folder:
trivy config ./terraform For these 31 lines it reports 14 findings: 7 HIGH, 4 MEDIUM and 3 LOW. That's normal for code nobody has scanned before. To see only the important ones first:
trivy config --severity HIGH,CRITICAL ./terraform main.tf (terraform) =================== Tests: 7 (SUCCESSES: 0, FAILURES: 7) Failures: 7 (HIGH: 7, CRITICAL: 0) Each finding looks like this:
AWS-0107 (HIGH): Security group rule allows unrestricted ingress from any IP address. ════════════════════════════════════════ Security groups provide stateful filtering of ingress and egress network traffic to AWS resources. It is recommended that no security group allows unrestricted ingress access to remote server administration ports, such as SSH to port 22 and RDP to port 3389. See https://avd.aquasec.com/misconfig/aws-0107 ──────────────────────────────────────── main.tf:10 via main.tf:5-11 (ingress) via main.tf:1-12 (aws_security_group.web) ──────────────────────────────────────── 1 resource "aws_security_group" "web" { . 10 [ cidr_blocks = ["0.0.0.0/0"] .. 12 } ──────────────────────────────────────── You get the check ID (AWS-0107), a severity, a short explanation, a link with the fix, and the exact file and line. Older Trivy versions print the ID as AVD-AWS-0107. It's the same check.
Fixing them
Most fixes are small:
resource "aws_security_group" "web" { # ... ingress { description = "SSH from the office VPN only" from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = [var.admin_cidr] } } resource "aws_s3_bucket_public_access_block" "uploads" { bucket = aws_s3_bucket.uploads.id block_public_acls = true block_public_policy = true ignore_public_acls = true restrict_public_buckets = true } resource "aws_db_instance" "main" { # ... storage_encrypted = true deletion_protection = true } The public access block alone clears five findings, because Trivy checks each of its four settings separately and also checks that the block exists at all. So don't panic at the first number: 14 findings doesn't mean 14 pieces of work.
Not every finding needs a fix, either. For example, Trivy flags S3 buckets that don't use your own KMS key (AWS-0132). S3 already encrypts new objects by default, so for many buckets that's a decision rather than a bug. We'll come back to how to record that decision.
What AWS gives you
AWS can find the same problems, but from the other side: it looks at the resources in your account, not at your code.
- AWS Config records the configuration of your resources and evaluates them against rules. AWS provides managed rules for common checks, and you can write your own.
- AWS Security Hub CSPM runs security standards on top of Config. Its AWS Foundational Security Best Practices standard is a ready-made set of controls, and it gathers the results from all your accounts in one place.
The checks overlap a lot. Every HIGH finding we fixed above has a matching Security Hub control:
| Problem | Trivy | Security Hub control |
|---|---|---|
| SSH open to the internet | AWS-0107 | EC2.19: Security groups should not allow unrestricted access to ports with high risk (port 22 is on its list) |
| S3 public access not blocked | AWS-0086 / 0087 / 0091 / 0093 | S3.8: S3 general purpose buckets should block public access |
| RDS storage not encrypted | AWS-0080 | RDS.3: RDS DB instances should have encryption at-rest enabled |
| RDS deletion protection off | AWS-0177 | RDS.8: RDS DB instances should have deletion protection enabled |
So the real difference isn't what they check. It's when and where.
Side by side
Trivy config | AWS Config / Security Hub CSPM | |
|---|---|---|
| Looks at | your Terraform code | real resources in your account |
| When | before terraform apply | after the resource exists, continuously |
| Changes made in the console | not seen | caught |
| Needs AWS access | no | runs inside your account |
| Where you see results | CI log, GitHub issues | AWS console, across all accounts |
| Cost | free | Config: per configuration item recorded (USD 0.003) and per rule evaluation; Security Hub CSPM: per security check (USD 0.001 for the first 100,000), plus Config's configuration items |
The cost row is the same story as Inspector in part 1: AWS's tools are priced per resource and per evaluation, so the bill grows with your account. Trivy costs nothing but a few seconds of CI time.
The "console changes" row matters too. If someone opens a port by hand at 2 a.m. during an incident, Trivy will never know, because it only reads the code. AWS Config will.
Running it in GitHub Actions
In part 1, I left the choice between blocking the build and reporting the findings up to you. For Terraform, we report: every finding becomes a GitHub issue, the build keeps going, and the issues get picked up like any other work.
name: Terraform scan on: push: branches: [main] paths: ["terraform/**"] permissions: contents: read issues: write jobs: trivy-config: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 - name: Scan Terraform with Trivy uses: aquasecurity/trivy-action@v0.36.0 with: scan-type: config scan-ref: terraform severity: MEDIUM,HIGH,CRITICAL format: json output: trivy-config.json - name: Open an issue per finding env: GH_TOKEN: ${{ github.token }} run: | # Titles we already know about: open issues, and ones closed as "not planned" known=$(gh issue list --state all --limit 1000 --json title,state,stateReason \ --jq '.[] | select(.state == "OPEN" or .stateReason == "NOT_PLANNED") | .title') jq -c '.Results[]? | .Target as $file | .Misconfigurations[]? | . + {File: $file}' trivy-config.json | while read -r finding; do title=$(jq -r '"[\(.Severity)] \(.ID): \(.CauseMetadata.Resource) - \(.Title)"' <<< "$finding") if grep -qxF -- "$title" <<< "$known"; then echo "Already tracked: $title" continue fi body=$(jq -r '"**File:** \(.File)\n**Resource:** \(.CauseMetadata.Resource)\n\n\(.Description)\n\n**How to fix:** \(.Resolution)\n\n\(.PrimaryURL)"' <<< "$finding") gh issue create --title "$title" --body "$body" done How it works:
- The scan doesn't fail the job. Without
exit-code, Trivy exits with 0 even when it finds something. It writes the results totrivy-config.json. - One issue per finding. The title has the severity, check ID and resource, for example
[HIGH] AWS-0107: aws_security_group.web - Security groups should not allow unrestricted ingress to SSH or RDP from any IP address.That makes the issue list easy to sort and search. - No duplicates. If an open issue with the same title exists, it's skipped. So the workflow can run on every push without flooding the repository.
- "Won't fix" is a closed issue. For a finding you decide to accept, like the KMS key example above, close the issue as Not planned and leave a comment explaining why. The workflow sees that and won't open it again. The decision and the reason stay in the issue history, where anyone can find them.
- Fixed means closed as completed. If the finding comes back later, the next run opens a new issue for it.
A note on cost: the GitHub Security tab could show these findings too (format: sarif), but as I mentioned in part 1, for private repositories that requires the paid GitHub Code Security add-on. Issues are free and good enough for us.
Which one should you pick?
Trivy is enough if:
- all (or almost all) of your infrastructure is in Terraform,
- you want to find problems while the code is still in a PR, not after it's live,
- you want the findings in GitHub, next to the code,
- and you'd rather not pay per resource.
Add AWS Config / Security Hub CSPM if:
- people still change things in the console,
- you have resources that were never imported into Terraform,
- you manage many accounts and want one view across all of them,
- or an auditor needs a continuous compliance report.
Or use both. They don't compete. Trivy catches the problem before it's created, and AWS catches whatever got past it or was changed by hand. If you only pick one to start with and your infrastructure is mostly Terraform, Trivy gives you the most for free.
Wrap-up
-
trivy configchecks your Terraform for risky AWS settings before anything is applied. It's free and needs no AWS access. - Expect a long list on the first run. Many findings share one fix, and some are decisions, not bugs.
- You don't have to fail the build. Turning findings into issues, and closing the ones you accept as not planned with a reason, keeps every decision on record.
- AWS Config and Security Hub CSPM check the same kinds of things, but on live resources. That's the only way to catch changes made outside Terraform.
Put together with part 1, the picture looks like this: Trivy checks your images and your Terraform before they ship, and AWS's own services watch what's actually running. Neither is the whole story on its own.

