8.1 Golden Images with EC2 Image Builder & Amazon ECR
Key Takeaways
- EC2 Image Builder automates the building, testing, and distribution of hardened golden AMIs and container images across AWS accounts and Regions.
- Image recipes define base OS images, build components (for software and CIS/STIG hardening), and test components, while infrastructure configurations specify worker instance types, VPC subnets, and IAM roles.
- Distribution configurations govern cross-account sharing (via AWS Organizations or account IDs), multi-Region replication, and mandatory AWS KMS re-encryption using target Customer Managed Keys (CMKs).
- Amazon ECR tag immutability prevents image tag tampering or accidental overwrites, while ECR lifecycle policies automatically purge untagged images or retain the last N tagged images to optimize storage costs.
- ECR offers basic vulnerability scanning (Clair, scan-on-push) and enhanced scanning (Amazon Inspector, continuous CVE vulnerability detection across repositories and running container workloads).
8.1 Golden Images with EC2 Image Builder & Amazon ECR
CloudOps Blueprint Focus: The AWS Certified CloudOps Engineer – Associate (SOA-C03) exam tests your ability to automate golden image creation, enforce security baselines with EC2 Image Builder, manage cross-account and cross-Region AMI distribution, secure container registries with Amazon ECR tag immutability and lifecycle policies, and detect vulnerabilities using basic and enhanced Amazon Inspector scanning.
EC2 Image Builder Architecture & Pipelines
Modern cloud operations mandate immutable infrastructure patterns where machine images (AMIs) and container images are standardized, hardened, and tested before deployment. AWS EC2 Image Builder provides an automated pipeline service to produce and validate golden AMIs and container images without custom maintenance scripts.
An Image Builder pipeline orchestrates three core resources:
- Image Recipe (or Container Recipe): Combines a base operating system image with build and test components.
- Infrastructure Configuration: Defines compute capacity, VPC networking, and IAM permissions used to run the build.
- Distribution Configuration: Defines target AWS accounts, AWS Organizations Organizational Units (OUs), and AWS Regions receiving the final image.
Recipe Components: Build and Test Phases
An Image Recipe pairs a parent image (e.g., Amazon Linux 2023 or Windows Server) with declarative YAML Components across three sequential phases:
- Build Phase: Installs software, CloudWatch and Systems Manager agents, and executes CIS benchmarks or DISA STIG hardening scripts.
- Validate Phase: Verifies software installations and service states.
- Test Phase: Executes automated smoke and security tests (e.g., InSpec, OpenSCAP).
If any test step returns a non-zero exit code, Image Builder aborts the pipeline, marks the build as failed, and terminates the worker instance, preventing defective images from publishing.
Infrastructure Configuration & Build Environment
The Infrastructure Configuration dictates the temporary EC2 worker environment:
- Compute & Networking: Defines instance type (e.g.,
t3.medium), VPC subnet, and security group. In private subnets, VPC endpoints for Systems Manager (ssm,ssmmessages,ec2messages) and S3 are mandatory, as Image Builder orchestrates builds via Systems Manager Run Command. - IAM Instance Profile: Attaches an IAM role containing
EC2InstanceProfileForImageBuilderandAmazonSSMManagedInstanceCore. - S3 Logging: Directs component logs and console outputs to an S3 bucket for auditing.
Distribution Configuration & KMS Re-Encryption
The Distribution Configuration defines multi-account and multi-Region distribution:
- Target Regions: Replicates finalized AMIs to destination Regions, assigning regional AMI IDs while preserving metadata and tags.
- Launch Permissions: Grants specific AWS account IDs or an entire AWS Organizations OU rights to launch instances from the distributed AMI.
- KMS Key Re-Encryption: Encrypts EBS snapshots in target accounts and Regions.
| Parameter | Operational Function | KMS & Security Requirements |
|---|---|---|
| Launch Permissions | Shares AMI across AWS accounts or Organizations OUs. | Allows member accounts to launch EC2 instances directly. |
| Regional Replication | Copies golden AMI to target destination Regions. | Requires regional KMS CMKs in each destination Region. |
| KMS Encryption Key | Re-encrypts snapshots during cross-account/Region copy. | AWS-managed keys (aws/ebs) cannot be shared cross-account. Must specify destination CMK ARNs. |
[!IMPORTANT] EBS snapshots encrypted with default AWS-managed KMS keys (
aws/ebs) cannot be shared cross-account. To distribute encrypted AMIs to member accounts, the distribution configuration must specify destination KMS Customer Managed Keys (CMKs), and destination key policies must permit Image Builder to create grants and re-encrypt snapshots.
Image Pipeline Execution Triggers
Image Pipelines support three execution trigger patterns:
- Manual Execution: Triggered on demand via the console, AWS CLI (
aws imagebuilder start-image-pipeline-execution), or CI/CD pipelines. - Schedule Expressions: Automated recurring builds defined via cron or rate expressions (e.g., weekly builds for OS security patches).
- Event-Driven Triggers via Amazon EventBridge: Executes automatically when upstream base AMIs are updated by AWS or when vulnerability alerts trigger remediation workflows.
Amazon Elastic Container Registry (ECR) Governance
Amazon ECR provides a managed OCI container registry requiring strict operational governance.
Tag Immutability
By default, ECR allows tag mutability, allowing tags like latest or v1.2.0 to be overwritten. Enabling Tag Immutability guarantees that pushed tags cannot be altered, rejecting duplicate tags with ImageTagAlreadyExistsException.
ECR Lifecycle Policies
To prevent unbounded storage costs, ECR Lifecycle Policies evaluate declarative JSON rules in priority order:
- Untagged Images: Purges intermediate or untagged images after a set retention window (e.g., 7 days).
- Tagged Image Count: Retains only the last $N$ images matching a prefix (e.g., retaining the 20 most recent
prod-*images). - Tagged Image Age: Expires non-production images (e.g.,
test-*) older than 30 days.
ECR Image Vulnerability Scanning: Basic vs. Enhanced
Amazon ECR offers two vulnerability scanning tiers to secure container images:
| Feature | Basic Scanning | Enhanced Scanning (Amazon Inspector) |
|---|---|---|
| Engine | Open-source Clair engine. | Amazon Inspector vulnerability engine. |
| Frequency | Manual on-demand or Scan-on-Push. | Continuous Scanning on push and ongoing CVE monitoring. |
| Package Scope | Operating system packages only. | Operating system packages and application language dependencies. |
| Runtime Context | Static repository findings only. | Correlates findings with running tasks in Amazon ECS and EKS. |
Enhanced scanning eliminates the blind spot where an image passes scan-on-push inspection but becomes vulnerable later when a new CVE is published.
Cross-Account & Cross-Region ECR Replication
Amazon ECR supports native replication for enterprise architectures:
- Cross-Region Replication: Replicates pushed images to secondary operating Regions, eliminating cross-Region pull latencies and data transfer costs.
- Cross-Account Replication: Synchronizes golden container images from a central build account to member production accounts across AWS Organizations using repository prefix filters (e.g.,
prod/*).
A multinational financial enterprise uses EC2 Image Builder to produce hardened golden AMIs in a central security account. These AMIs must be distributed to member accounts in the organization across us-east-1 and eu-west-1. The security team mandates that all AMIs must be encrypted using customer managed keys (CMKs) in each target account and Region. When testing the distribution configuration, copy jobs to the member accounts fail with an access denied encryption error. What is the root cause and required fix?
A platform engineering team manages an Amazon Elastic Container Registry (ECR) repository for a production payment microservice. Due to compliance and cost governance, the team must satisfy two requirements: developers must be prevented from overwriting existing container image tags (such as v1.2.0), and storage costs must be minimized by automatically purging untagged images older than 7 days while retaining only the 20 most recent images tagged with the prefix prod-. How should the CloudOps engineer configure the ECR repository?
A security compliance audit reveals that an organization's container workloads running on Amazon ECS are vulnerable to newly discovered Common Vulnerabilities and Exposures (CVEs) that were published after the container images were initially built and pushed to Amazon ECR. The current ECR configuration only runs a basic scan once when an image is first pushed. Which operational solution provides continuous, automated CVE vulnerability monitoring for container images stored in ECR without requiring manual rescanning?