Sub topic 9
Scanning images for vulnerabilities
This chapter assumes you can push an image to an ECR repository, that you know what a CVE (Common Vulnerabilities and Exposures) identifier is, and that your AWS CLI credentials are configured. Earlier chapters created the my-app repository with scanOnPush=true. Here you will see what that setting does, what the findings contain, and when to switch to enhanced scanning.
What an image scan does
A scan unpacks the image layers, lists the software packages it finds, and compares each package version against a database of known vulnerabilities. Each match is a finding with a CVE ID, a severity, and, where known, the package version that fixes it.
A scan only reports known problems in installed packages. It does not find bugs in your own code, leaked secrets, or insecure configuration. A clean scan means no known CVEs matched, not that the image is safe.
ECR offers two scan types, and you choose one for the whole registry:
| Aspect | Basic scanning | Enhanced scanning |
|---|---|---|
| Engine | ECR's built-in scanner using the CVE database | Amazon Inspector |
| What it checks | Operating system packages | Operating system packages and programming-language packages (for example Python, Java, Node.js dependencies) |
| When it runs | On push, or manually | On push, or continuously |
| Rescans on new CVEs | No. You trigger a scan again | Yes, with continuous scanning |
| Extra cost | None | Amazon Inspector charges apply |
| Where findings appear | ECR console and API | ECR, Amazon Inspector, and EventBridge |
Basic scanning
You already enabled basic scanning on one repository with scanOnPush=true. You can also change it later:
aws ecr put-image-scanning-configuration \
--repository-name my-app \
--image-scanning-configuration scanOnPush=true \
--region us-east-1To scan an image that is already stored, for example one pushed before you enabled the setting, start a manual scan:
aws ecr start-image-scan \
--repository-name my-app \
--image-id imageTag=1.0.0 \
--region us-east-1Basic scanning limits manual scans of the same image to one per 24 hours, so a repeat call within that window returns an error. A scan takes from seconds to a few minutes. Instead of polling by hand, wait for it to finish:
aws ecr wait image-scan-complete \
--repository-name my-app \
--image-id imageTag=1.0.0 \
--region us-east-1Reading findings
Start with the summary, which counts findings by severity:
aws ecr describe-image-scan-findings \
--repository-name my-app \
--image-id imageTag=1.0.0 \
--region us-east-1 \
--query 'imageScanFindings.findingSeverityCounts'A typical result looks like {"CRITICAL": 1, "HIGH": 4, "MEDIUM": 12, "LOW": 7}. To see the individual findings, list the critical and high ones with the package name and version:
aws ecr describe-image-scan-findings \
--repository-name my-app \
--image-id imageTag=1.0.0 \
--region us-east-1 \
--query 'imageScanFindings.findings[?severity==`CRITICAL` || severity==`HIGH`].{cve:name,severity:severity,uri:uri,attrs:attributes}'Severity values are CRITICAL, HIGH, MEDIUM, LOW, INFORMATIONAL, and UNDEFINED. UNDEFINED means the source database has no rating for that CVE yet.
The scan status is reported alongside the findings. Check imageScanStatus.status. A status of COMPLETE means findings are valid. FAILED includes a description, and UNSUPPORTED_IMAGE appears when the image's operating system is not supported by the scanner. Images built on scratch or some distroless bases can fall in this group, because there is no package database to read.
Enhanced scanning
Enhanced scanning is configured at the registry level, not per repository. You turn it on once for the Region, then use filters to choose which repositories are scanned and how. Amazon Inspector must be able to operate in the account. ECR creates the required service-linked role when you enable enhanced scanning.
Create a file rules.json:
[
{
"scanFrequency": "CONTINUOUS_SCAN",
"repositoryFilters": [
{
"filter": "prod-*",
"filterType": "WILDCARD"
}
]
},
{
"scanFrequency": "SCAN_ON_PUSH",
"repositoryFilters": [
{
"filter": "*",
"filterType": "WILDCARD"
}
]
}
]Then apply it:
aws ecr put-registry-scanning-configuration \
--scan-type ENHANCED \
--rules file://rules.json \
--region us-east-1With this configuration, repositories whose names start with prod- are scanned continuously, and every other repository is scanned on push. The two scan frequencies for enhanced scanning are:
SCAN_ON_PUSH: the image is scanned once when it is pushed. New CVEs published later are not checked.CONTINUOUS_SCAN: the image is scanned at push and then rescanned whenever Inspector learns about a new CVE that affects its packages, or when the image's packages change.
Continuous scanning is what makes enhanced scanning valuable. An image you pushed six months ago and never touched can gain a critical finding today, and basic scanning will not tell you.
Switching back to basic scanning uses the same command with --scan-type BASIC. Findings from the earlier scan type stay readable, but new scans use the new engine, and you should expect different counts for the same image because the two engines use different data sources and cover different package types.
Retention of continuous scans
Inspector does not rescan old images forever. For continuous scanning, ECR stops monitoring an image after a configured period measured from the push date or the last pull date, and the image's findings are then closed. The registry's scanning settings control this period. If you keep rarely pulled images for rollback, check that the period covers them.
Getting findings out of the console
Scans produce events, so you can act on results without checking by hand.
- Basic scanning emits an Amazon EventBridge event named
ECR Image Scanwhen a scan completes. Its detail includes the scan status and a count per severity. - Enhanced scanning emits
ECR Image Scanevents for scan completion and also sends findings to Inspector. Inspector emits its own finding events and can aggregate results across accounts under AWS Organizations.
A common pattern routes events where critical or high counts are greater than zero to an SNS topic or a ticket queue.
Using scan results in a pipeline
ECR does not block a push or a pull because of findings. Scanning is advisory. To enforce a policy, add a check in your build or deploy pipeline. This script fails when an image has any critical finding:
#!/usr/bin/env bash
set -euo pipefail
REPO=my-app
TAG=1.0.0
REGION=us-east-1
aws ecr wait image-scan-complete \
--repository-name "$REPO" --image-id imageTag="$TAG" --region "$REGION"
CRITICAL=$(aws ecr describe-image-scan-findings \
--repository-name "$REPO" --image-id imageTag="$TAG" --region "$REGION" \
--query 'imageScanFindings.findingSeverityCounts.CRITICAL' --output text)
if [ "$CRITICAL" != "None" ] && [ "$CRITICAL" -gt 0 ]; then
echo "Found $CRITICAL critical findings in $REPO:$TAG" >&2
exit 1
fi
echo "No critical findings"The None check matters. When no critical findings exist, the key is missing from the severity counts, and the CLI prints None.
Because your repository uses immutable tags, my-app:1.0.0 always refers to one image, so the result you check is the result you deploy. If you gate on a mutable tag such as latest, the image could change between the check and the deployment. Gate on the digest where you can.
Common problems
- Scan stays
IN_PROGRESSor fails. Check thedescriptionfield inimageScanStatus. Unsupported base images are the usual cause. ScanNotFoundExceptionor no findings. The image was never scanned, or you are looking at a different Region. Findings are per Region.- Counts differ between basic and enhanced. The engines use different databases and enhanced also covers language packages, so enhanced usually reports more.
- Findings with no fix. Many findings have no patched version yet. Prioritize those with a fix available, and rebuild on a newer base image regularly.
- Access denied. Reading findings needs
ecr:DescribeImageScanFindings, starting a scan needsecr:StartImageScan, and changing registry scan settings needsecr:PutRegistryScanningConfiguration. Enhanced scanning also needs the permissions to enable Inspector.
Choosing between basic and enhanced
Choose basic scanning when your images are mostly operating system packages, you want no extra cost, and you check images at build time. Choose enhanced scanning when your application bundles language dependencies, when images stay deployed long after they were built, or when you need findings across many accounts in Inspector.
Many teams use enhanced scanning with continuous scans for production repositories and scan on push everywhere else, as the example rules do.
Exercise
Your registry uses basic scanning. A service image pushed three months ago passed with no critical findings. Today a critical CVE is published for one of its packages. Will ECR report it, and what would you change so it does?
Show answer
No. Basic scanning only runs on push or when you start a manual scan, so the old result stays unchanged until someone rescans (at most once per 24 hours per image). Switch the registry to enhanced scanning with CONTINUOUS_SCAN for that repository, using aws ecr put-registry-scanning-configuration --scan-type ENHANCED --rules file://rules.json. Inspector then rescans the image when the new CVE is published and sends the finding to ECR, Inspector, and EventBridge.
Exercise
A pipeline step runs aws ecr describe-image-scan-findings and passes because the findings list is empty. Later you find the image had known vulnerabilities. Name one likely cause and one check that prevents it.
Show answer
The scan did not complete successfully, for example imageScanStatus.status was FAILED or UNSUPPORTED_IMAGE, which also returns no findings. Run aws ecr wait image-scan-complete and verify the status is COMPLETE before reading the findings.