Building and Enforcing Hardened Images
This lesson explains how building hardened, tested AMIs and container images centrally means every instance or container launches with the right controls already in place.
Learning outcomes
After completing this lesson, you will be able to:
- Describe how EC2 Image Builder produces hardened images.
- Identify what a hardened image contains.
- Enforce approved images with Allowed AMIs and policies.
- Describe hardening and scanning practices for container images.
EC2 Image Builder
EC2 Image Builder produces hardened images through an automated pipeline. The process consists of the following steps.
-
Build
A recipe combines a base image with components: OS updates, CIS or STIG hardening, SSM and CloudWatch agents, security tools.
-
Test and scan
The pipeline tests the image and scans it with Inspector.
-
Distribute
Approved AMIs or container images are distributed to accounts and Regions on a schedule; old images are deprecated automatically.
An Image Builder pipeline
What a hardened image contains
A hardened image includes only what is necessary and is configured securely from the start. The following list describes what such an image contains.
- Minimal packages and services.
- No credentials, keys or secrets (fetch them at runtime from Secrets Manager).
- IMDSv2 required.
- Logging and monitoring agents, disk encryption, host firewall settings.
In the image or not?
Enforcing approved images
Building hardened images is only effective if instances cannot be launched from other images, and three controls enforce this. The EC2 Allowed AMIs setting limits which AMI providers or image IDs can be discovered and launched in an account, and declarative policies in AWS Organizations apply it across every account. SCPs can deny RunInstances unless the image meets conditions, such as an approved ec2:Owner or an image tag. AWS Config rules detect instances already running from unapproved AMIs, so they can be reported or replaced. The following table summarises the controls.
| Control | How |
|---|---|
| Allowed AMIs setting | Limits launchable AMI providers or IDs; deployable with declarative policies |
| SCPs | Conditions such as ec2:Owner or image tags |
| AWS Config rules | Detect instances from unapproved AMIs |
The following diagram shows how an allowed AMIs setting, applied across an organisation, decides which instance launches proceed.

Enforcing approved images
Container images
Container images require their own hardening and scanning practices. The following list describes them.
- Minimal or distroless bases; sign images; enable ECR tag immutability.
- Scan in ECR with Inspector enhanced scanning, on push and continuously.
- Run as non-root, with read-only root file systems and dropped Linux capabilities.
Hardening container images
Allowing only approved AMIs
Developers occasionally launch community AMIs of unknown origin. The organisation wants only images built by its own Image Builder account (999999999999) or published by Amazon. The EC2 Allowed AMIs setting, applied organisation-wide with a declarative policy, enforces this at launch:
{
"ec2_attributes": {
"allowed_images_settings": {
"state": { "@@assign": "enabled" },
"image_criteria": {
"criteria_1": { "allowed_image_providers": { "@@append": ["999999999999", "amazon"] } }
}
}
}
}Starting the setting in audit-mode first shows which running instances would be affected without blocking anything. Image Builder's distribution settings share each new hardened AMI with the organisation, and its lifecycle policies deprecate old ones so they cannot be chosen by mistake.
Why should secrets never be baked into AMIs?
Show answer
Anyone who can launch or copy the image can read them, and they cannot be rotated. Secrets are retrieved at runtime instead.
Exam Tips
Remember for the exam
- An automated pipeline for hardened AMIs, with testing and scanning, is EC2 Image Builder.
- To allow only approved AMIs to be launched, use the allowed AMIs setting, a declarative policy or an SCP.
- Never bake secrets into images; retrieve them at runtime.
- To scan container images on push and continuously, use ECR with Inspector.
- Containers should run as non-root, with read-only file systems.