Automating AMI and Container Image Builds with EC2 Image Builder
Golden images, which are pre-configured and hardened AMIs or container images, make instances and containers start quickly and consistently. EC2 Image Builder automates building, testing, distributing and retiring these images. This lesson explains Image Builder's components, recipes, infrastructure and distribution configurations, image pipelines and workflows, and how its output integrates with deployments.
Learning outcomes
After completing this lesson, you will be able to:
- Explain why golden images are used and how they compare with configuring instances at launch.
- Describe Image Builder components, image recipes, container recipes, infrastructure configurations and distribution settings.
- Explain how image pipelines are scheduled, tested, scanned and retired with lifecycle policies.
- Describe how new images are propagated to launch templates, Auto Scaling groups and pipelines.
Golden Images Versus Configuration at Launch
An instance can be configured at launch with user data scripts or configuration management, or it can start from a golden image that already contains the operating system patches, agents, security hardening and application dependencies. Configuring at launch keeps images simple but makes every launch slower and dependent on package repositories being available, which harms scaling during traffic spikes. Golden images launch quickly and identically, which suits Auto Scaling groups that must scale out fast, and they can be tested before use. Most organisations combine the two: a golden image contains everything that changes rarely, and launch-time configuration supplies environment-specific values.
Building golden images by hand is error-prone and quickly falls behind on patches. EC2 Image Builder automates the process at no additional charge beyond the resources it uses, such as the build instances and the storage of the images.
Image Builder Building Blocks
Image Builder is configured through several reusable resources. Each one is versioned with semantic versions (major.minor.patch), and Image Builder adds a build number to each image.
- Component
- A YAML document that defines steps in phases:
build(install and configure software),validate(check the build on the build instance) andtest(run on a test instance launched from the new image). Steps use actions such as ExecuteBash, ExecutePowerShell, S3Download and Reboot. AWS provides managed components, for example for installing the CloudWatch agent, applying updates and CIS or STIG hardening. - Image recipe
- Defines an AMI: the parent image (an AMI or an Image Builder managed image), the ordered build and test components, block device mappings and the working directory.
- Container recipe
- Defines a container image: the parent image, components, a Dockerfile template and the target Amazon ECR repository.
- Infrastructure configuration
- Defines the build and test instances: instance types, the IAM instance profile, subnet and security groups, an SNS topic for notifications, S3 logging and whether to terminate instances on failure.
- Distribution settings
- Defines where outputs go: target Regions and accounts, AMI names and tags, launch permissions for accounts, OUs or the organisation, launch templates to update and, for Windows, EC2 Fast Launch.
- Image pipeline
- Ties a recipe, an infrastructure configuration and distribution settings together with a schedule and testing options.
The build instances are managed through AWS Systems Manager, so the instance profile must include AmazonSSMManagedInstanceCore and EC2InstanceProfileForImageBuilder (or EC2InstanceProfileForImageBuilderECRContainerBuilds for container images), and the subnet must allow the instance to reach Systems Manager and the package sources, through a NAT gateway or VPC endpoints. A build that stays in the building state and then fails with an SSM timeout usually lacks this connectivity or the instance profile.
The following diagram shows how the building blocks fit into a pipeline and how its images are used.

Image Builder resources
Image Pipelines, Testing and Scanning
An image pipeline can run manually, on a cron schedule or on a schedule with the condition that it runs only if dependency updates are available, meaning that the parent image or a component with a wildcard version such as 1.x.x has a newer version. The conditional option rebuilds images when a new patched AWS base image is published without producing identical images on other days.
During a build, Image Builder launches a build instance from the parent image, runs the build and validate phases, creates the image, then launches a test instance from the new image and runs the test phase. If any step fails, the image is not distributed. Image Builder can also scan the output with Amazon Inspector, recording findings with the image. Image workflows customise the build and test sequence, for example adding a step that waits for an external approval or runs additional checks.
Old images accumulate cost and risk being used after they are superseded. Image Builder lifecycle policies deprecate, disable or delete images and their snapshots based on age or count, with rules that can keep a minimum number of versions. Deprecated AMIs remain launchable by those who know their ID but are hidden from general listings.
Propagating New Images to Deployments
Producing an image is only half of the task; workloads must start using it. Several integration patterns are used, often together.
- Launch template updates: distribution settings can create a new launch template version that references the new AMI and optionally set it as the default. An Auto Scaling group that uses the default or latest version then uses the new AMI for new instances, and an instance refresh replaces existing instances gradually.
- Systems Manager parameters: a subscriber to the Image Builder SNS notification, such as a Lambda function, writes the new AMI ID to a Parameter Store parameter. CloudFormation templates and launch templates can resolve AMI IDs from parameters (for example with the
AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>parameter type orresolve:ssm:in a launch template), so the next deployment uses the latest image. Image Builder distribution can also write the AMI ID to a parameter directly. - Pipelines: an EventBridge rule on the Image Builder image state change event or the SNS notification starts a CodePipeline release that tests and rolls out the new image.
- Container images: container recipes push to ECR, where an ECR source action can start a deployment pipeline.
Roll out golden images
Monthly Hardened AMIs for Many Accounts
A company runs Auto Scaling groups in 40 accounts and three Regions. Security requires that instances use an AMI with the latest patches, CIS hardening and the CloudWatch agent, no more than 30 days old, and that each image is tested before use.
-
Recipe
Create an image recipe from the latest Amazon Linux AMI with AWS managed components for updates, CIS hardening and the CloudWatch agent, plus a custom test component that verifies the agent is running and required ports are closed.
-
Pipeline
Create an image pipeline that runs weekly when dependency updates are available, with Inspector scanning enabled.
-
Distribution
Distribute to the three Regions, share launch permissions with the organisation's workload OUs and write the AMI ID to a Parameter Store parameter.
-
Rollout and retirement
An EventBridge rule on successful builds starts a pipeline that updates launch templates and starts instance refreshes; a lifecycle policy deprecates images after 30 days and deletes them after 90.
An Image Builder build remains in the building state for a long time and then fails with a timeout waiting for the instance to be managed by Systems Manager. What are the likely causes?
Show answer
The infrastructure configuration's instance profile lacks AmazonSSMManagedInstanceCore (and the Image Builder policy), or the subnet has no route to the Systems Manager endpoints through a NAT gateway or VPC endpoints.
Exam Tips
Remember for the exam
- EC2 Image Builder automates golden AMIs and container images: components (build, validate, test phases), recipes, infrastructure configuration, distribution settings and pipelines.
- Schedule pipelines to run only when dependency updates are available to rebuild when the parent image or components change.
- Build instances need an instance profile with AmazonSSMManagedInstanceCore and network access to Systems Manager.
- Propagate new AMIs through launch template versions plus instance refresh, Parameter Store parameters or event-driven pipelines.
- Image Builder lifecycle policies deprecate, disable and delete old images automatically.