AWS Builder Center

Optimizing CI/CD Pipelines for Vulnerability Detection: Risk-Based Scanning with Python and AWS

A practical guide to fixing the two failure modes of pipeline security scanning: too slow to run on every build, and too noisy to act on when it does.

Google cloud certified professional machine learning engineer

Why This Topic

AWS Builder Center content that performs well shares a consistent shape: a real implementation experience, a step by step build, an explained architecture decision, and lessons pulled from an actual pain point, not abstract theory. Security scanning content on the platform tends to stop at "add a scanner to your pipeline," Amazon Inspector, ECR image scanning, a dependency checker step. Far less content addresses what actually happens after teams do that: scans that take fifteen minutes and get skipped under deadline pressure, or scanners that report four hundred findings a week until engineers stop reading them entirely.
CI/CD vulnerability scanning has a well known adoption problem, not a detection problem. This article addresses the optimization layer, making scans fast enough to run on every build and making findings prioritized enough to actually get fixed, using Python to bridge AWS's native scanning tools with a risk-based triage layer. This sits directly in your niche, Observability Engineering and DevOps Automation with Python, applied to the point where security tooling and pipeline engineering meet.



The Problem: Scanning Gets Skipped, and Findings Get Ignored

Two distinct failure modes show up almost everywhere vulnerability scanning is bolted onto CI/CD:
Speed. A full dependency and container image scan that takes ten to fifteen minutes gets excluded from the fast feedback loop developers actually use. It moves to a nightly job, which means a vulnerable dependency merged Monday morning isn't flagged until Tuesday's report, well after it's already in a deployed build.
Signal. Vulnerability scanners report by severity (critical, high, medium, low) using CVSS scores that describe theoretical severity, not whether the vulnerable code path is actually reachable in your application. A high severity finding in a dependency function your code never calls generates exactly as much alert fatigue as one in a function called on every request, until teams stop distinguishing between them and start ignoring both.
Fixing this isn't about adding more scanning, most teams already have enough coverage. It's about making scanning fast enough to stay in the fast path, and making findings specific enough to act on.



The Architecture: Fast Path Scanning with Risk-Based Triage

