Skip to Content
Asterrr's Handbook
3 · Infrastructure security

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.

build and testsnapshotdistributeRecipe andcomponentsImage pipelineTemporary buildinstanceHardened AMIAuto Scalinggroup
An Image Builder pipeline builds on a temporary instance, tests the result, and distributes the AMI to Regions and accounts that an Auto Scaling launch template can use.

EC2 Image Builder

ItemWhat to remember
PipelineAutomation that runs a recipe on a schedule and distributes the output
RecipeBase image plus ordered build and test components. Container recipes produce Docker images
ComponentsYAML or JSON documents. AWS provides public ones, including STIG hardening components
StagesBuild, validate, test. The image is distributed only if tests pass
DistributionMultiple Regions, KMS encryption of the AMI, sharing across accounts and Organizations
SharingComponents, images and recipes can be shared with AWS RAM
CostNo charge for Image Builder itself. You pay for the instances, storage and scanning it uses
OutputAMI or container image (pushed to ECR)

Image Builder launches temporary instances, so their instance profile must carry these managed policies.

PolicyPurpose
EC2InstanceProfileForImageBuilderMinimum permissions to build an AMI
EC2InstanceProfileForImageBuilderECRContainerBuildsECR access for container builds
AmazonSSMManagedInstanceCoreSystems 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:Decrypt and kms:CreateGrant on 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

TypeEngineScopeFrequencies
BasicAWS native scanning with the CVE databaseOperating system packagesScan on push, or manual
EnhancedAmazon InspectorOS and programming language packagesScan 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

Scenario
A SaaS company builds container images in CodePipeline and pushes them to ECR. A recent incident involved an image with a critical OpenSSL CVE that reached production. The company wants to stop images with critical findings from being deployed and to be alerted when a CVE is later published for an image already running. Which approach meets both requirements?

Further reading

On this page