Image Builder and ECR Image Scanning
Automate hardened AMIs and container images with EC2 Image Builder and find vulnerabilities in ECR with basic or enhanced scanning.
Exam tasks: 3.2 (hardened AMIs and container images with Systems Manager and Image Builder)
The decision: how do you make every AMI and container image start hardened and patched, and keep known-vulnerable images out of production?
Instances in an Auto Scaling group are disposable, so patches belong in the image, not on the running host. EC2 Image Builder automates that. Amazon ECR scanning then checks container images for known CVEs.
EC2 Image Builder
| Item | What to remember |
|---|---|
| Pipeline | Automation that runs a recipe on a schedule and distributes the output |
| Recipe | Base image plus ordered build and test components. Container recipes produce Docker images |
| Components | YAML or JSON documents. AWS provides public ones, including STIG hardening components |
| Stages | Build, validate, test. The image is distributed only if tests pass |
| Distribution | Multiple Regions, KMS encryption of the AMI, sharing across accounts and Organizations |
| Sharing | Components, images and recipes can be shared with AWS RAM |
| Cost | No charge for Image Builder itself. You pay for the instances, storage and scanning it uses |
| Output | AMI or container image (pushed to ECR) |
Image Builder launches temporary instances, so their instance profile must carry these managed policies.
| Policy | Purpose |
|---|---|
| EC2InstanceProfileForImageBuilder | Minimum permissions to build an AMI |
| EC2InstanceProfileForImageBuilderECRContainerBuilds | ECR access for container builds |
| AmazonSSMManagedInstanceCore | Systems Manager actions used during the build |
s3:PutObject (custom) | Needed only to write build logs to an S3 bucket |
For cross-account AMI copies, create the EC2ImageBuilderDistributionCrossAccountRole in each target account.
Golden AMI details
- AWS provides hardening components for CIS benchmarks and DISA STIGs, plus agents such as the CloudWatch agent.
- Image Builder can run an Inspector scan on the build output and fail the pipeline on findings above a threshold.
- Distribution re-encrypts with a KMS key per Region. Target accounts need
kms:Decryptandkms:CreateGranton the key in its key policy. - Lifecycle policies deprecate and delete old images, so nobody launches a two-year-old AMI.
- Image Builder drives its build instances through Systems Manager, so they need no SSH.
Keep people on the golden images.
- Allowed AMIs (an EC2 account setting, managed by declarative policies in Organizations) restricts which AMI owners or names can be launched.
- Block public access for AMIs stops accidental public sharing.
- Treat instances as immutable. Fix the image and replace instances rather than patching the fleet by hand. Use Patch Manager for long-lived servers (see Vulnerability scanning and patching).
Exam signal
"Every new AMI must include the security agent and pass the CIS benchmark before any team can use it, across 20
accounts" is an Image Builder pipeline with hardening and test components, an Inspector scan, and distribution
to the OUs. Pair it with Allowed AMIs or an SCP condition on ec2:Owner to make the golden images mandatory.
Hardened container images
- Start from a minimal base (distroless, Alpine, Bottlerocket for nodes). Fewer packages mean fewer CVEs.
- Run as a non-root user, drop Linux capabilities, and use a read-only root file system in the task definition or pod spec.
- Never bake secrets into layers. Every layer is readable by anyone who can pull the image. Inject secrets at run time from Secrets Manager or Parameter Store.
- Turn on ECR tag immutability so a trusted tag can't be overwritten, and deploy by digest.
- Sign images (AWS Signer with Notation) and verify signatures at admission in EKS.
- Scan with Inspector enhanced scanning on push and continuously.
ECR image scanning
| Type | Engine | Scope | Frequencies |
|---|---|---|---|
| Basic | AWS native scanning with the CVE database | Operating system packages | Scan on push, or manual |
| Enhanced | Amazon Inspector | OS and programming language packages | Scan on push, or continuous |
- Enhanced scanning keeps results current as new CVEs appear, and Inspector emits an EventBridge event for changes.
- Switching between basic and enhanced makes earlier scan results unavailable until you switch back.
- Pipeline pattern. Scan on push, read findings through
DescribeImageScanFindings, fail the CI/CD stage on Critical findings, and alert through SNS or CloudWatch.
Exam signal
"Continuously rescan images already in the registry as new CVEs are published" means ECR enhanced scanning with Inspector. "Standard patched AMI every month across accounts" means an Image Builder pipeline with a schedule and a distribution configuration.
Exam trap
Basic scanning only looks at OS packages. A finding about a vulnerable Python or Java library needs enhanced scanning.
Scenarios
Enhanced scanning scans on push for the gate and keeps rescanning when new CVEs appear, emitting events for alerts. Basic scanning doesn't rescan continuously, and a weekly review doesn't block anything. CIS assessments check OS configuration of EC2, not image CVEs. GuardDuty Runtime Monitoring detects behaviour at run time, not known vulnerabilities before deployment.
Related
Further reading
Security Lake and OCSF Integrations
Centralize AWS, SaaS and third-party security logs in OCSF format with Amazon Security Lake and AWS AppFabric.
Vulnerability scanning and patching
Amazon Inspector for EC2, ECR, Lambda and code repositories, GuardDuty Runtime Monitoring, Systems Manager Patch Manager and State Manager, and security scanning inside CI/CD pipelines.