[ Pull Request ]
|
v
[ Incremental Scan: only changed dependencies/layers ] <- fast path, every PR
|
v
[ Findings from Amazon Inspector / ECR scan / dependency scanner ]
|
v
[ Python Risk Scorer: CVSS + reachability + exposure context ]
|
score above threshold?
|
+---+---+
| |
no yes
| |
[ log, [ Block merge + annotated PR comment
don't with exact finding and fix guidance ]
block ]
|
v
[ Nightly Full Scan: complete dependency tree, all layers, trend tracking ]
The core idea: split scanning into two tiers. A fast, incremental scan runs on every pull request and only blocks merges for findings that clear a risk threshold, not every CVSS-rated finding. A comprehensive nightly scan catches everything else and tracks trends, without sitting in the developer's fast feedback loop.



Step 1: Incremental Scanning, Only What Changed

Running a full dependency tree scan on every commit is what makes scans slow. Scoping the scan to only the dependencies that actually changed in a given PR cuts that time dramatically:
python
import subprocess
import json

def get_changed_dependencies(base_ref="main"):
"""Diffs the lockfile against the base branch to isolate exactly
which dependencies changed in this PR, rather than rescanning everything."""
diff_output = subprocess.run(
["git", "diff", f"{base_ref}...HEAD", "--", "requirements.txt", "poetry.lock"],
capture_output=True, text=True,
).stdout

added_packages = []
for line in diff_output.splitlines():
if line.startswith("+") and not line.startswith("+++"):
package_line = line[1:].strip()
if "==" in package_line:
name, version = package_line.split("==")
added_packages.append({"name": name.strip(), "version": version.strip()})

return added_packages

def scan_changed_packages(packages):
"""Scans only the changed packages against a vulnerability database,
instead of the full dependency tree."""
findings = []
for pkg in packages:
result = subprocess.run(
["pip-audit", "--package", f"{pkg['name']}=={pkg['version']}", "--format", "json"],
capture_output=True, text=True,
)
if result.returncode != 0 and result.stdout:
findings.extend(json.loads(result.stdout).get("vulnerabilities", []))
return findings
On a typical PR that changes two or three dependencies, this reduces scan scope from hundreds of packages to a handful, which is what actually makes sub-two-minute scanning on every PR realistic instead of aspirational.



Step 2: Risk Scoring Beyond Raw CVSS

This is the step that solves the noise problem. Instead of treating every CVSS-rated finding as equally urgent, combine severity with reachability and deployment context to produce an actionable priority score:
python
import ast
from dataclasses import dataclass

@dataclass
class RiskFactors:
cvss_score: float
is_reachable: bool # is the vulnerable function actually called in our code
is_internet_facing: bool # does this service accept external traffic
has_known_exploit: bool # is there a known exploit in the wild (via CVE metadata)

def calculate_risk_score(factors: RiskFactors) -> float:
"""Produces a 0-10 actionable risk score, distinct from the raw CVSS score,
by weighting exploitability and exposure alongside theoretical severity."""
score = factors.cvss_score * 0.4 # base severity still matters, but isn't the whole picture

if factors.is_reachable:
score += 3.0
if factors.is_internet_facing:
score += 2.0
if factors.has_known_exploit:
score += 1.5

return min(round(score, 1), 10.0)

def check_reachability(package_name, vulnerable_function, source_dir="src"):
"""Static analysis check: does any code in this repo actually call the
vulnerable function, or is the dependency present but that code path unused."""
import os

for root, _, files in os.walk(source_dir):
for file in files:
if not file.endswith(".py"):
continue
filepath = os.path.join(root, file)
try:
with open(filepath) as f:
tree = ast.parse(f.read())
except SyntaxError:
continue

for node in ast.walk(tree):
if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
if node.func.attr == vulnerable_function:
return True
return False
A high CVSS finding with  is_reachable=False  and  is_internet_facing=False  scores meaningfully lower than the same CVSS finding in a directly called, internet facing code path. That's the actual distinction engineers need to triage findings quickly, and it's exactly the context raw CVSS scores don't provide on their own.



Step 3: Blocking Merges Selectively, Not Universally

The risk score determines whether a finding blocks the merge or simply gets logged for the nightly report to catch:
python
BLOCK_THRESHOLD = 7.0

def evaluate_pr_findings(findings_with_scores):
"""Returns (should_block, blocking_findings, informational_findings)."""
blocking = [f for f in findings_with_scores if f["risk_score"] >= BLOCK_THRESHOLD]
informational = [f for f in findings_with_scores if f["risk_score"] < BLOCK_THRESHOLD]

return len(blocking) > 0, blocking, informational

def format_pr_comment(blocking_findings, informational_findings):
lines = []
if blocking_findings:
lines.append("## Blocking vulnerabilities found\n")
for f in blocking_findings:
lines.append(
f"- **{f['package']}** ({f['cve_id']}): risk score {f['risk_score']}/10, "
f"reachable={f['is_reachable']}. Fix: upgrade to {f.get('fixed_version', 'latest')}."
)
if informational_findings:
lines.append(f"\n<details><summary>{len(informational_findings)} lower-risk findings (non-blocking)</summary>\n")
for f in informational_findings:
lines.append(f"- {f['package']} ({f['cve_id']}): risk score {f['risk_score']}/10")
lines.append("</details>")

return "\n".join(lines)
The collapsed, non-blocking section for lower-risk findings matters here as much as the blocking logic does. It keeps the information available for anyone who wants it, without forcing every developer to scroll past forty low-priority findings to find the two that actually need fixing before merge.



Real World Scenario: What This Changes

A team scanning on every PR previously saw scans take twelve minutes and routinely got skipped or run asynchronously after merge under deadline pressure, meaning vulnerable dependencies regularly reached production before anyone reviewed the scan output. After scoping scans to changed dependencies only, PR scan time drops to under ninety seconds, fast enough to stay in the required check list without developer pushback. In the same week, the risk scoring layer reduces blocking findings from an average of eleven per week down to two, both genuinely reachable, internet facing vulnerabilities, while the other nine lower-risk findings still get tracked in the nightly report instead of disappearing. The two remaining findings get fixed within the day, instead of sitting in an ignored backlog of forty.



Common Pitfalls

  • Scanning the full dependency tree on every PR. This is what makes scans too slow to stay in the fast path. Scope to changed dependencies for PR-time scans, run comprehensive scans nightly.
  • Treating CVSS score as the only signal. Raw severity without reachability and exposure context is exactly what produces alert fatigue. Combine them into a single actionable score.
  • Blocking merges on every finding regardless of risk. This trains developers to find workarounds around the security gate entirely. Block selectively, log everything else visibly.
  • Skipping the nightly comprehensive scan. The fast PR-time scan is intentionally narrow in scope. A full scan still needs to run regularly to catch vulnerabilities in unchanged dependencies as new CVEs get published against them.
  • Static reachability analysis without acknowledging its limits. AST-based reachability checks catch direct calls well but can miss dynamic dispatch or reflection-based calls. Treat it as a strong signal, not a guarantee, and pair it with runtime monitoring where feasible.



Closing Thought

The gap in most CI/CD security scanning setups isn't detection coverage, it's the layer between detection and action: scans too slow to stay in the fast path, and findings too undifferentiated to prioritize. Splitting scanning into a fast, scoped PR-time check and a comprehensive nightly sweep, combined with a risk score that accounts for reachability and exposure rather than raw CVSS alone, is what turns vulnerability scanning from a compliance checkbox teams route around into a gate that actually catches what matters, without becoming the reason a deploy gets delayed for a finding nobody can act on.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article