Lesson 1 of 6 Compute Security: EC2, Containers and Lambda

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.

  1. Build

    A recipe combines a base image with components: OS updates, CIS or STIG hardening, SSM and CloudWatch agents, security tools.

  2. Test and scan

    The pipeline tests the image and scans it with Inspector.

  3. Distribute

    Approved AMIs or container images are distributed to accounts and Regions on a schedule; old images are deprecated automatically.

Put in order

An Image Builder pipeline

Exam skills 3.2.1

Arrange the stages of a hardened image pipeline.

Log in to try this activity Interactive activities are free with an account.

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.
Sort into groups

In the image or not?

Exam skills 3.2.1

Decide what belongs in a hardened AMI.

Log in to try this activity Interactive activities are free with an account.

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.

ControlHow
Allowed AMIs settingLimits launchable AMI providers or IDs; deployable with declarative policies
SCPsConditions such as ec2:Owner or image tags
AWS Config rulesDetect instances from unapproved AMIs

The following diagram shows how an allowed AMIs setting, applied across an organisation, decides which instance launches proceed.

Allowed AMIs, applied organisation-wide by a declarative policy. Launches are allowed only from images whose provider is the Image Builder account 999999999999 or Amazon, so community AMIs are refused. Audit mode first shows the effect; Image Builder shares new hardened AMIs and deprecates old ones.
The scenario is illustrative.
Match the pairs

Enforcing approved images

Exam skills 3.2.1 · 6.1.3

Match each control with how it enforces approved AMIs.

Log in to try this activity Interactive activities are free with an account.

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.
Select all that apply

Hardening container images

Exam skills 3.2.1 · 3.2.3

Pick the practices for hardening and scanning container images.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.