Sub topic 9
Scanning images for vulnerabilities
This chapter assumes you can create an ECR repository, push an image with Docker, and read basic IAM policies. A container image contains an operating system layer and application packages. Any of them can include a known vulnerability, which is a flaw that has a public identifier called a CVE (Common Vulnerabilities and Exposures). Image scanning compares the packages in an image against databases of known CVEs and reports what it finds.
Basic and enhanced scanning
ECR offers two scanning types. You choose the type at the registry level, and it applies to every repository in that registry in that Region.
| Feature | Basic scanning | Enhanced scanning |
|---|---|---|
| Powered by | ECR, using the Clair open source project (AWS native technology in newer setups) | Amazon Inspector |
| Covers | Operating system packages | Operating system packages and programming language packages |
| When it runs | On push, or manually | On push, and continuously as new CVEs are published |
| Extra cost | None | Inspector charges apply |
| Where findings appear | ECR console and API | ECR, Inspector, and Amazon EventBridge |
Basic scanning is a good starting point for small projects. Choose enhanced scanning when you need language-package coverage (for example Python or npm dependencies) or when you want old images to be re-evaluated as new CVEs appear.
Turning on scan on push
In the earlier chapter you created a repository with scanOnPush=true. You can also enable it on an existing repository.
aws ecr put-image-scanning-configuration \
--repository-name my-app \
--image-scanning-configuration scanOnPush=true \
--region us-east-1With basic scanning, each push then starts a scan automatically. You can also start one by hand for a specific image.
aws ecr start-image-scan \
--repository-name my-app \
--image-id imageTag=1.0.0 \
--region us-east-1Basic scanning allows one manual scan per image every 24 hours.
Reading the findings
Scans run asynchronously, so the result is not available the instant a push finishes. Wait until the status is COMPLETE, then read the findings.
aws ecr describe-image-scan-findings \
--repository-name my-app \
--image-id imageTag=1.0.0 \
--region us-east-1 \
--query 'imageScanFindings.findingSeverityCounts'The output is a count per severity level, for example {"CRITICAL": 1, "HIGH": 4, "MEDIUM": 9}. Severity levels are CRITICAL, HIGH, MEDIUM, LOW, INFORMATIONAL, and UNDEFINED. Remove the --query option to see each finding with its CVE ID, the affected package, and its version.
Common errors and gotchas
- Reading findings too early. Calling
describe-image-scan-findingsbefore the scan finishes returns aScanNotFoundExceptionerror or an in-progress status. Useaws ecr wait image-scan-completeto block until it is done. - Treating a clean scan as permanent. With basic scanning, a scan reflects the CVE database at the moment it ran. An image that was clean last month may be vulnerable today. Enhanced scanning removes this gap.
- Expecting scanning to block pushes or deploys. ECR only reports. It never rejects an image. To enforce a rule, add a pipeline step that reads the severity counts and fails the build.
- Fixing the wrong layer. Most findings come from the base image. Rebuilding on an updated base image often clears many findings at once.
- Switching scan types without checking permissions. Enhanced scanning needs Amazon Inspector enabled for the account, which uses a service-linked role that Inspector creates.
aws ecr wait image-scan-complete \
--repository-name my-app \
--image-id imageTag=1.0.0 \
--region us-east-1Exercise
Your team pushes a Python service that depends on many pip packages. A basic scan reports no findings, yet a security reviewer says vulnerable pip packages may be present. What is the likely cause, and what change would address it?
Show answer
Basic scanning checks only operating system packages, so it does not see vulnerable Python (pip) packages. Switching the registry to enhanced scanning, which uses Amazon Inspector, adds language-package coverage and also rescans images continuously as new CVEs are published.